2022-02-25 16:57:08 +05:00
|
|
|
import { memo, MouseEventHandler, useCallback, useState } from 'react'
|
2022-06-09 17:51:41 +05:00
|
|
|
import { useNavigate, useLocation } from 'react-router-dom'
|
|
|
|
import { Button, Dropdown, DropDownProps } 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-06-09 17:51:41 +05:00
|
|
|
import { getUserLogin, removeUser } from '@utils'
|
2021-12-27 18:06:26 +05:00
|
|
|
import { ChangePassword } from './ChangePassword'
|
2022-06-09 17:51:41 +05:00
|
|
|
import { PrivateMenu } from './Private'
|
|
|
|
|
|
|
|
import AdminPanel from '@pages/AdminPanel'
|
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)
|
|
|
|
|
2022-06-09 17:51:41 +05:00
|
|
|
const navigate = useNavigate()
|
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) => {
|
2022-06-09 17:51:41 +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-06-09 17:51:41 +05:00
|
|
|
navigate('/login', { state: { from: location.pathname }})
|
|
|
|
}, [navigate, 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={(
|
2022-06-09 17:51:41 +05:00
|
|
|
<PrivateMenu style={{ textAlign: 'right' }}>
|
2022-02-07 14:58:38 +05:00
|
|
|
{isAdmin ? (
|
2022-06-09 17:51:41 +05:00
|
|
|
<PrivateMenu.Link visible key={'/'} path={'/'} title={'Вернуться на сайт'}/>
|
2022-02-07 14:58:38 +05:00
|
|
|
) : (
|
2022-06-09 17:51:41 +05:00
|
|
|
<PrivateMenu.Link key={'admin'} path={'/admin'} title={'Панель администратора'} content={AdminPanel}/>
|
2022-02-07 14:58:38 +05:00
|
|
|
)}
|
2022-06-09 17:51:41 +05:00
|
|
|
<PrivateMenu.Link visible onClick={onChangePasswordClick} title={'Сменить пароль'} />
|
|
|
|
<PrivateMenu.Link visible path={'/login'} onClick={removeUser} title={'Выход'} />
|
|
|
|
</PrivateMenu>
|
2021-12-02 15:21:10 +05:00
|
|
|
)}
|
|
|
|
>
|
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
|
|
|
})
|