2023-12-13 14:32:57 +03:00
|
|
|
'use client';
|
|
|
|
|
2023-12-18 19:42:27 +03:00
|
|
|
import clsx from 'clsx';
|
2023-12-13 15:03:58 +03:00
|
|
|
import { useLayoutEffect, useMemo, useState } from 'react';
|
2023-08-26 17:26:49 +03:00
|
|
|
import { toast } from 'react-toastify';
|
|
|
|
|
2024-01-04 19:38:12 +03:00
|
|
|
import SubmitButton from '@/components/ui/SubmitButton';
|
|
|
|
import TextInput from '@/components/ui/TextInput';
|
2023-12-26 14:23:51 +03:00
|
|
|
import { useBlockNavigation } from '@/context/NavigationContext';
|
2023-12-13 14:32:57 +03:00
|
|
|
import { useUserProfile } from '@/context/UserProfileContext';
|
|
|
|
import { IUserUpdateData } from '@/models/library';
|
2023-08-26 17:26:49 +03:00
|
|
|
|
|
|
|
function EditorProfile() {
|
|
|
|
const { updateUser, user, processing } = useUserProfile();
|
2023-12-28 14:04:44 +03:00
|
|
|
|
2023-08-26 17:26:49 +03:00
|
|
|
const [username, setUsername] = useState('');
|
|
|
|
const [email, setEmail] = useState('');
|
|
|
|
const [first_name, setFirstName] = useState('');
|
|
|
|
const [last_name, setLastName] = useState('');
|
|
|
|
|
2023-12-28 14:04:44 +03:00
|
|
|
const isModified: boolean = useMemo(() => {
|
2023-08-27 15:39:49 +03:00
|
|
|
if (!user) {
|
2023-12-13 14:32:57 +03:00
|
|
|
return false;
|
2023-08-27 15:39:49 +03:00
|
|
|
}
|
2023-12-28 14:04:44 +03:00
|
|
|
return user.email !== email || user.first_name !== first_name || user.last_name !== last_name;
|
2023-12-13 14:32:57 +03:00
|
|
|
}, [user, email, first_name, last_name]);
|
2023-12-13 15:03:58 +03:00
|
|
|
useBlockNavigation(isModified);
|
2023-08-27 15:39:49 +03:00
|
|
|
|
2023-08-26 17:26:49 +03:00
|
|
|
useLayoutEffect(() => {
|
|
|
|
if (user) {
|
|
|
|
setUsername(user.username);
|
|
|
|
setEmail(user.email);
|
|
|
|
setFirstName(user.first_name);
|
|
|
|
setLastName(user.last_name);
|
|
|
|
}
|
|
|
|
}, [user]);
|
2023-12-28 14:04:44 +03:00
|
|
|
|
2023-08-26 17:26:49 +03:00
|
|
|
function handleSubmit(event: React.FormEvent<HTMLFormElement>) {
|
|
|
|
event.preventDefault();
|
|
|
|
const data: IUserUpdateData = {
|
|
|
|
username: username,
|
|
|
|
email: email,
|
|
|
|
first_name: first_name,
|
2023-12-28 14:04:44 +03:00
|
|
|
last_name: last_name
|
2023-08-26 17:26:49 +03:00
|
|
|
};
|
|
|
|
updateUser(data, () => toast.success('Изменения сохранены'));
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2024-03-19 19:19:08 +03:00
|
|
|
<form onSubmit={handleSubmit} className={clsx('cc-column', 'min-w-[18rem]', 'px-6 py-2')}>
|
2024-03-18 16:21:39 +03:00
|
|
|
<TextInput
|
|
|
|
id='username'
|
|
|
|
autoComplete='username'
|
|
|
|
disabled
|
|
|
|
label='Логин'
|
|
|
|
title='Логин изменить нельзя'
|
|
|
|
value={username}
|
|
|
|
/>
|
2023-12-28 14:04:44 +03:00
|
|
|
<TextInput
|
|
|
|
id='first_name'
|
2024-03-18 16:21:39 +03:00
|
|
|
autoComplete='off'
|
2023-12-28 14:04:44 +03:00
|
|
|
allowEnter
|
|
|
|
label='Имя'
|
|
|
|
value={first_name}
|
|
|
|
onChange={event => setFirstName(event.target.value)}
|
|
|
|
/>
|
|
|
|
<TextInput
|
|
|
|
id='last_name'
|
2024-03-18 16:21:39 +03:00
|
|
|
autoComplete='off'
|
2023-12-28 14:04:44 +03:00
|
|
|
allowEnter
|
|
|
|
label='Фамилия'
|
|
|
|
value={last_name}
|
|
|
|
onChange={event => setLastName(event.target.value)}
|
|
|
|
/>
|
|
|
|
<TextInput
|
|
|
|
id='email'
|
2024-03-18 16:21:39 +03:00
|
|
|
autoComplete='off'
|
2023-12-28 14:04:44 +03:00
|
|
|
allowEnter
|
|
|
|
label='Электронная почта'
|
|
|
|
value={email}
|
|
|
|
onChange={event => setEmail(event.target.value)}
|
|
|
|
/>
|
|
|
|
<SubmitButton className='self-center mt-6' text='Сохранить данные' loading={processing} disabled={!isModified} />
|
|
|
|
</form>
|
|
|
|
);
|
2023-08-26 17:26:49 +03:00
|
|
|
}
|
|
|
|
|
2023-12-28 14:04:44 +03:00
|
|
|
export default EditorProfile;
|