import React, { useCallback, useState } from 'react'; import { useTranslation } from 'next-i18next'; import { useRouter } from 'next/router'; import { apiv3Post } from '~/client/util/apiv3-client'; import { useCurrentUser } from '../stores/context'; export type InvitedFormProps = { invitedFormUsername: string, invitedFormName: string, } export const InvitedForm = (props: InvitedFormProps): JSX.Element => { const { t } = useTranslation(); const router = useRouter(); const { data: user } = useCurrentUser(); const [loginErrors, setLoginErrors] = useState([]); const [isLoading, setIsLoading] = useState(false); const { invitedFormUsername, invitedFormName } = props; const submitHandler = useCallback(async(e) => { e.preventDefault(); setIsLoading(true); const formData = e.target.elements; const { 'invitedForm[name]': { value: name }, 'invitedForm[password]': { value: password }, 'invitedForm[username]': { value: username }, } = formData; const invitedForm = { name, password, username, }; 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 */}
{/* UserID Form */}
{/* Name Form */}
{/* Password Form */}
{/* Create Button */}
GROWI.ORG
); };