import type { FormEventHandler, JSX } from 'react'; import { memo, useCallback, useState } from 'react'; import { Lang, AllLang } from '@growi/core'; import { LoadingSpinner } from '@growi/ui/dist/components'; import { useTranslation } from 'next-i18next'; import { useRouter } from 'next/router'; import { i18n as i18nConfig } from '^/config/next-i18next.config'; import { apiv3Post } from '~/client/util/apiv3-client'; import { useTWithOpt } from '~/client/util/t-with-opt'; import { toastError } from '~/client/util/toastr'; import type { IErrorV3 } from '~/interfaces/errors/v3-error'; import styles from './InstallerForm.module.scss'; const moduleClass = styles['installer-form'] ?? ''; type Props = { minPasswordLength: number, } const InstallerForm = memo((props: Props): JSX.Element => { const { t, i18n } = useTranslation(); const { minPasswordLength } = props; const router = useRouter(); const tWithOpt = useTWithOpt(); const isSupportedLang = AllLang.includes(i18n.language as Lang); const [isLoading, setIsLoading] = useState(false); const [currentLocale, setCurrentLocale] = useState(isSupportedLang ? i18n.language : Lang.en_US); const [registerErrors, setRegisterErrors] = useState([]); const onClickLanguageItem = useCallback((locale) => { i18n.changeLanguage(locale); setCurrentLocale(locale); }, [i18n]); const submitHandler: FormEventHandler = useCallback(async(e: any) => { e.preventDefault(); setIsLoading(true); const formData = e.target.elements; const { 'registerForm[username]': { value: username }, 'registerForm[name]': { value: name }, 'registerForm[email]': { value: email }, 'registerForm[password]': { value: password }, } = formData; const data = { registerForm: { username, name, email, password, 'app:globalLang': currentLocale, }, }; try { setRegisterErrors([]); await apiv3Post('/installer', data); router.push('/'); } catch (errs) { const err = errs[0]; const code = err.code; setIsLoading(false); setRegisterErrors(errs); if (code === 'failed_to_login_after_install') { toastError(t('installer.failed_to_login_after_install')); setTimeout(() => { router.push('/login') }, 700); // Wait 700 ms to show toastr } toastError(t('installer.failed_to_install')); } }, [currentLocale, router, t]); return (

{ t('installer.create_initial_account') }
{ t('installer.initial_account_will_be_administrator_automatically') }

{ registerErrors != null && registerErrors.length > 0 && (
{registerErrors.map(err => ( {tWithOpt(err.message, err.args)}
))}
) }
language
{ i18nConfig.locales.map((locale) => { let fixedT; if (i18n != null) { fixedT = i18n.getFixedT(locale); i18n.loadLanguages(i18nConfig.locales); } return ( ); }) }
GROWI.org
); }); InstallerForm.displayName = 'InstallerForm'; export default InstallerForm;