import useGlobalStore from '@/config/use-global'; import { sendEmailCode } from '@/services/common/common'; import { zodResolver } from '@hookform/resolvers/zod'; import { Icon } from '@iconify/react'; 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 { Markdown } from '@workspace/ui/custom-components/markdown'; import { useCountDown } from 'ahooks'; import { useTranslations } from 'next-intl'; import { Dispatch, SetStateAction, useState } from 'react'; import { useForm } from 'react-hook-form'; import { z } from 'zod'; import CloudFlareTurnstile from './turnstile'; export default function UserRegisterForm({ loading, onSubmit, initialValues, setInitialValues, onSwitchForm, }: { loading?: boolean; onSubmit: (data: any) => void; initialValues: any; setInitialValues: Dispatch>; onSwitchForm: (type?: 'register' | 'reset') => void; }) { const t = useTranslations('auth.register'); const { common } = useGlobalStore(); const { verify, register, invite } = common; const [targetDate, setTargetDate] = useState(); const [, { seconds }] = useCountDown({ targetDate, onEnd: () => { setTargetDate(undefined); }, }); const handleSendCode = async () => { await sendEmailCode({ email: initialValues.email, type: 1, }); setTargetDate(Date.now() + 60000); }; const formSchema = z .object({ email: z.string(), password: z.string(), repeat_password: z.string(), code: register.enable_email_verify ? z.string() : z.string().nullish(), invite: invite.forced_invite ? z.string() : z.string().nullish(), cf_token: verify.enable_register_verify && verify.turnstile_site_key ? z.string() : z.string().nullish(), }) .superRefine(({ password, repeat_password }, ctx) => { if (password !== repeat_password) { ctx.addIssue({ code: z.ZodIssueCode.custom, message: t('passwordMismatch'), path: ['repeat_password'], }); } }); const form = useForm>({ resolver: zodResolver(formSchema), defaultValues: { ...initialValues, invite: sessionStorage.getItem('invite'), }, }); return ( <> {register.stop_register ? ( {t('message')} ) : (
( )} /> ( )} /> ( )} /> {register.enable_email_verify && ( (
)} /> )} ( )} /> {verify.enable_register_verify && ( ( )} /> )} )}
{t('existingAccount')}
); }