Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cnft1 2775 ethnicity section #1837

Merged
merged 3 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,26 +1,29 @@
import {
AddressEntry,
AdministrativeEntry,
IdentificationEntry,
PhoneEmailEntry,
RaceEntry,
NameEntry
PhoneEmailEntry,
NameEntry,
EthnicityEntry,
IdentificationEntry
} from 'apps/patient/data/entry';
import { internalizeDate } from 'date';
import { useState } from 'react';
import { FormProvider, useForm } from 'react-hook-form';
import { AddressMultiEntry } from './inputs/address/AddressMultiEntry';
import { Administrative } from './inputs/administrative/Administrative';
import { IdentificationMultiEntry } from './inputs/identification/IdentificationMultiEntry';
import { NameMultiEntry } from './inputs/name/NameMultiEntry';
import { PhoneAndEmailMultiEntry } from './inputs/phone/PhoneAndEmailMultiEntry';
import { RaceMultiEntry } from './inputs/race/RaceMultiEntry';
import { AddPatientExtendedNav } from './nav/AddPatientExtendedNav';
import styles from './add-patient-extended-form.module.scss';
import { EthnicityEntryCard } from './inputs/ethnicity/EthnicityEntryCard';
import { NameMultiEntry } from './inputs/name/NameMultiEntry';

