temp
This commit is contained in:
parent
c781252cb3
commit
c1b57c430d
@ -184,7 +184,7 @@
|
|||||||
"id": 1,
|
"id": 1,
|
||||||
"email": "myemail@mail.com",
|
"email": "myemail@mail.com",
|
||||||
"password": "12345678",
|
"password": "12345678",
|
||||||
"expirationDate": null
|
"expirationDate": "2025-01-12T20:22:19.846Z"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"id": 2,
|
"id": 2,
|
||||||
|
Binary file not shown.
Before Width: | Height: | Size: 551 KiB |
@ -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;
|
|
@ -3,7 +3,7 @@ import {
|
|||||||
} from 'react-bootstrap';
|
} from 'react-bootstrap';
|
||||||
|
|
||||||
import {
|
import {
|
||||||
PersonCircle, EnvelopeFill, LockFill, EyeFill,
|
PersonCircle, EnvelopeFill, LockFill,
|
||||||
} from 'react-bootstrap-icons';
|
} from 'react-bootstrap-icons';
|
||||||
|
|
||||||
import { useState } from 'react';
|
import { useState } from 'react';
|
||||||
@ -26,8 +26,6 @@ const PageLogin = () => {
|
|||||||
await handleSubmit(event);
|
await handleSubmit(event);
|
||||||
};
|
};
|
||||||
|
|
||||||
// <InputGroup.Text><EyeFill /></InputGroup.Text>
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<section id="login">
|
<section id="login">
|
||||||
|
BIN
Отчет лаб5.docx
BIN
Отчет лаб5.docx
Binary file not shown.
Loading…
Reference in New Issue
Block a user