import { postJSON } from '@/infrastructure/fetch-json' import useWaitForI18n from '@/shared/hooks/use-wait-for-i18n' import Notification from '@/shared/components/notification' import getMeta from '@/utils/meta' import { FormEvent, useState } from 'react' import { Button } from 'react-bootstrap' import { Trans, useTranslation } from 'react-i18next' import LoadingSpinner from '@/shared/components/loading-spinner' import MaterialIcon from '@/shared/components/material-icon' import { sendMB } from '@/infrastructure/event-tracking' import { Interstitial } from '@/shared/components/interstitial' type Feedback = { type: 'input' | 'alert' style: 'error' | 'info' message: string } type ConfirmEmailFormProps = { confirmationEndpoint: string flow: string resendEndpoint: string successMessage: React.ReactNode successButtonText: string } export function ConfirmEmailForm({ confirmationEndpoint, flow, resendEndpoint, successMessage, successButtonText, }: ConfirmEmailFormProps) { const { t } = useTranslation() const [confirmationCode, setConfirmationCode] = useState('') const [feedback, setFeedback] = useState(null) const [isConfirming, setIsConfirming] = useState(false) const [isResending, setIsResending] = useState(false) const [successRedirectPath, setSuccessRedirectPath] = useState('') const email = getMeta('ol-email') const { isReady } = useWaitForI18n() const errorHandler = (err: any, actionType?: string) => { let errorName = err?.data?.message?.key || 'generic_something_went_wrong' if (err?.response?.status === 429) { if (actionType === 'confirm') { errorName = 'too_many_confirm_code_verification_attempts' } else if (actionType === 'resend') { errorName = 'too_many_confirm_code_resend_attempts' } setFeedback({ type: 'alert', style: 'error', message: errorName, }) } else { setFeedback({ type: 'input', style: 'error', message: errorName, }) } sendMB('email-verification-error', { errorName, flow, }) } const invalidFormHandler = () => { if (!confirmationCode) { return setFeedback({ type: 'input', style: 'error', message: 'please_enter_confirmation_code', }) } } const submitHandler = (e: FormEvent) => { e.preventDefault() setIsConfirming(true) setFeedback(null) postJSON(confirmationEndpoint, { body: { code: confirmationCode, }, }) .then(data => { setSuccessRedirectPath(data?.redir || '/') }) .catch(err => { errorHandler(err, 'confirm') }) .finally(() => { setIsConfirming(false) }) sendMB('email-verification-click', { button: 'verify', flow, }) } const resendHandler = (e: FormEvent ) } function ConfirmEmailSuccessfullForm({ successMessage, successButtonText, redirectTo, }: { successMessage: React.ReactNode successButtonText: string redirectTo: string }) { const submitHandler = (e: FormEvent) => { e.preventDefault() location.assign(redirectTo) } return (
{successMessage}
) } function ErrorMessage({ error }: { error: string }) { const { t } = useTranslation() switch (error) { case 'invalid_confirmation_code': return {t('invalid_confirmation_code')} case 'expired_confirmation_code': return ( ]} /> ) case 'email_already_registered': return {t('email_already_registered')} case 'too_many_confirm_code_resend_attempts': return {t('too_many_confirm_code_resend_attempts')} case 'too_many_confirm_code_verification_attempts': return {t('too_many_confirm_code_verification_attempts')} case 'we_sent_new_code': return {t('we_sent_new_code')} case 'please_enter_confirmation_code': return {t('please_enter_confirmation_code')} default: return {t('generic_something_went_wrong')} } }