Skip to content
This repository has been archived by the owner on Jan 9, 2023. It is now read-only.

Commit

Permalink
feat(newrelatedpersonmodal): update Error Feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
bmoore235 authored and jackcmeyer committed Apr 17, 2020
1 parent 5d02d97 commit 57f9d7a
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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',
)
})
})
Expand Down
1 change: 1 addition & 0 deletions src/locales/enUs/translations/patient/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.',
},
Expand Down
22 changes: 17 additions & 5 deletions src/patients/related-persons/AddRelatedPersonModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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: '',
Expand Down Expand Up @@ -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) {
Expand All @@ -63,6 +66,11 @@ const AddRelatedPersonModal = (props: Props) => {
return <div>{`${p.fullName} (${p.code})`}</div>
}}
/>
{isRelatedPersonInvalid && (
<div className="text-left ml-3 mt-1 text-small text-danger invalid-feedback d-block">
{t('patient.relatedPersons.error.relatedPersonRequired')}
</div>
)}
</div>
</div>
</div>
Expand All @@ -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<HTMLInputElement>) => {
onInputElementChange(event, 'type')
Expand Down Expand Up @@ -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'))
}
},
}}
Expand Down

0 comments on commit 57f9d7a

Please sign in to comment.