2022-02-25 16:57:08 +05:00
|
|
|
|
import { memo, MouseEventHandler, useCallback, useState } from 'react'
|
|
|
|
|
import { Link, useHistory, useLocation } from 'react-router-dom'
|
2022-02-07 14:58:38 +05:00
|
|
|
|
import { Button, Dropdown, DropDownProps, 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
|
|
|
|
|
2022-02-07 14:58:38 +05:00
|
|
|
|
import { getUserLogin, removeUser } from '@utils/storage'
|
2021-12-29 17:48:10 +05:00
|
|
|
|
|
2021-12-27 18:06:26 +05:00
|
|
|
|
import { ChangePassword } from './ChangePassword'
|
2022-02-07 14:58:38 +05:00
|
|
|
|
import { PrivateMenuItemLink } from './Private/PrivateMenuItem'
|
2021-12-02 15:21:10 +05:00
|
|
|
|
|
2022-02-07 14:58:38 +05:00
|
|
|
|
type UserMenuProps = Omit<DropDownProps, 'overlay'> & { isAdmin?: boolean }
|
2021-12-02 15:49:41 +05:00
|
|
|
|
|
2022-02-07 14:58:38 +05:00
|
|
|
|
export const UserMenu = memo<UserMenuProps>(({ isAdmin, ...other }) => {
|
2021-12-02 15:21:10 +05:00
|
|
|
|
const [isModalVisible, setIsModalVisible] = useState<boolean>(false)
|
|
|
|
|
|
|
|
|
|
const history = useHistory()
|
2022-02-25 16:57:08 +05:00
|
|
|
|
const location = useLocation()
|
2021-12-02 15:21:10 +05:00
|
|
|
|
|
2022-02-25 16:57:08 +05:00
|
|
|
|
const onChangePasswordClick: MouseEventHandler = useCallback((e) => {
|
2021-12-02 15:21:10 +05:00
|
|
|
|
setIsModalVisible(true)
|
|
|
|
|
e.preventDefault()
|
2022-02-25 16:57:08 +05:00
|
|
|
|
}, [])
|
2021-12-02 15:21:10 +05:00
|
|
|
|
|
2022-02-25 16:57:08 +05:00
|
|
|
|
const onChangePasswordOk = useCallback(() => {
|
2021-12-27 18:06:26 +05:00
|
|
|
|
setIsModalVisible(false)
|
2022-02-25 16:57:08 +05:00
|
|
|
|
history.push({ pathname: '/login', state: { from: location.pathname }})
|
|
|
|
|
}, [history, location])
|
2021-12-27 18:06:26 +05:00
|
|
|
|
|
2021-12-02 15:21:10 +05:00
|
|
|
|
return (
|
|
|
|
|
<>
|
|
|
|
|
<Dropdown
|
2022-02-07 14:58:38 +05:00
|
|
|
|
{...other}
|
2021-12-02 15:21:10 +05:00
|
|
|
|
placement={'bottomRight'}
|
|
|
|
|
overlay={(
|
|
|
|
|
<Menu style={{ textAlign: 'right' }}>
|
2022-02-07 14:58:38 +05:00
|
|
|
|
{isAdmin ? (
|
2022-02-25 16:57:08 +05:00
|
|
|
|
<PrivateMenuItemLink key={''} path={'/'} title={'Вернуться на сайт'}/>
|
2022-02-07 14:58:38 +05:00
|
|
|
|
) : (
|
2022-02-25 16:57:08 +05:00
|
|
|
|
<PrivateMenuItemLink key={'admin'} path={'/admin'} title={'Панель администратора'}/>
|
2022-02-07 14:58:38 +05:00
|
|
|
|
)}
|
2021-12-02 15:21:10 +05:00
|
|
|
|
<Menu.Item>
|
|
|
|
|
<Link to={'/'} onClick={onChangePasswordClick}>Сменить пароль</Link>
|
|
|
|
|
</Menu.Item>
|
|
|
|
|
<Menu.Item>
|
2022-02-25 16:57:08 +05:00
|
|
|
|
<Link to={{ pathname: '/login', state: { from: location.pathname }}} 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
|
|
|
|
</>
|
|
|
|
|
)
|
2022-02-07 14:58:38 +05:00
|
|
|
|
})
|