Добавлено поле подтверждение пароля в окне смены

Добавлена информация о пользователе в окне смены
This commit is contained in:
Александр Сироткин 2021-12-28 19:19:12 +05:00
parent 19afed9c4f
commit 5007398e6e
3 changed files with 38 additions and 14 deletions

View File

@ -2,23 +2,25 @@ import { memo, useState } from 'react'
import { useForm } from 'antd/lib/form/Form' import { useForm } from 'antd/lib/form/Form'
import { Form, Input, Modal, FormProps } from 'antd' import { Form, Input, Modal, FormProps } from 'antd'
import { AuthService } from '../services/api' import { AuthService, UserDto } from '../services/api'
import { passwordRules } from '../utils/validationRules' import { passwordRules, createPasswordRules } from '../utils/validationRules'
import LoaderPortal from './LoaderPortal' import LoaderPortal from './LoaderPortal'
import { invokeWebApiWrapperAsync } from './factory' import { invokeWebApiWrapperAsync } from './factory'
import { UserView } from './Views'
const formLayout: FormProps = { labelCol: { span: 11 }, wrapperCol: { span: 16 } } const formLayout: FormProps = { labelCol: { span: 11 }, wrapperCol: { span: 16 } }
export type ChangePasswordProps = { export type ChangePasswordProps = {
userId?: number user?: UserDto
visible?: boolean visible?: boolean
onCancel?: () => void onCancel?: () => void
onOk?: () => void onOk?: () => void
} }
const fieldRules = [...passwordRules, ...createPasswordRules]
export const ChangePassword = memo<ChangePasswordProps>(({ userId, visible, onCancel, onOk }) => { export const ChangePassword = memo<ChangePasswordProps>(({ user, visible, onCancel, onOk }) => {
const [showLoader, setShowLoader] = useState<boolean>(false) const [showLoader, setShowLoader] = useState<boolean>(false)
const [password, setPassword] = useState<string>('') const [password, setPassword] = useState<string>('')
@ -31,7 +33,7 @@ export const ChangePassword = memo<ChangePasswordProps>(({ userId, visible, onCa
const onFormFinish = () => invokeWebApiWrapperAsync( const onFormFinish = () => invokeWebApiWrapperAsync(
async() => { async() => {
await AuthService.changePassword(userId ?? localStorage['userId'], `"${password}"`) await AuthService.changePassword(user?.id ?? localStorage['userId'], `"${password}"`)
onOk?.() onOk?.()
}, },
setShowLoader, setShowLoader,
@ -41,7 +43,12 @@ export const ChangePassword = memo<ChangePasswordProps>(({ userId, visible, onCa
return ( return (
<Modal <Modal
centered centered
title={'Сменить пароль'} title={(
<>
Сменить пароль
{user && <>&nbsp;(<UserView user={user} />)</>}
</>
)}
visible={visible} visible={visible}
onCancel={onModalCancel} onCancel={onModalCancel}
onOk={() => form.submit()} onOk={() => form.submit()}
@ -53,9 +60,26 @@ export const ChangePassword = memo<ChangePasswordProps>(({ userId, visible, onCa
name={'change-password'} name={'change-password'}
onFinish={onFormFinish} onFinish={onFormFinish}
> >
<Form.Item label={'Новый пароль'} name={'new-password'} rules={passwordRules}> <Form.Item
label={'Новый пароль'}
name={'new-password'}
required={true}
rules={fieldRules}
>
<Input.Password onChange={(e) => setPassword(e.target.value)} value={password} /> <Input.Password onChange={(e) => setPassword(e.target.value)} value={password} />
</Form.Item> </Form.Item>
<Form.Item
label={'Подтверждение пароля'}
name={'confirm-password'}
required={true}
rules={[() => ({ validator(_, value: string) {
if (value !== password)
return Promise.reject('Пароли не совпадают!')
return Promise.resolve()
}})]}
>
<Input.Password />
</Form.Item>
</Form> </Form>
</LoaderPortal> </LoaderPortal>
</Modal> </Modal>

View File

@ -41,5 +41,5 @@ export const TelemetryView = memo<TelemetryViewProps>(({ telemetry }) => telemet
{getTelemetryLabel(telemetry)} {getTelemetryLabel(telemetry)}
</Tooltip> </Tooltip>
) : ( ) : (
<Tooltip title={'нет данных'}>{getTelemetryLabel()}</Tooltip> <Tooltip title={'нет данных'}>-</Tooltip>
)) ))

View File

@ -47,12 +47,12 @@ export default function UserController() {
const [users, setUsers] = useState([]) const [users, setUsers] = useState([])
const [showLoader, setShowLoader] = useState(false) const [showLoader, setShowLoader] = useState(false)
const [columns, setColumns] = useState([]) const [columns, setColumns] = useState([])
const [selectedId, setSelectedId] = useState(null) const [selectedUser, setSelectedUser] = useState(null)
const additionalButtons = (record, editingKey) => ( const additionalButtons = (record, editingKey) => (
<Button <Button
icon={<UserSwitchOutlined />} icon={<UserSwitchOutlined />}
onClick={() => setSelectedId(record.id)} onClick={() => setSelectedUser(record)}
title={'Сменить пароль'} title={'Сменить пароль'}
disabled={editingKey !== ''} disabled={editingKey !== ''}
/> />
@ -168,10 +168,10 @@ export default function UserController() {
/> />
</LoaderPortal> </LoaderPortal>
<ChangePassword <ChangePassword
userId={selectedId} user={selectedUser}
visible={selectedId > 0} visible={selectedUser}
onCancel={() => setSelectedId(null)} onCancel={() => setSelectedUser(null)}
onOk={() => setSelectedId(null)} onOk={() => setSelectedUser(null)}
/> />
</> </>
) )