|
| 1 | +import React from 'react'; |
| 2 | +import { useForm } from 'react-hook-form'; |
| 3 | +import { zodResolver } from '@hookform/resolvers/zod'; |
| 4 | + |
| 5 | +import { |
| 6 | + ChangeProfileSchema, |
| 7 | + ChangeProfileSchemaType, |
| 8 | + ChangePasswordSchema, |
| 9 | + ChangePasswordSchemaType, |
| 10 | +} from './profile-schema'; |
| 11 | +import { RegularForm } from '../../shared/forms/RegularForm/RegularForm'; |
| 12 | +import { FormInputField } from '../../shared/forms/FormInputField/FormInputField'; |
| 13 | +import { FormTextareaField } from '../../shared/forms/FormTextareaField/FormTextareaField'; |
| 14 | +import { Button } from '../../shared/button/Button'; |
| 15 | + |
| 16 | +import s from './ProfileForm.module.scss'; |
| 17 | + |
| 18 | +const ChangeProfileForm = () => { |
| 19 | + const { |
| 20 | + register, |
| 21 | + handleSubmit, |
| 22 | + formState: { errors }, |
| 23 | + } = useForm<ChangeProfileSchemaType>({ |
| 24 | + shouldUnregister: true, |
| 25 | + resolver: zodResolver(ChangeProfileSchema), |
| 26 | + }); |
| 27 | + |
| 28 | + const onSubmit = (data: ChangeProfileSchemaType) => { |
| 29 | + console.log(data); |
| 30 | + }; |
| 31 | + |
| 32 | + return ( |
| 33 | + <RegularForm title="Изменить профиль" onSubmit={handleSubmit(onSubmit)}> |
| 34 | + <FormInputField name="name" register={register} type="text" errors={errors.name}> |
| 35 | + Псевдоним |
| 36 | + </FormInputField> |
| 37 | + <FormTextareaField name="description" register={register} errors={errors.description}> |
| 38 | + О себе |
| 39 | + </FormTextareaField> |
| 40 | + <Button type="submit">Сохранить</Button> |
| 41 | + </RegularForm> |
| 42 | + ); |
| 43 | +}; |
| 44 | + |
| 45 | +const ChangePasswordForm = () => { |
| 46 | + const { |
| 47 | + register, |
| 48 | + handleSubmit, |
| 49 | + formState: { errors }, |
| 50 | + } = useForm<ChangePasswordSchemaType>({ |
| 51 | + shouldUnregister: true, |
| 52 | + resolver: zodResolver(ChangePasswordSchema), |
| 53 | + }); |
| 54 | + |
| 55 | + const onSubmit = (data: ChangePasswordSchemaType) => { |
| 56 | + console.log(data); |
| 57 | + }; |
| 58 | + |
| 59 | + return ( |
| 60 | + <RegularForm title="Изменить пароль" onSubmit={handleSubmit(onSubmit)}> |
| 61 | + <FormInputField name="password" register={register} type="password" errors={errors.password}> |
| 62 | + Пароль |
| 63 | + </FormInputField> |
| 64 | + <FormInputField name="newPassword" register={register} type="password" errors={errors.newPassword}> |
| 65 | + Новый пароль |
| 66 | + </FormInputField> |
| 67 | + <FormInputField name="confirmPassword" register={register} type="password" errors={errors.confirmPassword}> |
| 68 | + Повторите пароль |
| 69 | + </FormInputField> |
| 70 | + <Button type="submit">Изменить</Button> |
| 71 | + </RegularForm> |
| 72 | + ); |
| 73 | +}; |
| 74 | + |
| 75 | +export const ProfileForm = () => ( |
| 76 | + <div className={s.container}> |
| 77 | + <ChangeProfileForm /> |
| 78 | + <ChangePasswordForm /> |
| 79 | + </div> |
| 80 | +); |
0 commit comments