asb_cloud_front/src/components/UserMenu.tsx
goodm2ice 85f98044c6 * Добавлен компонент для получения Route по умолчанию
* Доблена страница "Доступ запрещён"
* Актуализирована работа с правами
* Добавлен переключатель оси X TVD
2022-02-07 14:58:38 +05:00

59 lines
2.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { memo, MouseEventHandler, useState } from 'react'
import { Link, useHistory } from 'react-router-dom'
import { Button, Dropdown, DropDownProps, Menu } from 'antd'
import { UserOutlined } from '@ant-design/icons'
import { getUserLogin, removeUser } from '@utils/storage'
import { ChangePassword } from './ChangePassword'
import { PrivateMenuItemLink } from './Private/PrivateMenuItem'
type UserMenuProps = Omit<DropDownProps, 'overlay'> & { isAdmin?: boolean }
export const UserMenu = memo<UserMenuProps>(({ isAdmin, ...other }) => {
const [isModalVisible, setIsModalVisible] = useState<boolean>(false)
const history = useHistory()
const onChangePasswordClick: MouseEventHandler = (e) => {
setIsModalVisible(true)
e.preventDefault()
}
const onChangePasswordOk = () => {
setIsModalVisible(false)
history.push('/login')
}
return (
<>
<Dropdown
{...other}
placement={'bottomRight'}
overlay={(
<Menu style={{ textAlign: 'right' }}>
{isAdmin ? (
<PrivateMenuItemLink path={'/'} title={'Вернуться на сайт'}/>
) : (
<PrivateMenuItemLink path={'/admin'} title={'Панель администратора'}/>
)}
<Menu.Item>
<Link to={'/'} onClick={onChangePasswordClick}>Сменить пароль</Link>
</Menu.Item>
<Menu.Item>
<Link to={'/login'} onClick={removeUser}>Выход</Link>
</Menu.Item>
</Menu>
)}
>
<Button icon={<UserOutlined/>}>{getUserLogin()}</Button>
</Dropdown>
<ChangePassword
visible={isModalVisible}
onOk={onChangePasswordOk}
onCancel={() => setIsModalVisible(false)}
/>
</>
)
})