import useGlobalStore from '@/config/use-global'; 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/custom-components/area-code-select'; import { Icon } from '@workspace/ui/custom-components/icon'; import { useTranslations } from 'next-intl'; import { Dispatch, SetStateAction, useRef, useState } from 'react'; import { useForm } from 'react-hook-form'; import { z } from 'zod'; import SendCode from '../send-code'; import CloudFlareTurnstile, { TurnstileRef } from '../turnstile'; export default function LoginForm({ loading, onSubmit, initialValues, setInitialValues, onSwitchForm, }: { loading?: boolean; onSubmit: (data: any) => void; initialValues: any; setInitialValues: Dispatch>; onSwitchForm: Dispatch>; }) { const t = useTranslations('auth.login'); const { common } = useGlobalStore(); const { verify } = common; const formSchema = z.object({ telephone_area_code: z.string(), telephone: z.string(), telephone_code: z.string().optional(), password: z.string().optional(), cf_token: verify.enable_login_verify && verify.turnstile_site_key ? z.string() : z.string().optional(), }); const form = useForm>({ resolver: zodResolver(formSchema), defaultValues: initialValues, }); const [mode, setMode] = useState<'password' | 'code'>('password'); const turnstile = useRef(null); const handleSubmit = form.handleSubmit((data) => { onSubmit(data); turnstile.current?.reset(); }); return ( <>
(
( { if (value.phone) { form.setValue('telephone_area_code', value.phone); } }} /> )} />
)} /> (
{mode === 'code' && ( )}
)} /> {verify.enable_login_verify && ( ( )} /> )}
); }