diff --git a/src/__tests__/patients/related-persons/AddRelatedPersonModal.test.tsx b/src/__tests__/patients/related-persons/AddRelatedPersonModal.test.tsx index 23b5348334..b9c71a7fcf 100644 --- a/src/__tests__/patients/related-persons/AddRelatedPersonModal.test.tsx +++ b/src/__tests__/patients/related-persons/AddRelatedPersonModal.test.tsx @@ -143,10 +143,11 @@ describe('Add Related Person Modal', () => { wrapper.update() const errorAlert = wrapper.find(Alert) + expect(onSaveSpy).not.toHaveBeenCalled() expect(errorAlert).toHaveLength(1) expect(errorAlert.prop('message')).toEqual( - 'patient.relatedPersons.error.relatedPersonRequired patient.relatedPersons.error.relationshipTypeRequired', + 'patient.relatedPersons.error.relatedPersonErrorBanner', ) }) }) diff --git a/src/locales/enUs/translations/patient/index.ts b/src/locales/enUs/translations/patient/index.ts index e599ffb33f..7f83290fda 100644 --- a/src/locales/enUs/translations/patient/index.ts +++ b/src/locales/enUs/translations/patient/index.ts @@ -25,6 +25,7 @@ export default { relatedPerson: 'Related Person', relatedPersons: { error: { + relatedPersonErrorBanner: 'Unable to add new related person.', relatedPersonRequired: 'Related Person is required.', relationshipTypeRequired: 'Relationship Type is required.', }, diff --git a/src/patients/related-persons/AddRelatedPersonModal.tsx b/src/patients/related-persons/AddRelatedPersonModal.tsx index 809c8e66e1..fd648a924b 100644 --- a/src/patients/related-persons/AddRelatedPersonModal.tsx +++ b/src/patients/related-persons/AddRelatedPersonModal.tsx @@ -19,6 +19,8 @@ const AddRelatedPersonModal = (props: Props) => { const { show, toggle, onCloseButtonClick, onSave } = props const { t } = useTranslation() const [errorMessage, setErrorMessage] = useState('') + const [isRelatedPersonInvalid, setIsRelatedPersonInvalid] = useState(false) + const [isRelationshipInvalid, setIsRelationshipInvalid] = useState(false) const [relatedPerson, setRelatedPerson] = useState({ patientId: '', type: '', @@ -54,6 +56,7 @@ const AddRelatedPersonModal = (props: Props) => { searchAccessor="fullName" placeholder={t('patient.relatedPerson')} onChange={onPatientSelect} + isInvalid={isRelatedPersonInvalid} onSearch={async (query: string) => PatientRepository.search(query)} renderMenuItemChildren={(p: Patient) => { if (patientId() === p.id) { @@ -63,6 +66,11 @@ const AddRelatedPersonModal = (props: Props) => { return
{`${p.fullName} (${p.code})`}
}} /> + {isRelatedPersonInvalid && ( +
+ {t('patient.relatedPersons.error.relatedPersonRequired')} +
+ )} @@ -73,6 +81,8 @@ const AddRelatedPersonModal = (props: Props) => { label={t('patient.relatedPersons.relationshipType')} value={relatedPerson.type} isEditable + isInvalid={isRelationshipInvalid} + feedback={t('patient.relatedPersons.error.relationshipTypeRequired')} isRequired onChange={(event: React.ChangeEvent) => { onInputElementChange(event, 'type') @@ -100,19 +110,21 @@ const AddRelatedPersonModal = (props: Props) => { icon: 'add', iconLocation: 'left', onClick: () => { - let newErrorMessage = '' + let isValid = true if (!relatedPerson.patientId) { - newErrorMessage += `${t('patient.relatedPersons.error.relatedPersonRequired')} ` + isValid = false + setIsRelatedPersonInvalid(true) } if (!relatedPerson.type) { - newErrorMessage += `${t('patient.relatedPersons.error.relationshipTypeRequired')}` + isValid = false + setIsRelationshipInvalid(true) } - if (!newErrorMessage) { + if (isValid) { onSave(relatedPerson as RelatedPerson) } else { - setErrorMessage(newErrorMessage.trim()) + setErrorMessage(t('patient.relatedPersons.error.relatedPersonErrorBanner')) } }, }}