'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 Loading from '../components/base/loading'
import Button from '@/app/components/base/button'
import I18n from '@/context/i18n'
import { fetchSetupStatus, setup } from '@/service/common'
import type { SetupStatusResponse } from '@/models/common'
const validEmailReg = /^[\w\.-]+@([\w-]+\.)+[\w-]{2,}$/
const validPassword = /^(?=.*[a-zA-Z])(?=.*\d).{8,}$/
const InstallForm = () => {
const { t } = useTranslation()
const { locale } = useContext(I18n)
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 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 true
}
const handleSetting = async () => {
if (!valid())
return
await setup({
body: {
email,
name,
password,
},
})
router.push('/signin')
}
useEffect(() => {
fetchSetupStatus().then((res: SetupStatusResponse) => {
if (res.step === 'finished')
window.location.href = '/signin'
else
setLoading(false)
})
}, [])
return (
loading
?
{t('login.setAdminAccountDesc')}