Skip to content

Commit

Permalink
added validation
Browse files Browse the repository at this point in the history
  • Loading branch information
kslazykv committed Jul 31, 2024
1 parent 4a96ae0 commit d067ef5
Showing 1 changed file with 24 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ const Participants = ({
}); //filter string and index of participant
const { apiClient } = useInvitationForPunchOutContext();
const [isLoading, setIsLoading] = useState<boolean>(false);
const [emailError, setEmailError] = useState<string | null>(null);

const nameCombiner = (firstName: string, lastName: string): string => {
return `${firstName} ${lastName}`;
Expand Down Expand Up @@ -191,16 +192,26 @@ const Participants = ({
}
};

const validateEmail = (email: string): boolean => {
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
return emailRegex.test(email);
};

const setExternalEmail = (value: string, index: number): void => {
setParticipants((p) => {
const participantsCopy = [...p];
participantsCopy[index].role = null;
participantsCopy[index].person = null;
participantsCopy[index].externalEmail = {
email: value,
};
return participantsCopy;
});
if (!validateEmail(value)) {
setEmailError('Invalid email address');
} else {
setEmailError(null);
setParticipants((p) => {
const participantsCopy = [...p];
participantsCopy[index].role = null;
participantsCopy[index].person = null;
participantsCopy[index].externalEmail = {
email: value,
};
return participantsCopy;
});
}
};

const setType = (value: string, index: number): void => {
Expand Down Expand Up @@ -430,6 +441,10 @@ const Participants = ({
index
)
}
helperText={emailError}
variant={
emailError ? 'error' : undefined
}
/>
</div>
)}
Expand Down

0 comments on commit d067ef5

Please sign in to comment.