asb_cloud_front/src/components/UserMenu.tsx

57 lines
2.2 KiB
TypeScript
Raw Normal View History

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