import React, { useEffect, useState } from 'react'; import { Transition } from '@headlessui/react'; import { ArrowPathIcon, ArrowTopRightOnSquareIcon, } from '@heroicons/react/20/solid'; import { Options } from '../../interfaces'; type Props = { registrationStep: string; registrationIsVisible: boolean; setRegistrationIsVisible: ( visible: boolean ) => void; onStep: ( requestedStep: string ) => void; onMaybeLater?: () => void; registrationName: string; registrationEmail: string; setRegistrationName: ( name: string ) => void; setRegistrationEmail: ( email: string ) => void; commAdministrative: boolean; commNews: boolean; commDeveloper: boolean; setCommAdministrative: ( enabled: boolean ) => void; setCommNews: ( enabled: boolean ) => void; setCommDeveloper: ( enabled: boolean ) => void; options: Options; error?: string; }; export const Registration: React.FC< Props > = ( { registrationStep, setRegistrationIsVisible, onStep, onMaybeLater, registrationName, setRegistrationName, registrationEmail, setRegistrationEmail, commAdministrative, commNews, commDeveloper, setCommAdministrative, setCommNews, setCommDeveloper, options, error, }: Props ) => { const [ emailError, setEmailError ] = useState< boolean >( false ); const [ nameError, setNameError ] = useState< boolean >( false ); const [ errorTime, setErrorTime ] = useState< number >( -1 ); const handleRegisterClick = ( e: React.MouseEvent< HTMLButtonElement > ) => { e.preventDefault(); setEmailError( false ); setNameError( false ); const nameRegex = /^[A-Za-zÀ-ÖØ-öø-ÿ0-9\s]+$/; if ( ! nameRegex.test( registrationName ) ) { setNameError( true ); return; } const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; if ( ! emailRegex.test( registrationEmail ) ) { setEmailError( true ); return; } onStep( 'pin' ); // Proceed to the next step }; useEffect( () => { // if there is a register error, count down a timer to when user can request to register again if ( error == 'register-error' && errorTime == -1 ) { // use local var let time = 9; setErrorTime( time ); const interval = setInterval( () => { time -= 1; // set error time to local var setErrorTime( time ); if ( time < 0 ) { // clear once finished clearInterval( interval ); } }, 1000 ); } } ); return (
); };