From b816ebf84661fe7290338451fe2737dfc1c0efa2 Mon Sep 17 00:00:00 2001 From: Altemush Bhatti Date: Fri, 10 Apr 2020 17:44:10 -0700 Subject: [PATCH] fix(patients): current patient can no longer be related person (#1959) --- .../AddRelatedPersonModal.test.tsx | 59 +++++++++++++++---- .../related-persons/AddRelatedPersonModal.tsx | 19 ++++-- 2 files changed, 60 insertions(+), 18 deletions(-) diff --git a/src/__tests__/patients/related-persons/AddRelatedPersonModal.test.tsx b/src/__tests__/patients/related-persons/AddRelatedPersonModal.test.tsx index 19a0ae75c4..23b5348334 100644 --- a/src/__tests__/patients/related-persons/AddRelatedPersonModal.test.tsx +++ b/src/__tests__/patients/related-persons/AddRelatedPersonModal.test.tsx @@ -3,20 +3,51 @@ import React from 'react' import { ReactWrapper, mount } from 'enzyme' import { Modal, Alert, Typeahead } from '@hospitalrun/components' import { act } from '@testing-library/react' -import AddRelatedPersonModal from '../../../patients/related-persons/AddRelatedPersonModal' +import { Provider } from 'react-redux' +import thunk from 'redux-thunk' +import configureMockStore, { MockStore } from 'redux-mock-store' +import Patient from 'model/Patient' import TextInputWithLabelFormGroup from '../../../components/input/TextInputWithLabelFormGroup' +import AddRelatedPersonModal from '../../../patients/related-persons/AddRelatedPersonModal' + +const mockStore = configureMockStore([thunk]) describe('Add Related Person Modal', () => { + const patient = { + id: '123', + prefix: 'prefix', + givenName: 'givenName', + familyName: 'familyName', + suffix: 'suffix', + sex: 'male', + type: 'charity', + occupation: 'occupation', + preferredLanguage: 'preferredLanguage', + phoneNumber: 'phoneNumber', + email: 'email@email.com', + address: 'address', + code: 'P00001', + dateOfBirth: new Date().toISOString(), + } as Patient + + let store: MockStore + describe('layout', () => { let wrapper: ReactWrapper + + store = mockStore({ + patient: { patient }, + }) beforeEach(() => { wrapper = mount( - , + + + , ) }) @@ -65,12 +96,14 @@ describe('Add Related Person Modal', () => { beforeEach(() => { onSaveSpy = jest.fn() wrapper = mount( - , + + + , ) }) diff --git a/src/patients/related-persons/AddRelatedPersonModal.tsx b/src/patients/related-persons/AddRelatedPersonModal.tsx index d4e652fd28..809c8e66e1 100644 --- a/src/patients/related-persons/AddRelatedPersonModal.tsx +++ b/src/patients/related-persons/AddRelatedPersonModal.tsx @@ -5,6 +5,8 @@ import TextInputWithLabelFormGroup from 'components/input/TextInputWithLabelForm import RelatedPerson from 'model/RelatedPerson' import PatientRepository from 'clients/db/PatientRepository' import Patient from 'model/Patient' +import { useSelector } from 'react-redux' +import { RootState } from '../../store' interface Props { show: boolean @@ -21,6 +23,9 @@ const AddRelatedPersonModal = (props: Props) => { patientId: '', type: '', }) + const { patient } = useSelector((state: RootState) => state.patient) + + const patientId = () => patient.id const onFieldChange = (key: string, value: string) => { setRelatedPerson({ @@ -33,8 +38,8 @@ const AddRelatedPersonModal = (props: Props) => { onFieldChange(fieldName, event.target.value) } - const onPatientSelect = (patient: Patient[]) => { - setRelatedPerson({ ...relatedPerson, patientId: patient[0].id }) + const onPatientSelect = (p: Patient[]) => { + setRelatedPerson({ ...relatedPerson, patientId: p[0].id }) } const body = ( @@ -50,9 +55,13 @@ const AddRelatedPersonModal = (props: Props) => { placeholder={t('patient.relatedPerson')} onChange={onPatientSelect} onSearch={async (query: string) => PatientRepository.search(query)} - renderMenuItemChildren={(patient: Patient) => ( -
{`${patient.fullName} (${patient.code})`}
- )} + renderMenuItemChildren={(p: Patient) => { + if (patientId() === p.id) { + return
+ } + + return
{`${p.fullName} (${p.code})`}
+ }} />