From cc835d523ceb5a8e7399dcbf11ebf7b7019f9a05 Mon Sep 17 00:00:00 2001 From: TinsFox Date: Thu, 9 May 2024 15:38:09 +0800 Subject: [PATCH] refactor: install form (#4154) --- web/app/install/installForm.tsx | 115 ++++++++++++++------------------ web/i18n/en-US/login.ts | 1 + web/package.json | 3 + web/yarn.lock | 46 +++++-------- 4 files changed, 71 insertions(+), 94 deletions(-) diff --git a/web/app/install/installForm.tsx b/web/app/install/installForm.tsx index 631812613e..414bd9d6fd 100644 --- a/web/app/install/installForm.tsx +++ b/web/app/install/installForm.tsx @@ -1,73 +1,67 @@ 'use client' import React, { useEffect } from 'react' import { useTranslation } from 'react-i18next' + import Link from 'next/link' import { useRouter } from 'next/navigation' -// import { useContext } from 'use-context-selector' -import Toast from '../components/base/toast' + +import type { SubmitHandler } from 'react-hook-form' +import { useForm } from 'react-hook-form' +import { z } from 'zod' +import { zodResolver } from '@hookform/resolvers/zod' +import classNames from 'classnames' import Loading from '../components/base/loading' import Button from '@/app/components/base/button' -// import I18n from '@/context/i18n' import { fetchInitValidateStatus, fetchSetupStatus, setup } from '@/service/common' import type { InitValidateStatusResponse, SetupStatusResponse } from '@/models/common' -const validEmailReg = /^[\w\.-]+@([\w-]+\.)+[\w-]{2,}$/ 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 [email, setEmail] = React.useState('') - const [name, setName] = React.useState('') - const [password, setPassword] = React.useState('') 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 showErrorMessage = (message: string) => { - Toast.notify({ - type: 'error', - message, - }) - } - const valid = () => { - if (!email) { - showErrorMessage(t('login.error.emailEmpty')) - return false - } - if (!validEmailReg.test(email)) { - showErrorMessage(t('login.error.emailInValid')) - return false - } - if (!name.trim()) { - showErrorMessage(t('login.error.nameEmpty')) - return false - } - if (!password.trim()) { - showErrorMessage(t('login.error.passwordEmpty')) - return false - } - if (!validPassword.test(password)) { - showErrorMessage(t('login.error.passwordInvalid')) - return false - } - - return true - } - const handleSetting = async () => { - if (!valid()) - return + const onSubmit: SubmitHandler = async (data) => { await setup({ body: { - email, - name, - password, + ...data, }, }) router.push('/signin') } + const handleSetting = async () => { + handleSubmit(onSubmit)() + } + useEffect(() => { fetchSetupStatus().then((res: SetupStatusResponse) => { if (res.step === 'finished') { @@ -93,24 +87,22 @@ const InstallForm = () => { mt-1 text-sm text-gray-600 '>{t('login.setAdminAccountDesc')}