import React, { useCallback, useState, type JSX } from 'react'; import { LoadingSpinner } from '@growi/ui/dist/components'; import { useTranslation } from 'next-i18next'; import { useRouter } from 'next/router'; import { useForm } from 'react-hook-form'; import { apiv3Post } from '~/client/util/apiv3-client'; import { useCurrentUser } from '~/states/global'; type InvitedFormProps = { invitedFormUsername: string, invitedFormName: string, } type InvitedFormValues = { name: string, username: string, password: string, }; export const InvitedForm = (props: InvitedFormProps): JSX.Element => { const { t } = useTranslation(); const router = useRouter(); const user = useCurrentUser(); const [loginErrors, setLoginErrors] = useState([]); const [isLoading, setIsLoading] = useState(false); const { invitedFormUsername, invitedFormName } = props; const { register, handleSubmit, formState: { isSubmitting }, } = useForm({ defaultValues: { name: invitedFormName, username: invitedFormUsername, }, }); const submitHandler = useCallback(async(values: InvitedFormValues) => { setIsLoading(true); const invitedForm = { name: values.name, username: values.username, password: values.password, }; try { const res = await apiv3Post('/invited', { invitedForm }); const { redirectTo } = res.data; router.push(redirectTo ?? '/'); } catch (err) { setLoginErrors(err); setIsLoading(false); } }, [router]); const formNotification = useCallback(() => { return ( <> { loginErrors != null && loginErrors.length > 0 ? (

{ loginErrors.map((err) => { return { t(err.message) }
; }) }

) : (

{ t('invited.discription_heading') }

{ t('invited.discription') }

) } ); }, [loginErrors, t]); if (user == null) { return <>; } return (
{ formNotification() }
{/* Email Form */}
mail
{/* UserID Form */}
person
{/* Name Form */}
sell
{/* Password Form */}
lock
{/* Create Button */}
GROWI.ORG
); };