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-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
|
|
|
|
|
|
|
|
|
const handleLogout = () => {
|
|
|
|
|
localStorage.removeItem('login')
|
|
|
|
|
localStorage.removeItem('token')
|
|
|
|
|
}
|
|
|
|
|
|
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-21 15:37:23 +05:00
|
|
|
|
<PrivateMenuItem roles={'admin'}>
|
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>
|
|
|
|
|
<Link to={'/login'} onClick={handleLogout}>Выход</Link>
|
|
|
|
|
</Menu.Item>
|
|
|
|
|
</Menu>
|
|
|
|
|
)}
|
|
|
|
|
>
|
|
|
|
|
<Button icon={<UserOutlined/>}>{localStorage['login']}</Button>
|
|
|
|
|
</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
|
|
|
|
</>
|
|
|
|
|
)
|
|
|
|
|
}
|