// Once all sections have been updated with proper types this will be removed
type ExtendedPatientCreationForm = {
administrative: AdministrativeEntry;
ethnicity: EthnicityEntry;
address: AddressEntry[];
phone: PhoneEmailEntry[];
race: RaceEntry[];
Expand All @@ -37,12 +40,16 @@ type DirtyState = {
race: boolean;
};
export const AddPatientExtendedForm = () => {
const today = internalizeDate(new Date());
const form = useForm<ExtendedPatientCreationForm>({
defaultValues: {
phone: [],
administrative: {
asOf: internalizeDate(new Date()),
asOf: today,
comment: ''
},
ethnicity: {
asOf: today
}
},
mode: 'onBlur'
Expand Down Expand Up @@ -91,6 +98,7 @@ export const AddPatientExtendedForm = () => {
form.setValue('race', raceData);
}}
/>
<EthnicityEntryCard />
</div>
</FormProvider>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,4 @@
align-items: center;
@extend %thin-bottom;
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { render } from '@testing-library/react';
import { PatientEthnicityCodedValue } from 'apps/patient/profile/ethnicity';
import { FormProvider, useForm } from 'react-hook-form';
import { EthnicityEntryCard } from './EthnicityEntryCard';

const mockEthnicityValues: PatientEthnicityCodedValue = {
ethnicGroups: [{ name: 'Hispanic or Latino', value: '2135-2' }],
ethnicityUnknownReasons: [{ name: 'Not asked', value: '6' }],
detailedEthnicities: [{ name: 'Central American', value: '2155-0' }]
};

jest.mock('apps/patient/profile/ethnicity/usePatientEthnicityCodedValues', () => ({
usePatientEthnicityCodedValues: () => mockEthnicityValues
}));

const Fixture = () => {
const form = useForm();
return (
<FormProvider {...form}>
<EthnicityEntryCard />
</FormProvider>
);
};

describe('EthnicityEntryCard', () => {
it('should display Ethnicity header on card', () => {
const { getAllByText } = render(<Fixture />);
expect(getAllByText('Ethnicity')).toHaveLength(2);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { EthnicityEntryFields } from 'apps/patient/data/ethnicity/EthnicityEntryFields';
import { Card } from '../../card/Card';

export const EthnicityEntryCard = () => {
return (
<Card id="ethnicity" title="Ethnicity">
<EthnicityEntryFields />
</Card>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const AddPatientExtendedNav = () => {
<a href="#section-PhoneAndEmail">Phone & email</a>
<a href="#identification">Identification</a>
<a href="#section-Race">Race</a>
<a href="#section-Ethnicity">Ethnicity</a>
<a href="#ethnicity">Ethnicity</a>
<a href="#section-SexAndBirth">Sex & birth</a>
<a href="#section-Mortality">Mortality</a>
<a href="#section-General">General patient information</a>
Expand Down
1 change: 1 addition & 0 deletions apps/modernization-ui/src/apps/patient/data/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ type Race = EffectiveDated & {
type Ethnicity = EffectiveDated & {
ethnicity: string;
detailed: string[];
reasonUnknown?: string;
};

type Sex = {
Expand Down
1 change: 1 addition & 0 deletions apps/modernization-ui/src/apps/patient/data/entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ type RaceEntry = EffectiveDated & {
type EthnicityEntry = EffectiveDated & {
ethnicity: Selectable;
detailed: Selectable[];
reasonUnknown?: Selectable;
};

type SexEntry = EffectiveDated & {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { FormProvider, useForm } from 'react-hook-form';
import { EthnicityEntryFields } from './EthnicityEntryFields';
import { PatientEthnicityCodedValue } from 'apps/patient/profile/ethnicity';
import { EthnicityEntry } from '../entry';
import { render, waitFor } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { act } from 'react-dom/test-utils';

const mockEthnicityValues: PatientEthnicityCodedValue = {
ethnicGroups: [
{ name: 'Hispanic or Latino', value: '2135-2' },
{ name: 'Unknown', value: 'UNK' }
],
ethnicityUnknownReasons: [{ name: 'Not asked', value: '6' }],
detailedEthnicities: [{ name: 'Central American', value: '2155-0' }]
};

jest.mock('apps/patient/profile/ethnicity/usePatientEthnicityCodedValues', () => ({
usePatientEthnicityCodedValues: () => mockEthnicityValues
}));
const Fixture = () => {
const form = useForm<EthnicityEntry>({ mode: 'onBlur' });
return (
<FormProvider {...form}>
<EthnicityEntryFields />
</FormProvider>
);
};

describe('EthnicityEntryFields', () => {
it('should render the proper labels', () => {
const { getByLabelText, queryByLabelText, getByText } = render(<Fixture />);

expect(getByLabelText('Ethnicity information as of')).toBeInTheDocument();
expect(getByText('Ethnicity information as of')).toHaveClass('required');
expect(getByLabelText('Ethnicity')).toBeInTheDocument();
expect(getByText('Ethnicity')).not.toHaveClass('required');
expect(queryByLabelText('Spanish origin')).not.toBeInTheDocument();
expect(queryByLabelText('Reason unknown')).not.toBeInTheDocument();
});

it('should require as of date', async () => {
const { getByLabelText, getByText } = render(<Fixture />);
const asOf = getByLabelText('Ethnicity information as of');
act(() => {
userEvent.click(asOf);
userEvent.tab();
});

await waitFor(() => {
expect(getByText('As of date is required.')).toBeInTheDocument();
});
});

it('should display spanish origin when hispanic or latino selected', async () => {
const { getByLabelText, queryByLabelText } = render(<Fixture />);

act(() => {
userEvent.selectOptions(getByLabelText('Ethnicity'), '2135-2');
});

await waitFor(() => {
expect(getByLabelText('Spanish origin')).toBeInTheDocument();
});
expect(queryByLabelText('Reason unknown')).not.toBeInTheDocument();
});

it('should display unknown reason when unknown selected', async () => {
const { getByLabelText, queryByLabelText } = render(<Fixture />);

act(() => {
userEvent.selectOptions(getByLabelText('Ethnicity'), 'UNK');
});

await waitFor(() => {
expect(getByLabelText('Reason unknown')).toBeInTheDocument();
});
expect(queryByLabelText('Spanish origin')).not.toBeInTheDocument();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import { usePatientEthnicityCodedValues } from 'apps/patient/profile/ethnicity';
import { DatePickerInput } from 'components/FormInputs/DatePickerInput';
import { MultiSelect, SingleSelect } from 'design-system/select';
import { useEffect } from 'react';
import { Controller, useFormContext, useWatch } from 'react-hook-form';
import { EthnicityEntry } from '../entry';

const UNKNOWN = 'UNK';
const HISPANIC = '2135-2';

export const EthnicityEntryFields = () => {
const { control, setValue } = useFormContext<{ ethnicity: EthnicityEntry }>();
const coded = usePatientEthnicityCodedValues();
const selectedEthinicity = useWatch({ control, name: 'ethnicity.ethnicity' });

useEffect(() => {
setValue('ethnicity.reasonUnknown', undefined);
setValue('ethnicity.detailed', []);
}, [selectedEthinicity]);

return (
<section>
<Controller
control={control}
name="ethnicity.asOf"
rules={{ required: { value: true, message: 'As of date is required.' } }}
render={({ field: { onBlur, onChange, value, name }, fieldState: { error } }) => (
<DatePickerInput
label="Ethnicity information as of"
orientation="horizontal"
defaultValue={value}
onBlur={onBlur}
onChange={onChange}
name={name}
disableFutureDates
errorMessage={error?.message}
required
/>
)}
/>
<Controller
control={control}
name="ethnicity.ethnicity"
render={({ field: { onChange, onBlur, value, name } }) => (
<SingleSelect
label="Ethnicity"
orientation="horizontal"
onChange={onChange}
onBlur={onBlur}
id={name}
name={name}
value={value}
options={coded.ethnicGroups}
/>
)}
/>

{selectedEthinicity?.value === HISPANIC && (
<Controller
control={control}
name="ethnicity.detailed"
shouldUnregister
render={({ field: { onChange, onBlur, value, name } }) => (
<MultiSelect
label="Spanish origin"
orientation="horizontal"
onChange={onChange}
onBlur={onBlur}
id={name}
name={name}
value={value}
options={coded.detailedEthnicities}
/>
)}
/>
)}
{selectedEthinicity?.value === UNKNOWN && (
<Controller
control={control}
name="ethnicity.reasonUnknown"
shouldUnregister
render={({ field: { onChange, onBlur, value, name } }) => (
<SingleSelect
label="Reason unknown"
orientation="horizontal"
value={value}
onChange={onChange}
onBlur={onBlur}
id={name}
name={name}
options={coded.ethnicityUnknownReasons}
/>
)}
/>
)}
</section>
);
};
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { asValue, asValues } from 'options';
import { EthnicityEntry } from './entry';
import { Ethnicity } from './api';
import { EthnicityEntry } from '../entry';
import { Ethnicity } from '../api';

const asEthnicity = (entry: EthnicityEntry): Ethnicity => {
const { ethnicity, detailed, ...remaining } = entry;
const { ethnicity, detailed, reasonUnknown, ...remaining } = entry;

return {
ethnicity: asValue(ethnicity),
detailed: asValues(detailed),
reasonUnknown: asValue(reasonUnknown),
...remaining
};
};
Expand Down
2 changes: 1 addition & 1 deletion apps/modernization-ui/src/apps/patient/data/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export { asAddress } from './address/asAddress';
export { asPhoneEmail } from './phoneEmail/asPhoneEmail';
export { asIdentification } from './identification/asIdentification';
export { asRace } from './race/asRace';
export { asEthnicity } from './asEthnicity';
export { asEthnicity } from './ethnicity/asEthnicity';
export { asSex } from './asSex';
export { asBirth } from './asBirth';
export { asMortality } from './asMortality';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ const initialCoded = {
detailedEthnicities: []
};

type PatietnEthnicityCodedValue = {
type PatientEthnicityCodedValue = {
ethnicGroups: CodedValue[];
ethnicityUnknownReasons: CodedValue[];
detailedEthnicities: CodedValue[];
};

const usePatientEthnicityCodedValues = () => {
const [coded, setCoded] = useState<PatietnEthnicityCodedValue>(initialCoded);
const [coded, setCoded] = useState<PatientEthnicityCodedValue>(initialCoded);

const handleComplete = (data: Result) => {
setCoded({
Expand All @@ -65,4 +65,4 @@ const usePatientEthnicityCodedValues = () => {
};

export { usePatientEthnicityCodedValues };
export type { PatietnEthnicityCodedValue };
export type { PatientEthnicityCodedValue };
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export const MultiSelect = ({
theme={theme}
styles={styles}
isMulti
id={id}
inputId={id}
name={name}
options={options}
value={value}
Expand Down
Loading