Skip to content

Commit

Permalink
Show error if email not valid
Browse files Browse the repository at this point in the history
TODO add error translation + add delay
  • Loading branch information
noel-yeldos committed Dec 13, 2024
1 parent 405f86d commit b39e3eb
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
19 changes: 17 additions & 2 deletions client/packages/host/src/Help/FeedbackForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,14 @@ import { useFeedbackForm } from '../api/hooks/help/useFeedbackForm';
export const FeedbackForm = () => {
const t = useTranslation();
const { success, error } = useNotification();
const { updateDraft, resetDraft, saveFeedback, draft, isValidInput } =
useFeedbackForm();
const {
updateDraft,
resetDraft,
saveFeedback,
draft,
isValidInput,
checkEmailValidity,
} = useFeedbackForm();

const save = async () => {
try {
Expand All @@ -29,6 +35,8 @@ export const FeedbackForm = () => {
}
};

const isValidEmail = checkEmailValidity(draft.replyEmail);

return (
<>
<InputWithLabelRow
Expand All @@ -38,7 +46,14 @@ export const FeedbackForm = () => {
<BasicTextInput
value={draft.replyEmail}
onChange={e => updateDraft({ replyEmail: e.target.value })}
// onChange={handleChange}
fullWidth
helperText={
!isValidEmail
? 'Please enter a valid email e.g [email protected]'
: ''
}
error={!isValidEmail}
/>
}
/>
Expand Down
13 changes: 10 additions & 3 deletions client/packages/host/src/api/hooks/help/useFeedbackForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,26 @@ export function useFeedbackForm() {
setDraft(defaultDraft);
};

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

const isValidInput = !!draft.replyEmail && !!draft.body && isValidEmail();
// const isNotValidEmail = (email: string) => {
// const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
// if (!emailRegex.test(email)) return true;
// };

const isValidInput =
!!draft.replyEmail && !!draft.body && checkEmailValidity(draft.replyEmail);

return {
updateDraft,
resetDraft,
saveFeedback: insert,
draft,
isValidInput,
checkEmailValidity,
};
}

Expand Down

0 comments on commit b39e3eb

Please sign in to comment.