import React, { useEffect, useState } from 'react'; import { LoginComponentProps } from './types'; import { getTranslations } from '../../shared/utils/localization-util'; import { confirmMember } from './login-services'; interface TouchedFields { password?: boolean; repeatPassword?: boolean; } interface FormFields { password: string; repeatPassword: string; } const ConfirmComponent: React.FC = ({ tideClientConfig, languageCode, handleBackToLogin }) => { const [isConfirmed, setIsConfirmed] = useState(false); const [touched, setTouched] = useState(); const [formValues, setFormValues] = useState({ password: '', repeatPassword: '' }); const [errors, setErrors] = useState({} as any); const [token, setToken] = useState(null); const translations = getTranslations(languageCode ?? 'en-GB'); useEffect(() => { if (typeof window !== 'undefined') { setToken(new URLSearchParams(window.location.search).get('token')); } }, []); const handleBlur = (event: React.ChangeEvent) => { if (!touched) { setTouched({ password: false, repeatPassword: false }); } setTouched({ ...touched, [event.target.name]: true }); validate(); }; const onChange = (event: React.ChangeEvent) => { setFormValues({ ...formValues, [event.target.name]: event.target.value }); validate(); }; const validate = () => { let validationErrors = {} as any; if (formValues) { if (!formValues.password || formValues.password.length === 0) { validationErrors.password = true; } if (!formValues.repeatPassword || formValues.repeatPassword.length === 0) { validationErrors.repeatPassword = true; } else if (formValues.password !== formValues.repeatPassword) { validationErrors.passwordMatch = true; } if (!token || token.length === 0) { validationErrors.token = true; } } setErrors(validationErrors); return Object.keys(validationErrors).length === 0; }; const handleConfirmation = async () => { setTouched({ password: true, repeatPassword: true }); const isValid = validate(); if (!isValid) return; if (token) { try { const response = await confirmMember(token, formValues.password, true, tideClientConfig); if (response.ok) { setIsConfirmed(true); } } catch { // Optional: handle unexpected API issues } } }; useEffect(() => { validate(); }, [formValues, token]); return ( <> {!isConfirmed && ( <>

{translations.LOGIN.RESET_PASSWORD_TITLE}

e.preventDefault()} noValidate>
{(touched?.password || touched?.repeatPassword) && errors.password && (
{translations.LOGIN.PASSWORD_REQUIRED}
)}
{(touched?.repeatPassword || touched?.password) && (errors.repeatPassword || errors.passwordMatch) && (
{errors.repeatPassword === 'required' && translations.LOGIN.REPEAT_PASSWORD_REQUIRED} {errors.passwordMatch === 'mismatch' && translations.LOGIN.PASSWORDS_DO_NOT_MATCH}
)}
)} {isConfirmed && (

{translations.LOGIN.RECEIVED_REQUEST}

{translations.LOGIN.ACCOUNT_ACTIVATED_LOGIN}

)} ); }; export default ConfirmComponent;