From 165cab01ce335f0b55b3559df1cb31f8ca35017a Mon Sep 17 00:00:00 2001 From: ariari04 Date: Thu, 26 Sep 2024 19:35:36 +0600 Subject: [PATCH] fix bugs --- .../ChangePassword/ChangePassword.tsx | 181 +++++++++--------- .../ChangePasswordInput.tsx | 55 ++++-- 2 files changed, 121 insertions(+), 115 deletions(-) diff --git a/src/widgets/forms/ProfileForm/ChangePassword/ChangePassword.tsx b/src/widgets/forms/ProfileForm/ChangePassword/ChangePassword.tsx index 0dc7742..c61442b 100644 --- a/src/widgets/forms/ProfileForm/ChangePassword/ChangePassword.tsx +++ b/src/widgets/forms/ProfileForm/ChangePassword/ChangePassword.tsx @@ -1,3 +1,5 @@ +"use client"; + import { useState } from "react"; import { apiInstance } from "@/shared/config/apiConfig"; import { useSession } from "next-auth/react"; @@ -7,7 +9,7 @@ import ChangePasswordInput from "./ChangePasswordInput/ChangePasswordInput"; import { Link } from "@/shared/config/navigation"; import { z } from "zod"; import { zodResolver } from "@hookform/resolvers/zod"; -import { useForm } from "react-hook-form"; +import { FormProvider, useForm } from "react-hook-form"; interface IChangePasswordProps { closeWindow: (bool: boolean) => void; @@ -17,23 +19,15 @@ const ChangePassword: React.FC = ({ closeWindow, }: IChangePasswordProps) => { const session = useSession(); - const [oldPassword, setOldPassword] = useState(""); - const [newPassword, setNewPassword] = useState(""); - const [confirmNewPassword, setConfirmNewPassword] = useState(""); - - const [warningOldPassword, setWarningOldPassword] = useState(""); - const [warningNewPassword, setWarningNewPassword] = useState(""); - const [warningConfirmNewPassword, setWarningConfirmNewPassword] = - useState(""); const [error, setError] = useState(""); const [loader, setLoader] = useState(false); const [success, setSuccess] = useState(false); const changePasswordScheme = z .object({ - old_password: z.string().min(8, "Минимум 8 символов"), - new_password1: z.string().min(8, "Минимум 8 символов"), - new_password2: z.string().min(8, "Минимум 8 символов"), + old_password: z.string().min(8, { message: "Минимум 8 символов" }), + new_password1: z.string().min(8, { message: "Минимум 8 символов" }), + new_password2: z.string().min(8, { message: "Минимум 8 символов" }), }) .refine((data) => data.new_password1 === data.new_password2, { message: "Пароли не совпадают", @@ -41,40 +35,43 @@ const ChangePassword: React.FC = ({ }); type FormFields = z.infer; - const { - register, - handleSubmit, - formState: { errors }, - } = useForm({ + const form = useForm({ resolver: zodResolver(changePasswordScheme), + defaultValues: { + old_password: "", + new_password1: "", + new_password2: "", + }, }); const onSubmit = async (data: FormFields) => { try { - setError(""); - setLoader(true); + console.log(data); + // console.log("cfvgbhnj"); + // setError(""); + // setLoader(true); - const Authorization = `Bearer ${session.data?.access_token}`; - const config = { - headers: { - Authorization, - }, - }; - const res = await apiInstance.patch( - "/auth/password_change/", - data, - config - ); + // const Authorization = `Bearer ${session.data?.access_token}`; + // const config = { + // headers: { + // Authorization, + // }, + // }; + // const res = await apiInstance.patch( + // "/auth/password_change/", + // data, + // config + // ); - if ([200, 201].includes(res.status)) return setSuccess(true); + // if ([200, 201].includes(res.status)) return setSuccess(true); } catch (error: unknown) { - if (error instanceof AxiosError) { - if (error.response?.status === 400) { - setError("Некорректный старый пароль или недопустимый новый пароль"); - } - } else { - setError("Произошла непредвиденная ошибка"); - } + // if (error instanceof AxiosError) { + // if (error.response?.status === 400) { + // setError("Некорректный старый пароль или недопустимый новый пароль"); + // } + // } else { + // setError("Произошла непредвиденная ошибка"); + // } } finally { setLoader(false); } @@ -85,63 +82,59 @@ const ChangePassword: React.FC = ({ onClick={() => closeWindow(false)} className="w-full h-full fixed top-0 left-0 z-10 bg-gray-950 flex items-center justify-center" > -
e.stopPropagation()} - className="flex w-full max-w-[400px] p-6 flex-col gap-4 rounded-md bg-white" - > -
-

- Изменить пароль -

- setOldPassword(e.target.value)} - value={oldPassword} - placeholder="Введите старый пароль" - label="Старый пароль" - error={warningOldPassword} - /> - - Забыли пароль? - - setNewPassword(e.target.value)} - value={newPassword} - placeholder="Введите новый пароль" - label="Новый пароль" - error={warningNewPassword} - /> - setConfirmNewPassword(e.target.value)} - value={confirmNewPassword} - placeholder="Повторите новый пароль" - label="Потвердить новый пароль" - error={warningConfirmNewPassword} - /> - {error ?

{error}

: null} - {success ? ( -

Вы успешно поменяли пароль!

- ) : null} + +
e.stopPropagation()} + className="flex w-full max-w-[400px] p-6 flex-col gap-4 rounded-md bg-white" + > + +

+ Изменить пароль +

+ + + Забыли пароль? + + + + {error ?

{error}

: null} + {success ? ( +

Вы успешно поменяли пароль!

+ ) : null} -
- - -
- -
+
+ + +
+ +
+ ); }; diff --git a/src/widgets/forms/ProfileForm/ChangePassword/ChangePasswordInput/ChangePasswordInput.tsx b/src/widgets/forms/ProfileForm/ChangePassword/ChangePasswordInput/ChangePasswordInput.tsx index 95467d2..78c9644 100644 --- a/src/widgets/forms/ProfileForm/ChangePassword/ChangePasswordInput/ChangePasswordInput.tsx +++ b/src/widgets/forms/ProfileForm/ChangePassword/ChangePasswordInput/ChangePasswordInput.tsx @@ -4,35 +4,48 @@ import Image from "next/image"; import { useState } from "react"; import eye_off from "./icons/eye-off.svg"; import eye_on from "./icons/eye-on.svg"; +import { useFormContext } from "react-hook-form"; +import { cn } from "@/lib/utils"; -interface IChangePasswordInputProps - extends React.InputHTMLAttributes { - label: string; - error: string; +interface Props extends React.InputHTMLAttributes { + name: string; + label?: string; + required?: boolean; + className?: string; } -const ChangePasswordInput: React.FC = ({ - label, - error, - placeholder, +const ChangePasswordInput: React.FC = ({ name, - onChange, - value, -}: IChangePasswordInputProps) => { + label, + required, + className, + ...props +}) => { + const { + register, + formState: { errors }, + } = useFormContext(); + console.log(name); + + const errorText = errors[name]?.message as string; + const [isOpen, setIsOpen] = useState(false); return ( -
- +
+ {label && ( + + )}
@@ -40,9 +53,9 @@ const ChangePasswordInput: React.FC = ({ Eye Icon
- {error ? ( -

{error}

- ) : null} + {errorText && ( +

{errorText}

+ )}
); };