'use client' import React, { useEffect } from 'react' import { useTranslation } from 'react-i18next' import Link from 'next/link' import { useRouter } from 'next/navigation' import type { SubmitHandler } from 'react-hook-form' import { useForm } from 'react-hook-form' import { z } from 'zod' import { zodResolver } from '@hookform/resolvers/zod' import Loading from '../components/base/loading' import classNames from '@/utils/classnames' import Button from '@/app/components/base/button' import { fetchInitValidateStatus, fetchSetupStatus, setup } from '@/service/common' import type { InitValidateStatusResponse, SetupStatusResponse } from '@/models/common' const validPassword = /^(?=.*[a-zA-Z])(?=.*\d).{8,}$/ const accountFormSchema = z.object({ email: z .string() .min(1, { message: 'login.error.emailInValid' }) .email('login.error.emailInValid'), name: z.string().min(1, { message: 'login.error.nameEmpty' }), password: z.string().min(8, { message: 'login.error.passwordLengthInValid', }).regex(validPassword, 'login.error.passwordInvalid'), }) type AccountFormValues = z.infer const InstallForm = () => { const { t } = useTranslation() const router = useRouter() const [showPassword, setShowPassword] = React.useState(false) const [loading, setLoading] = React.useState(true) const { register, handleSubmit, formState: { errors }, } = useForm({ resolver: zodResolver(accountFormSchema), defaultValues: { name: '', password: '', email: '', }, }) const onSubmit: SubmitHandler = async (data) => { await setup({ body: { ...data, }, }) router.push('/signin') } const handleSetting = async () => { handleSubmit(onSubmit)() } useEffect(() => { fetchSetupStatus().then((res: SetupStatusResponse) => { if (res.step === 'finished') { localStorage.setItem('setup_status', 'finished') window.location.href = '/signin' } else { fetchInitValidateStatus().then((res: InitValidateStatusResponse) => { if (res.status === 'not_started') window.location.href = '/init' }) } setLoading(false) }) }, []) return ( loading ? : <>

{t('login.setAdminAccount')}

{t('login.setAdminAccountDesc')}

{errors.email && {t(`${errors.email?.message}`)}}
{errors.name && {t(`${errors.name.message}`)}}
{t('login.error.passwordInvalid')}
{t('login.license.tip')}   {t('login.license.link')}
) } export default InstallForm