2021-12-02 15:21:10 +05:00
|
|
|
|
import { MouseEventHandler, useState } from 'react'
|
|
|
|
|
import { Link, useHistory } from 'react-router-dom'
|
2021-12-27 18:06:26 +05:00
|
|
|
|
import { Button, Dropdown, Menu } from 'antd'
|
2021-12-02 15:21:10 +05:00
|
|
|
|
import { UserOutlined } from '@ant-design/icons'
|
2021-12-27 18:06:26 +05:00
|
|
|
|
|
2021-12-29 17:48:10 +05:00
|
|
|
|
import { getUserLogin, removeUser } from '../utils/storage'
|
|
|
|
|
|
2021-12-02 15:21:10 +05:00
|
|
|
|
import { PrivateMenuItem } from './Private'
|
2021-12-27 18:06:26 +05:00
|
|
|
|
import { ChangePassword } from './ChangePassword'
|
2021-12-02 15:21:10 +05:00
|
|
|
|
|
2021-12-02 15:49:41 +05:00
|
|
|
|
type UserMenuProps = {
|
|
|
|
|
isAdmin?: boolean
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const UserMenu: React.FC<UserMenuProps> = ({ isAdmin }) => {
|
2021-12-02 15:21:10 +05:00
|
|
|
|
const [isModalVisible, setIsModalVisible] = useState<boolean>(false)
|
|
|
|
|
|
|
|
|
|
const history = useHistory()
|
|
|
|
|
|
|
|
|
|
const onChangePasswordClick: MouseEventHandler = (e) => {
|
|
|
|
|
setIsModalVisible(true)
|
|
|
|
|
e.preventDefault()
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-27 18:06:26 +05:00
|
|
|
|
const onChangePasswordOk = () => {
|
|
|
|
|
setIsModalVisible(false)
|
|
|
|
|
history.push('/login')
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-02 15:21:10 +05:00
|
|
|
|
return (
|
|
|
|
|
<>
|
|
|
|
|
<Dropdown
|
|
|
|
|
placement={'bottomRight'}
|
|
|
|
|
overlay={(
|
|
|
|
|
<Menu style={{ textAlign: 'right' }}>
|
2021-12-29 17:48:10 +05:00
|
|
|
|
<PrivateMenuItem permission={'admin_panel'}>
|
2021-12-02 15:49:41 +05:00
|
|
|
|
{isAdmin ? (
|
|
|
|
|
<Link to={'/'}>Вернуться на сайт</Link>
|
|
|
|
|
) : (
|
|
|
|
|
<Link to={'/admin'}>Панель администратора</Link>
|
|
|
|
|
)}
|
2021-12-02 15:21:10 +05:00
|
|
|
|
</PrivateMenuItem>
|
|
|
|
|
<Menu.Item>
|
|
|
|
|
<Link to={'/'} onClick={onChangePasswordClick}>Сменить пароль</Link>
|
|
|
|
|
</Menu.Item>
|
|
|
|
|
<Menu.Item>
|
2021-12-29 17:48:10 +05:00
|
|
|
|
<Link to={'/login'} onClick={removeUser}>Выход</Link>
|
2021-12-02 15:21:10 +05:00
|
|
|
|
</Menu.Item>
|
|
|
|
|
</Menu>
|
|
|
|
|
)}
|
|
|
|
|
>
|
2021-12-29 17:48:10 +05:00
|
|
|
|
<Button icon={<UserOutlined/>}>{getUserLogin()}</Button>
|
2021-12-02 15:21:10 +05:00
|
|
|
|
</Dropdown>
|
2021-12-27 18:06:26 +05:00
|
|
|
|
<ChangePassword
|
2021-12-02 15:21:10 +05:00
|
|
|
|
visible={isModalVisible}
|
2021-12-27 18:06:26 +05:00
|
|
|
|
onOk={onChangePasswordOk}
|
|
|
|
|
onCancel={() => setIsModalVisible(false)}
|
|
|
|
|
/>
|
2021-12-02 15:21:10 +05:00
|
|
|
|
</>
|
|
|
|
|
)
|
|
|
|
|
}
|