Skip to content

Commit

Permalink
implement validation logic for first step of registration form
Browse files Browse the repository at this point in the history
  • Loading branch information
SimeJadrijev committed Jan 29, 2025
1 parent f36105e commit c933aa1
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { ChangeEvent } from 'react';
import { ChangeEvent, useEffect, useState } from 'react';
import { Input } from '../../Input';
import c from './FirstStepRegistrationForm.module.scss';
import { Checkbox } from '../../Checkbox';
import { RegistrationFormErrors } from '../../../types/errors/errors.dto';
import { validateField } from '../../../helpers/validateInput';

type UserData = {
firstName: string;
Expand All @@ -17,12 +19,16 @@ type UserData = {
type Props = {
userData: UserData;
updateUserData: (newData: Partial<UserData>) => void;
isSubmitted: boolean;
};

export const FirstStepRegistrationForm = ({
userData,
updateUserData,
isSubmitted,
}: Props) => {
const [errors, setErrors] = useState<RegistrationFormErrors>({});

const handleInputChange = (e: ChangeEvent<HTMLInputElement>) => {
const { name, value } = e.target;
updateUserData({ [name]: value });
Expand All @@ -33,6 +39,26 @@ export const FirstStepRegistrationForm = ({
updateUserData({ [name]: checked });
};

useEffect(() => {
if (isSubmitted) {
const newErrors: RegistrationFormErrors = {};

Object.keys(userData).forEach((key) => {
const error = validateField(
key as keyof UserData,
userData[key as keyof UserData],
userData,
);

if (error) {
newErrors[key as keyof RegistrationFormErrors] = error;
}
});

setErrors(newErrors);
}
}, [isSubmitted, userData]);

return (
<>
<div className={c.inputFieldsWrapper}>
Expand All @@ -41,31 +67,36 @@ export const FirstStepRegistrationForm = ({
value={userData.firstName}
placeholder='Ime'
onChange={handleInputChange}
error={errors.firstName}
/>
<Input
name='lastName'
value={userData.lastName}
placeholder='Prezime'
onChange={handleInputChange}
error={errors.lastName}
/>
<Input
name='email'
value={userData.email}
placeholder='Email'
onChange={handleInputChange}
error={errors.email}
/>
<Input
name='password'
value={userData.password}
placeholder='Lozinka'
onChange={handleInputChange}
error={errors.password}
type='password'
/>
<Input
name='repeatedPassword'
value={userData.repeatedPassword}
placeholder='Potvrdite lozinku'
onChange={handleInputChange}
error={errors.repeatedPassword}
type='password'
/>
</div>
Expand All @@ -76,20 +107,23 @@ export const FirstStepRegistrationForm = ({
checked={userData.newsletterEnabled}
name='newsletterEnabled'
onChange={handleCheckboxChange}
error={errors.termsAndConditionsEnabled}
key={1}
/>
<Checkbox
label='Želim primati novosti o tvrtkama i otvorenim radnim pozicijama.'
checked={userData.companiesNewsEnabled}
name='companiesNewsEnabled'
onChange={handleCheckboxChange}
error={errors.termsAndConditionsEnabled}
key={2}
/>
<Checkbox
label='Slažem se s uvjetima i odredbama.'
checked={userData.termsAndConditionsEnabled}
name='termsAndConditionsEnabled'
onChange={handleCheckboxChange}
error={errors.termsAndConditionsEnabled}
key={3}
/>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { UserData } from '../../../types/user/user.dto';

export const GeneralRegistrationForm = () => {
const [currentStep, setCurrentStep] = useState(1);
const [isSubmitted, setIsSubmitted] = useState(false);

const [userData, setUserData] = useState<UserData>({
firstName: '',
Expand All @@ -33,8 +34,6 @@ export const GeneralRegistrationForm = () => {
}));
};

console.log(userData);

return (
<div className={c.generalRegistrationForm}>
<div className={c.registrationUpper}>
Expand All @@ -49,6 +48,7 @@ export const GeneralRegistrationForm = () => {
<FirstStepRegistrationForm
userData={userData}
updateUserData={updateUserData}
isSubmitted={isSubmitted}
/>
)}
{currentStep === 2 && (
Expand All @@ -63,7 +63,12 @@ export const GeneralRegistrationForm = () => {
{currentStep === 4 && <div>Četvrti korak</div>}

<div className={c.buttonsWrapper}>
<Button type='submit' variant='orange' children='Registriraj se' />
<Button
type='submit'
variant='orange'
children='Registriraj se'
onClick={() => setIsSubmitted(!isSubmitted)}
/>
<Button
type='submit'
variant='black'
Expand Down

0 comments on commit c933aa1

Please sign in to comment.