From 7e22dac4cf0d60c090dbbda6f191b0ae263e52a1 Mon Sep 17 00:00:00 2001 From: noel-yeldos Date: Mon, 16 Dec 2024 12:41:15 +1300 Subject: [PATCH] Add debounce to email input --- .../common/src/intl/locales/en/common.json | 1 + .../packages/host/src/Help/FeedbackForm.tsx | 23 ++++++++------ .../src/api/hooks/help/useFeedbackForm.ts | 31 ++++++++++++++----- 3 files changed, 37 insertions(+), 18 deletions(-) diff --git a/client/packages/common/src/intl/locales/en/common.json b/client/packages/common/src/intl/locales/en/common.json index 40c26940e5..088cc1e301 100644 --- a/client/packages/common/src/intl/locales/en/common.json +++ b/client/packages/common/src/intl/locales/en/common.json @@ -1365,6 +1365,7 @@ "messages.error-generic_one": "{{count}} error", "messages.error-generic_other": "{{count}} errors", "messages.error-no-file-selected": "No file selected", + "messages.error-not-valid-email": "Please enter a valid email address", "messages.error-saving-prescription": "Error saving prescription 🥺", "messages.error-saving-return": "Error saving return 🥺", "messages.error-saving-shipment": "Error saving shipment 🥺", diff --git a/client/packages/host/src/Help/FeedbackForm.tsx b/client/packages/host/src/Help/FeedbackForm.tsx index e5bee87804..b8640dcfdd 100644 --- a/client/packages/host/src/Help/FeedbackForm.tsx +++ b/client/packages/host/src/Help/FeedbackForm.tsx @@ -20,7 +20,9 @@ export const FeedbackForm = () => { saveFeedback, draft, isValidInput, - checkEmailValidity, + checkEmailIsValid, + debounceValidation, + emailError, } = useFeedbackForm(); const save = async () => { @@ -35,7 +37,13 @@ export const FeedbackForm = () => { } }; - const isValidEmail = checkEmailValidity(draft.replyEmail); + const isValidEmail = checkEmailIsValid(draft.replyEmail); + + const handleChange = (e: React.ChangeEvent) => { + const email = e.target.value; + updateDraft({ replyEmail: email }); + debounceValidation(email); + }; return ( <> @@ -45,15 +53,10 @@ export const FeedbackForm = () => { Input={ updateDraft({ replyEmail: e.target.value })} - // onChange={handleChange} + onChange={handleChange} fullWidth - helperText={ - !isValidEmail - ? 'Please enter a valid email e.g help@example.com' - : '' - } - error={!isValidEmail} + helperText={emailError} + error={!isValidEmail && draft.replyEmail !== ''} /> } /> diff --git a/client/packages/host/src/api/hooks/help/useFeedbackForm.ts b/client/packages/host/src/api/hooks/help/useFeedbackForm.ts index c73350c741..76cf4ac828 100644 --- a/client/packages/host/src/api/hooks/help/useFeedbackForm.ts +++ b/client/packages/host/src/api/hooks/help/useFeedbackForm.ts @@ -1,7 +1,13 @@ import { InsertContactFormInput } from '@common/types'; import { useState } from 'react'; import { useFeedbackFormGraphQL } from './useFeedbackFormGraphQL'; -import { FnUtils, isEmpty, useMutation } from '@openmsupply-client/common'; +import { + FnUtils, + isEmpty, + useMutation, + useDebounceCallback, + useTranslation, +} from '@openmsupply-client/common'; type ContactFormInput = Pick; @@ -11,6 +17,8 @@ export function useFeedbackForm() { body: '', }; const [draft, setDraft] = useState(defaultDraft); + const [emailError, setEmailError] = useState(''); + const t = useTranslation(); const { mutateAsync: insert } = useInsert(); @@ -23,18 +31,23 @@ export function useFeedbackForm() { setDraft(defaultDraft); }; - const checkEmailValidity = (email: string) => { + const checkEmailIsValid = (email: string) => { const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; return emailRegex.test(email); }; - // const isNotValidEmail = (email: string) => { - // const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; - // if (!emailRegex.test(email)) return true; - // }; + const debounceValidation = useDebounceCallback( + (email: string) => { + if (!checkEmailIsValid(email)) + return setEmailError(t('messages.error-not-valid-email')); + return setEmailError(''); + }, + [setEmailError], + 100 + ); const isValidInput = - !!draft.replyEmail && !!draft.body && checkEmailValidity(draft.replyEmail); + !!draft.replyEmail && !!draft.body && checkEmailIsValid(draft.replyEmail); return { updateDraft, @@ -42,7 +55,9 @@ export function useFeedbackForm() { saveFeedback: insert, draft, isValidInput, - checkEmailValidity, + checkEmailIsValid, + debounceValidation, + emailError, }; }