import { zodResolver } from "@hookform/resolvers/zod"; import { Button } from "@workspace/ui/components/button"; import { Form, FormControl, FormField, FormItem, FormMessage, } from "@workspace/ui/components/form"; import { Input } from "@workspace/ui/components/input"; import { AreaCodeSelect } from "@workspace/ui/composed/area-code-select"; import { Icon } from "@workspace/ui/composed/icon"; import type { Dispatch, SetStateAction } from "react"; import { useRef, useState } from "react"; import { useForm } from "react-hook-form"; import { useTranslation } from "react-i18next"; import { z } from "zod"; import { useGlobalStore } from "@/stores/global"; import LocalCaptcha, { type LocalCaptchaRef } from "../local-captcha"; import SendCode from "../send-code"; import SliderCaptcha, { type SliderCaptchaRef } from "../slider-captcha"; import type { TurnstileRef } from "../turnstile"; import CloudFlareTurnstile from "../turnstile"; export default function ResetForm({ loading, onSubmit, initialValues, onSwitchForm, }: { loading?: boolean; onSubmit: (data: any) => void; initialValues: any; setInitialValues: Dispatch>; onSwitchForm: Dispatch>; }) { const { t } = useTranslation("auth"); const { common } = useGlobalStore(); const { verify, auth } = common; const [captchaId, setCaptchaId] = useState(""); const isTurnstile = verify.captcha_type === "turnstile"; const isLocal = verify.captcha_type === "local"; const isSlider = verify.captcha_type === "slider"; const captchaEnabled = verify.enable_user_reset_password_captcha; const formSchema = z.object({ telephone_area_code: z.string(), telephone: z.string(), password: z.string(), code: auth?.email?.enable_verify ? z.string() : z.string().nullish(), cf_token: captchaEnabled && isTurnstile && verify.turnstile_site_key ? z.string() : z.string().nullish(), captcha_code: captchaEnabled && isLocal ? z.string().min(1, t("captcha.required", "Please enter captcha code")) : z.string().nullish(), slider_token: captchaEnabled && isSlider ? z .string() .min(1, t("captcha.sliderRequired", "Please complete the slider")) : z.string().optional(), }); const form = useForm>({ resolver: zodResolver(formSchema), defaultValues: { cf_token: "", captcha_code: "", slider_token: "", ...initialValues, }, }); const turnstile = useRef(null); const localCaptcha = useRef(null); const sliderCaptcha = useRef(null); const handleSubmit = form.handleSubmit((data) => { try { // Add captcha_id for local captcha if (isLocal && captchaEnabled) { (data as any).captcha_id = captchaId; } onSubmit(data); } catch (_error) { turnstile.current?.reset(); localCaptcha.current?.reset(); sliderCaptcha.current?.reset(); } }); return ( <>
(
( { if (value.phone) { form.setValue( "telephone_area_code", value.phone ); } }} placeholder={t( "register.areaCodePlaceholder", "Area code..." )} simple value={field.value} /> )} />
)} /> (
)} /> ( )} /> {captchaEnabled && isTurnstile && ( ( )} /> )} {captchaEnabled && isLocal && ( ( )} /> )} {captchaEnabled && isSlider && ( ( )} /> )}
{t("reset.existingAccount", "Remember your password?")} 
); }