asb_cloud_front/src/components/UserMenu.tsx
goodm2ice 0e24780a91 * Дополнены названия полей TelemetryView
* Вынесен метод получения лейбла для TelemetryInfo
* Сменён тип смешивания проверки по правам и ролям
* WellIconState экспортирован из главного файла модуля components/icons
2021-12-21 15:37:23 +05:00

97 lines
3.5 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { MouseEventHandler, useState } from 'react'
import { Link, useHistory } from 'react-router-dom'
import { Button, Dropdown, Menu, Modal, Form, Input, FormProps } from 'antd'
import { useForm } from 'antd/lib/form/Form'
import { UserOutlined } from '@ant-design/icons'
import { AuthService } from '../services/api'
import { passwordRules } from '../utils/validationRules'
import { invokeWebApiWrapperAsync } from './factory'
import { PrivateMenuItem } from './Private'
import LoaderPortal from './LoaderPortal'
const handleLogout = () => {
localStorage.removeItem('login')
localStorage.removeItem('token')
}
const formLayout: FormProps = { labelCol: { span: 11 }, wrapperCol: { span: 16 } }
type UserMenuProps = {
isAdmin?: boolean
}
export const UserMenu: React.FC<UserMenuProps> = ({ isAdmin }) => {
const [form] = useForm()
const [isModalVisible, setIsModalVisible] = useState<boolean>(false)
const [showLoader, setShowLoader] = useState<boolean>(false)
const [password, setPassword] = useState<string>('')
const history = useHistory()
const changePassword = () => invokeWebApiWrapperAsync(
async() => {
await AuthService.changePassword(localStorage['userId'], `"${password}"`)
history.push('/login')
},
setShowLoader,
`Не удалось сменить пароль пользователя ${localStorage['login']}`
)
const onFormCancel = () => {
form.resetFields()
setIsModalVisible(false)
}
const onChangePasswordClick: MouseEventHandler = (e) => {
setIsModalVisible(true)
e.preventDefault()
}
return (
<>
<Dropdown
placement={'bottomRight'}
overlay={(
<Menu style={{ textAlign: 'right' }}>
<PrivateMenuItem roles={'admin'}>
{isAdmin ? (
<Link to={'/'}>Вернуться на сайт</Link>
) : (
<Link to={'/admin'}>Панель администратора</Link>
)}
</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>
<Modal
title={'Сменить пароль'}
centered
visible={isModalVisible}
onCancel={onFormCancel}
onOk={() => form.submit()}
>
<LoaderPortal show={showLoader}>
<Form
{...formLayout}
form={form}
name={'change-password'}
onFinish={changePassword}
>
<Form.Item label={'Новый пароль'} name={'new-password'} rules={passwordRules}>
<Input.Password onChange={(e) => setPassword(e.target.value)} value={password} />
</Form.Item>
</Form>
</LoaderPortal>
</Modal>
</>
)
}