Исправлена работа формы смены пароля

This commit is contained in:
goodmice 2022-05-19 01:28:06 +05:00
parent 12a6d96a95
commit 43e97750e4

View File

@ -1,5 +1,5 @@
import { memo, useState } from 'react' import { memo, useCallback, useMemo, useState } from 'react'
import { useForm } from 'antd/lib/form/Form' import { Rule } from 'antd/lib/form'
import { Form, Input, Modal, FormProps } from 'antd' import { Form, Input, Modal, FormProps } from 'antd'
import { AuthService, UserDto } from '@api' import { AuthService, UserDto } from '@api'
@ -21,12 +21,19 @@ export type ChangePasswordProps = {
const fieldRules = [...passwordRules, ...createPasswordRules] const fieldRules = [...passwordRules, ...createPasswordRules]
const confirmPasswordRules: Rule[] = [({ getFieldValue }) => ({ validator(_, value: string) {
if (value !== getFieldValue('new-password'))
return Promise.reject('Пароли не совпадают!')
return Promise.resolve()
}})]
export const ChangePassword = memo<ChangePasswordProps>(({ user, 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 [isDisabled, setIsDisabled] = useState(true) const [isDisabled, setIsDisabled] = useState(true)
const [form] = useForm() const userData = useMemo(() => user ?? { id: getUserId(), login: getUserLogin() } as UserDto, [user])
const [form] = Form.useForm()
const onFormChange = async () => await form.validateFields() const onFormChange = async () => await form.validateFields()
.then(() => setIsDisabled(false)) .then(() => setIsDisabled(false))
@ -37,15 +44,15 @@ export const ChangePassword = memo<ChangePasswordProps>(({ user, visible, onCanc
onCancel?.() onCancel?.()
} }
const onFormFinish = () => invokeWebApiWrapperAsync( const onFormFinish = useCallback((values: any) => invokeWebApiWrapperAsync(
async() => { async() => {
await AuthService.changePassword(user?.id ?? getUserId() ?? -1, `"${password}"`) await AuthService.changePassword(userData.id ?? -1, `${values['new-password']}`)
onOk?.() onOk?.()
}, },
setShowLoader, setShowLoader,
`Не удалось сменить пароль пользователя ${getUserLogin()}`, `Не удалось сменить пароль пользователя ${userData.login}`,
'Смена пароля пользователя' 'Смена пароля пользователя'
) ), [userData, onOk])
return ( return (
<Modal <Modal
@ -63,6 +70,8 @@ export const ChangePassword = memo<ChangePasswordProps>(({ user, visible, onCanc
okButtonProps={{ okButtonProps={{
disabled: isDisabled disabled: isDisabled
}} }}
// getContainer={false} // Исправляет ошибку с формой, но портит вид модалки при вызове из user menu
// TODO: разобраться
> >
<LoaderPortal show={showLoader}> <LoaderPortal show={showLoader}>
<Form <Form
@ -72,24 +81,10 @@ export const ChangePassword = memo<ChangePasswordProps>(({ user, visible, onCanc
onFinish={onFormFinish} onFinish={onFormFinish}
onChange={onFormChange} onChange={onFormChange}
> >
<Form.Item <Form.Item required rules={fieldRules} name={'new-password'} label={'Новый пароль'}>
label={'Новый пароль'} <Input.Password />
name={'new-password'}
required={true}
rules={fieldRules}
>
<Input.Password onChange={(e) => setPassword(e.target.value)} value={password} />
</Form.Item> </Form.Item>
<Form.Item <Form.Item required name={'confirm-password'} rules={confirmPasswordRules} label={'Подтверждение пароля'}>
label={'Подтверждение пароля'}
name={'confirm-password'}
required={true}
rules={[() => ({ validator(_, value: string) {
if (value !== password)
return Promise.reject('Пароли не совпадают!')
return Promise.resolve()
}})]}
>
<Input.Password /> <Input.Password />
</Form.Item> </Form.Item>
</Form> </Form>