import React, {Dispatch, SetStateAction, useContext, useState} from "react"; import {AlertContext} from "@/context/AlertContext"; import {Mail, Lock, PenLine, User} from 'lucide-react'; import {useTranslations} from "@/lib/i18n"; import {LangContext, LangContextProps} from "@/context/LangContext"; import {verifyInput} from "@/lib/utils/validation"; import {apiPostPublic} from "@/lib/api/client"; import Button from "@/components/ui/Button"; import InputField from "@/components/form/InputField"; import TextInput from "@/components/form/TextInput"; export default function StepOne( { username, email, setUsername, setEmail, handleNextStep }: { username: string; email: string; setUsername: Dispatch>, setEmail: Dispatch>, handleNextStep: Function; }) { const {errorMessage, successMessage} = useContext(AlertContext); const [firstName, setFirstName] = useState(''); const [lastName, setLastName] = useState(''); const [password, setPassword] = useState(''); const [repeatPassword, setRepeatPassword] = useState(''); const [userId, setUserId] = useState('') const t = useTranslations(); const {lang} = useContext(LangContext) async function handleStep(): Promise { if (!firstName || !lastName || !username || !password || !repeatPassword || !email) { errorMessage(t('registerStepOne.error.requiredFields')); return; } if (firstName.length < 2 || firstName.length > 50) { errorMessage(t('registerStepOne.error.firstNameLength')); return; } if (lastName.length < 2 || lastName.length > 50) { errorMessage(t('registerStepOne.error.lastNameLength')); return; } if (username.length < 3 || username.length > 50) { errorMessage(t('registerStepOne.error.usernameLength')); return; } if (verifyInput(firstName) || verifyInput(lastName) || verifyInput(username) || verifyInput(password) || verifyInput(repeatPassword) || verifyInput(email)) { errorMessage(t('registerStepOne.error.invalidInput')); return; } if (password != repeatPassword) { errorMessage(t('registerStepOne.error.passwordMismatch')); return; } try { const response: string = await apiPostPublic(`register/pre`, { firstName, lastName, username, email, password, retypePass: repeatPassword }, lang); if (!response) { errorMessage(t('registerStepOne.error.preRegister')); return; } setUserId(response); successMessage(t('registerStepOne.success.preRegister')); handleNextStep(); } catch (e: unknown) { if (e instanceof Error) { errorMessage(e.message); } else { errorMessage(t('registerStepOne.error.unknown')); } } } return (
setFirstName(e.target.value)} placeholder={t('registerStepOne.fields.firstName.placeholder')}/> }/> setLastName(e.target.value)} placeholder={t('registerStepOne.fields.lastName.placeholder')}/> }/>
setUsername(e.target.value)} placeholder={t('registerStepOne.fields.username.placeholder')}/> }/>

{t('registerStepOne.fields.username.note')}

setEmail(e.target.value)} placeholder={t('registerStepOne.fields.email.placeholder')}/> }/>

{t('registerStepOne.fields.password.label')}

setPassword(e.target.value)} required />

{t('registerStepOne.fields.repeatPassword.label')}

setRepeatPassword(e.target.value)} required />
) }