Skip to content

Commit

Permalink
Code cleanup/refactor, fix failing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kajambiya committed Mar 14, 2024
1 parent a22b6d6 commit 0b4843d
Show file tree
Hide file tree
Showing 19 changed files with 68 additions and 42 deletions.
6 changes: 0 additions & 6 deletions src/api/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,6 @@ export function getAllLocations(): Observable<{ uuid: string; display: string }[
);
}

export function getEncounterProviders(): Observable<{ uuid: string; display: string }[]> {
return openmrsObservableFetch(`/ws/rest/v1/provider?v=custom:(uuid,display)`).pipe(
map(({ data }) => data['results']),
);
}

export async function getPreviousEncounter(patientUuid: string, encounterType: string) {
const query = `patient=${patientUuid}&_sort=-_lastUpdated&_count=1&type=${encounterType}`;
let response = await openmrsFetch(`/ws/fhir2/R4/Encounter?${query}`);
Expand Down
1 change: 1 addition & 0 deletions src/api/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ export type RenderType =
| 'group'
| 'content-switcher'
| 'encounter-location'
| 'encounter-provider'
| 'textarea'
| 'toggle'
| 'fixed-value'
Expand Down
1 change: 0 additions & 1 deletion src/components/encounter/ohri-encounter-form.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -713,7 +713,6 @@ export const OHRIEncounterForm: React.FC<OHRIEncounterFormProps> = ({
value={{
values,
setFieldValue,
setEncounterProvider: setEncounterProvider,
setEncounterLocation: setEncounterLocation,
setObsGroupsToVoid: setObsGroupsToVoid,
obsGroupsToVoid: obsGroupsToVoid,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,16 @@ const encounterContext: EncounterContext = {
},
sessionMode: 'enter',
encounterDate: new Date(2020, 11, 29),
setEncounterDate: value => {},
setEncounterDate: (value) => {},

encounterProvider: '2c95f6f5-788e-4e73-9079-5626911231fa',
setEncounterProvider: jest.fn,
};

const renderForm = intialValues => {
const renderForm = (intialValues) => {
render(
<Formik initialValues={intialValues} onSubmit={null}>
{props => (
{(props) => (
<Form>
<OHRIFormContext.Provider
value={{
Expand All @@ -63,7 +66,7 @@ const renderForm = intialValues => {
fields: [question],
isFieldInitializationComplete: true,
isSubmitting: false,
formFieldHandlers: { 'obs': ObsSubmissionHandler }
formFieldHandlers: { obs: ObsSubmissionHandler },
}}>
<OHRIContentSwitcher question={question} onChange={jest.fn()} handler={ObsSubmissionHandler} />
</OHRIFormContext.Provider>
Expand Down
8 changes: 5 additions & 3 deletions src/components/inputs/select/ohri-dropdown.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,15 @@ const encounterContext: EncounterContext = {
},
sessionMode: 'enter',
encounterDate: new Date(2020, 11, 29),
setEncounterDate: value => {},
setEncounterDate: (value) => {},
encounterProvider: '2c95f6f5-788e-4e73-9079-5626911231fa',
setEncounterProvider: jest.fn,
};

const renderForm = intialValues => {
const renderForm = (intialValues) => {
render(
<Formik initialValues={intialValues} onSubmit={null}>
{props => (
{(props) => (
<Form>
<OHRIFormContext.Provider
value={{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ const encounterContext: EncounterContext = {
sessionMode: 'enter',
encounterDate: new Date(2023, 8, 29),
setEncounterDate: (value) => {},
encounterProvider: '2c95f6f5-788e-4e73-9079-5626911231fa',
setEncounterProvider: jest.fn,
};

const renderForm = (intialValues) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,7 @@ const UISelectExtended: React.FC<OHRIFormFieldProps> = ({ question, handler, onC
}, [encounterContext.sessionMode, question.readonly, question.inlineRendering, layoutType, workspaceLayout]);

useEffect(() => {
let datasourceName = question.questionOptions?.datasource?.name;
if (question.type === 'encounterProvider') {
datasourceName = question.type;
}
const datasourceName = question.questionOptions?.datasource?.name;
setConfig(
datasourceName
? question.questionOptions.datasource?.config
Expand Down
2 changes: 2 additions & 0 deletions src/components/inputs/unspecified/ohri-unspecified.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ const encounterContext: EncounterContext = {
sessionMode: 'enter',
encounterDate: new Date(2020, 11, 29),
setEncounterDate: (value) => {},
encounterProvider: '2c95f6f5-788e-4e73-9079-5626911231fa',
setEncounterProvider: jest.fn,
};

const renderForm = (intialValues) => {
Expand Down
23 changes: 16 additions & 7 deletions src/components/section/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,6 @@ export function getFieldControlWithFallback(question: OHRIFormField) {
return getRegisteredControl('text');
}

//Rendering overrides for existing AFE form schemas
if (question.type === 'encounterLocation') {
question.questionOptions.rendering = 'encounter-location';
}
if (question.type === 'encounterProvider') {
return getRegisteredControl('encounterProvider');
}
// Retrieve the registered control based on the specified rendering
return getRegisteredControl(question.questionOptions.rendering);
}
Expand All @@ -44,3 +37,19 @@ export function hasMissingConcept(question: OHRIFormField) {
question.type == 'obs' && !question.questionOptions.concept && question.questionOptions.rendering !== 'fixed-value'
);
}

/**
* Make schema transformations especially for originally AFE schemas to match the RFE schema
*/
export function transformQuestion(question: OHRIFormField) {
switch (question.type) {
case 'encounterProvider':
question.questionOptions.rendering = 'encounter-provider';
break;
case 'encounterLocation':
question.questionOptions.rendering = 'encounter-location';
break;
default:
break;
}
}
8 changes: 6 additions & 2 deletions src/components/section/ohri-form-section.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ import { getRegisteredFieldSubmissionHandler } from '../../registry/registry';
import { OHRIUnspecified } from '../inputs/unspecified/ohri-unspecified.component';
import { OHRIFormField, OHRIFormFieldProps, previousValue, SubmissionHandler } from '../../api/types';
import styles from './ohri-form-section.scss';
import { getFieldControlWithFallback, isUnspecifiedSupported } from './helpers';
import { getFieldControlWithFallback, isUnspecifiedSupported, transformQuestion } from './helpers';
import { OHRITooltip } from '../inputs/tooltip/ohri-tooltip';
import { OHRIFormContext } from '../../ohri-form-context';
import { PreviousValueReview } from '../previous-value-review/previous-value-review.component';
import { isTrue } from '../../utils/boolean-utils';
import { formatDate } from '@openmrs/esm-framework';

interface FieldComponentMap {
fieldComponent: React.ComponentType<OHRIFormFieldProps>;
Expand All @@ -31,6 +32,7 @@ const OHRIFormSection = ({ fields, onFieldChange }) => {
useEffect(() => {
Promise.all(
fields.map(async (fieldDescriptor) => {
transformQuestion(fieldDescriptor);
const fieldComponent = await getFieldControlWithFallback(fieldDescriptor);
const handler = await getRegisteredFieldSubmissionHandler(fieldDescriptor.type);
return { fieldDescriptor, fieldComponent, handler };
Expand Down Expand Up @@ -111,7 +113,9 @@ const OHRIFormSection = ({ fields, onFieldChange }) => {
<PreviousValueReview
previousValue={previousFieldValue}
displayText={
fieldDescriptor.questionOptions.rendering == 'checkbox'
fieldDescriptor.questionOptions.rendering === 'date'
? formatDate(previousFieldValue)
: fieldDescriptor.questionOptions.rendering == 'checkbox'
? previousCheckboxDisplayValue
: previousFieldValue?.display
}
Expand Down
1 change: 0 additions & 1 deletion src/datasources/provider-datasource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ export class ProviderDataSource extends BaseOpenMRSDataSource {

return openmrsFetch(searchTerm ? `${apiUrl}&q=${searchTerm}` : apiUrl).then(({ data }) => {
if (data.results) {
console.log(data.results);
return data.results;
}
return data;
Expand Down
12 changes: 9 additions & 3 deletions src/hooks/useInitialValues.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ describe('useInitialValues', () => {
const encounterDate = new Date();

afterEach(() => {
allFormFields.slice(0, 6).forEach(field => {
allFormFields.slice(0, 6).forEach((field) => {
delete field.value;
});
});
Expand All @@ -142,6 +142,8 @@ describe('useInitialValues', () => {
sessionMode: 'enter',
encounterDate: encounterDate,
setEncounterDate: jest.fn,
encounterProvider: '2c95f6f5-788e-4e73-9079-5626911231fa',
setEncounterProvider: jest.fn,
},
formFieldHandlers,
),
Expand Down Expand Up @@ -175,6 +177,8 @@ describe('useInitialValues', () => {
sessionMode: 'enter',
encounterDate: encounterDate,
setEncounterDate: jest.fn,
encounterProvider: '2c95f6f5-788e-4e73-9079-5626911231fa',
setEncounterProvider: jest.fn,
},
formFieldHandlers,
),
Expand All @@ -200,8 +204,8 @@ describe('useInitialValues', () => {
date_of_birth_1: new Date('2023-07-24T00:00:00.000+0000').toLocaleDateString('en-US'),
infant_name_1: ' TDB II',
});
expect(allFormFields.find(field => field.id === 'date_of_birth_1')).not.toBeNull();
expect(allFormFields.find(field => field.id === 'infant_name_1')).not.toBeNull();
expect(allFormFields.find((field) => field.id === 'date_of_birth_1')).not.toBeNull();
expect(allFormFields.find((field) => field.id === 'infant_name_1')).not.toBeNull();
});

it('should verify that the "isBindingComplete" flag is set to true only when the resolution of calculated values are completed', async () => {
Expand Down Expand Up @@ -233,6 +237,8 @@ describe('useInitialValues', () => {
sessionMode: 'enter',
encounterDate: encounterDate,
setEncounterDate: jest.fn,
encounterProvider: '2c95f6f5-788e-4e73-9079-5626911231fa',
setEncounterProvider: jest.fn,
},
formFieldHandlers,
),
Expand Down
1 change: 0 additions & 1 deletion src/ohri-form-context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ type OHRIFormContextProps = {
values: Record<string, any>;
setFieldValue: (field: string, value: any, shouldValidate?: boolean) => void;
setEncounterLocation: (value: any) => void;
setEncounterProvider: (value: any) => void;
obsGroupsToVoid: Array<any>;
setObsGroupsToVoid: (value: any) => void;
encounterContext: EncounterContext;
Expand Down
2 changes: 1 addition & 1 deletion src/registry/inbuilt-components/control-templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export const controlTemplates: Array<ControlTemplate> = [
},
},
{
name: 'encounterProvider',
name: 'encounter-provider',
datasource: {
name: 'provider_datasource',
},
Expand Down
2 changes: 1 addition & 1 deletion src/registry/inbuilt-components/template-component-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const templateToComponentMap = [
baseControlComponent: UISelectExtended,
},
{
name: 'encounterProvider',
name: 'encounter-provider',
baseControlComponent: UISelectExtended,
},
];
6 changes: 4 additions & 2 deletions src/submission-handlers/base-handlers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ const encounterContext: EncounterContext = {
},
sessionMode: 'enter',
encounterDate: new Date(2020, 11, 29),
setEncounterDate: value => {},
setEncounterDate: (value) => {},
encounterProvider: '2c95f6f5-788e-4e73-9079-5626911231fa',
setEncounterProvider: jest.fn,
};

describe('ObsSubmissionHandler - handleFieldSubmission', () => {
Expand Down Expand Up @@ -823,7 +825,7 @@ describe('ObsSubmissionHandler - getInitialValue', () => {
const originalModule = jest.requireActual('../api/api');
return {
getConcept: jest.fn(() => {
return new Observable(sub => {
return new Observable((sub) => {
sub.next({
uuid: '1492AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA',
display: 'Ever tested positive for HIV before?',
Expand Down
6 changes: 6 additions & 0 deletions src/submission-handlers/base-handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,12 @@ export const EncounterLocationSubmissionHandler: SubmissionHandler = {
getDisplayValue: (field: OHRIFormField, value) => {
return value.display;
},
getPreviousValue: (field: OHRIFormField, encounter: any, allFormFields: Array<OHRIFormField>) => {
return {
display: encounter.location.name,
uuid: encounter.location.uuid,
};
},
};

///////////////////////////////
Expand Down
6 changes: 3 additions & 3 deletions src/submission-handlers/encounterDatetimeHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ export const EncounterDatetimeHandler: SubmissionHandler = {
return value;
},
getInitialValue: (encounter: OpenmrsEncounter, field: OHRIFormField, allFormFields?: OHRIFormField[]) => {
return new Date(); // TO DO: pick it from the visit if present
return encounter.encounterDatetime ? new Date(encounter.encounterDatetime) : new Date();
},

getDisplayValue: (field: OHRIFormField, value: any) => {
return field.value ? field.value : null;
},
getPreviousValue: (field: OHRIFormField, value: any) => {
return null;
getPreviousValue: (field: OHRIFormField, encounter: OpenmrsEncounter, allFormFields?: OHRIFormField[]) => {
return new Date(encounter.encounterDatetime);
},
};
7 changes: 4 additions & 3 deletions src/submission-handlers/encounterProviderHandler.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { SubmissionHandler } from '..';
import { getEncounterProviders } from '../api/api';
import { OpenmrsEncounter, OHRIFormField } from '../api/types';
import { EncounterContext } from '../ohri-form-context';

Expand All @@ -9,7 +8,8 @@ export const EncounterProviderHandler: SubmissionHandler = {
return value;
},
getInitialValue: (encounter: OpenmrsEncounter, field: OHRIFormField, allFormFields?: OHRIFormField[]) => {
return new Date(); // TO DO: pick it from the visit if present
const encounterProvider = encounter.encounterProviders[0]?.provider;
return encounterProvider ? encounterProvider.uuid : null;
},

getDisplayValue: (field: OHRIFormField, value: any) => {
Expand All @@ -19,6 +19,7 @@ export const EncounterProviderHandler: SubmissionHandler = {
return value;
},
getPreviousValue: (field: OHRIFormField, encounter: OpenmrsEncounter, allFormFields: Array<OHRIFormField>) => {
return null;
const encounterProvider = encounter.encounterProviders[0]?.provider;
return encounterProvider ? { value: encounterProvider.uuid, display: encounterProvider.name } : null;
},
};

0 comments on commit 0b4843d

Please sign in to comment.