import { memo, ReactNode, useCallback, useState } from 'react' import { useNavigate, useLocation } from 'react-router-dom' import { Button, Collapse, Drawer, DrawerProps, Form, FormRule, Input, Popconfirm } from 'antd' import { useForm } from 'antd/lib/form/Form' import { useUser } from '@asb/context' import { Grid, GridItem } from '@components/Grid' import LoaderPortal from '@components/LoaderPortal' import { invokeWebApiWrapperAsync } from '@components/factory' import { isURLAvailable, removeUser } from '@utils' import { AuthService } from '@api' import '@styles/user_menu.less' export type UserMenuProps = DrawerProps & { isAdmin?: boolean additional?: ReactNode } type ChangePasswordForm = { 'new-password': string } const newPasswordRules: FormRule[] = [{ required: true, message: 'Пожалуйста, введите новый пароль!' }] const confirmPasswordRules: FormRule[] = [({ getFieldValue }) => ({ validator(_, value: string) { if (value !== getFieldValue('new-password')) return Promise.reject('Пароли не совпадают!') return Promise.resolve() }})] export const UserMenu = memo(({ isAdmin, additional, ...other }) => { const [showLoader, setShowLoader] = useState(false) const [changeLoginForm] = useForm() const navigate = useNavigate() const location = useLocation() const user = useUser() const navigateTo = useCallback((to: string) => navigate(to, { state: { from: location.pathname }}), [navigate, location.pathname]) const onChangePasswordOk = useCallback(() => invokeWebApiWrapperAsync( async (values: any) => { await AuthService.changePassword(user.id ?? -1, `${values['new-password']}`) removeUser() navigateTo('/login') }, setShowLoader, `Не удалось сменить пароль пользователя ${user.login}`, { actionName: 'Смена пароля пользователя' }, ), [navigateTo]) const logout = useCallback(() => { removeUser() navigateTo('/login') }, [navigateTo]) return (
{isAdmin ? ( ) : isURLAvailable('/admin') && ( )}
Логин: {user.login} Фамилия: {user.surname} Имя: {user.name} Отчество: {user.patronymic} E-mail: {user.email}
{additional}
) })