This commit is contained in:
ShabOl 2024-01-13 00:25:53 +04:00
parent c781252cb3
commit c1b57c430d
5 changed files with 2 additions and 101 deletions

View File

@ -184,7 +184,7 @@
"id": 1,
"email": "myemail@mail.com",
"password": "12345678",
"expirationDate": null
"expirationDate": "2025-01-12T20:22:19.846Z"
},
{
"id": 2,

Binary file not shown.

Before

Width:  |  Height:  |  Size: 551 KiB

View File

@ -1,97 +0,0 @@
import toast from 'react-hot-toast';
import { useEffect, useState } from 'react';
import UsersApiService from '../../authentification/service/UsersApiService';
const useUserSubscription = (userId) => {
const [subscriptionRefresh, setSubscriptionRefresh] = useState(false);
const [subscriptionInitialized, setSubscriptionInitialized] = useState(false);
const [currentUser, setCurrentUser] = useState({});
const [isSubscriptionActive, setIsSubscriptionActive] = useState(false);
const handleSubscriptionChange = () => setSubscriptionRefresh(!subscriptionRefresh);
const handleChange = async (userObject) => {
if (userId === -1) {
return;
}
await UsersApiService.update(userId, userObject);
handleSubscriptionChange();
toast.success('Статус подписки успешно обновлен');
};
const getExpirationDate = () => {
return currentUser.expirationDate;
};
const getExpirationDateFormatted = () => {
if (currentUser.expirationDate === null) {
return 'идёт отмена...';
}
const timestamp = Date.parse(currentUser.expirationDate);
const expirationDate = new Date(timestamp);
return expirationDate.toLocaleString('ru-RU').split(', ')[0];
};
const hasActiveSubscription = () => {
if (currentUser.expirationDate === null) {
return false;
}
const currentDate = new Date().getTime();
const expirationDate = Date.parse(getExpirationDate());
return currentDate <= expirationDate;
};
const setSubscription = (newExpirationDate) => {
currentUser.expirationDate = newExpirationDate.toISOString();
handleChange(currentUser);
};
const cancelSubscription = () => {
currentUser.expirationDate = null;
handleChange(currentUser);
};
const refreshUserData = async (id) => {
if (id === -1) {
return;
}
const data = await UsersApiService.get(id);
localStorage.setItem('currentUser', JSON.stringify(data));
setCurrentUser(data);
};
useEffect(() => {
refreshUserData(userId);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [subscriptionRefresh]);
useEffect(() => {
if (Object.keys(currentUser).length === 0) {
return;
}
setIsSubscriptionActive(hasActiveSubscription());
setSubscriptionInitialized(true);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [currentUser]);
return {
subscriptionInitialized,
isSubscriptionActive,
getExpirationDateFormatted,
cancelSubscription,
setSubscription,
};
};
export default useUserSubscription;

View File

@ -3,7 +3,7 @@ import {
} from 'react-bootstrap';
import {
PersonCircle, EnvelopeFill, LockFill, EyeFill,
PersonCircle, EnvelopeFill, LockFill,
} from 'react-bootstrap-icons';
import { useState } from 'react';
@ -26,8 +26,6 @@ const PageLogin = () => {
await handleSubmit(event);
};
// <InputGroup.Text><EyeFill /></InputGroup.Text>
return (
<>
<section id="login">

Binary file not shown.