diff --git a/client/components/CustomFieldsForm.js b/client/components/CustomFieldsForm.js index 9af38197c558..c71a4a482df5 100644 --- a/client/components/CustomFieldsForm.js +++ b/client/components/CustomFieldsForm.js @@ -5,15 +5,17 @@ import { useSetting } from '../contexts/SettingsContext'; import { useForm } from '../hooks/useForm'; import { useTranslation } from '../contexts/TranslationContext'; import { capitalize } from '../lib/capitalize'; +import { useComponentDidUpdate } from '../hooks/useComponentDidUpdate'; -const CustomTextInput = ({ name, required, minLength, maxLength, setState, state, className }) => { +const CustomTextInput = ({ name, required, minLength, maxLength, setState, state, className, setCustomFieldsError = () => [] }) => { const t = useTranslation(); + const [inputError, setInputError] = useState(''); + const verify = useMemo(() => { const errors = []; - if (!state && required) { - errors.push(t('Field_required')); + errors.push(t('The_field_is_required', name)); } if (state.length < minLength && state.length > 0) { @@ -21,29 +23,47 @@ const CustomTextInput = ({ name, required, minLength, maxLength, setState, state } return errors.join(', '); - }, [state, required, minLength, t]); + }, [state, required, minLength, t, name]); + + useEffect(() => { + setCustomFieldsError((oldErrors) => (verify ? [...oldErrors, { name }] : oldErrors.filter((item) => item.name !== name))); + }, [name, setCustomFieldsError, verify]); + + useComponentDidUpdate(() => { + setInputError(verify); + }, [verify]); return useMemo(() => {t(name)}{required && '*'} - setState(e.currentTarget.value)}/> + setState(e.currentTarget.value)}/> - {verify} - , [className, t, name, verify, maxLength, state, required, setState]); + {inputError} + , [className, t, name, required, inputError, maxLength, state, setState]); }; -const CustomSelect = ({ name, required, options = {}, setState, state, className }) => { +const CustomSelect = ({ name, required, options = {}, setState, state, className, setCustomFieldsError = () => [] }) => { const t = useTranslation(); + const [selectError, setSelectError] = useState(''); + const mappedOptions = useMemo(() => Object.values(options).map((value) => [value, value]), [options]); - const verify = useMemo(() => (!state.length && required ? t('Field_required') : ''), [required, state.length, t]); + const verify = useMemo(() => (!state.length && required ? t('The_field_is_required', name) : ''), [name, required, state.length, t]); + + useEffect(() => { + setCustomFieldsError((oldErrors) => (verify ? [...oldErrors, { name }] : oldErrors.filter((item) => item.name !== name))); + }, [name, setCustomFieldsError, verify]); + + useComponentDidUpdate(() => { + setSelectError(verify); + }, [verify]); return useMemo(() => {t(name)}{required && '*'} - setState(val)}/> - {verify} - , [className, t, name, verify, state, mappedOptions, required, setState]); + {selectError} + , [className, t, name, required, selectError, state, mappedOptions, setState]); }; const CustomFieldsAssembler = ({ formValues, formHandlers, customFields, ...props }) => Object.entries(customFields).map(([key, value]) => { diff --git a/client/omnichannel/directory/ContactForm.js b/client/omnichannel/directory/ContactForm.js index fb018ac6b77a..559bc8e3731d 100644 --- a/client/omnichannel/directory/ContactForm.js +++ b/client/omnichannel/directory/ContactForm.js @@ -96,6 +96,7 @@ export function ContactNewEdit({ id, data, reload, close }) { const [nameError, setNameError] = useState(); const [emailError, setEmailError] = useState(); const [phoneError, setPhoneError] = useState(); + const [customFieldsError, setCustomFieldsError] = useState([]); const { value: allCustomFields, phase: state } = useEndpointData('livechat/custom-fields'); @@ -193,7 +194,7 @@ export function ContactNewEdit({ id, data, reload, close }) { } }); - const formIsValid = name && !emailError && !phoneError; + const formIsValid = name && !emailError && !phoneError && customFieldsError.length === 0; if ([state].includes(AsyncStatePhase.LOADING)) { @@ -230,7 +231,7 @@ export function ContactNewEdit({ id, data, reload, close }) { { canViewCustomFields() && allCustomFields - && } + && } { ContactManager && }