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

OHRI-1857 restrict edit to latest mch visit #1772

Draft
wants to merge 14 commits into
base: dev
Choose a base branch
from
Draft
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
83 changes: 83 additions & 0 deletions __mocks__/encounters.mocks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import { OpenmrsEncounter } from '@openmrs/openmrs-form-engine-lib/src/api/types';
export const mockEncounters: OpenmrsEncounter[] = [
{
uuid: '2d8969e4-0b6b-4091-b918-edea29ccbc6c',
encounterDatetime: '2023-10-24T12:38:00.000+0000',
encounterType: '',
location: '',
patient: '',
encounterProviders: [
{
uuid: '35ebbdfa-0eeb-44b8-8d00-5548ad5f0190',
provider: {
uuid: 'e3b1f354-da0e-40e3-b77b-493d8e86db3c',
name: 'Ff8e2 b400A Fcd3C',
},
},
],
obs: [],
form: {
uuid: '60252155-f40d-3084-9d8e-5b7c8fafc68a',
name: 'Labour & Delivery Form',
},
},
{
uuid: 'e61810e9-cab0-403d-aea0-a08c66d17317',
encounterDatetime: '2023-09-25T14:00:54.000+0000',
encounterType: '',
location: '',
patient: '',
encounterProviders: [],
obs: [],
form: {
uuid: '2105c8ae-1935-375c-a7cc-e2ca04c8f6be',
name: 'Mother - Postnatal Form',
},
},
{
uuid: '65f7acf8-c610-46d2-a836-142776365cf7',
encounterDatetime: '2023-09-25T13:58:47.000+0000',
encounterType: {
uuid: '2678423c-0523-4d76-b0da-18177b439eed',
display: 'Labor and Delivery',
name: 'Labor and Delivery',
description: 'Labor and delivery visit by a mother',
retired: false,
links: [
{
rel: 'self',
uri: 'http://ohri-namibia-dev.globalhealthapp.net/openmrs/ws/rest/v1/encountertype/2678423c-0523-4d76-b0da-18177b439eed',
resourceAlias: 'encountertype',
},
{
rel: 'full',
uri: 'http://ohri-namibia-dev.globalhealthapp.net/openmrs/ws/rest/v1/encountertype/2678423c-0523-4d76-b0da-18177b439eed?v=full',
resourceAlias: 'encountertype',
},
],
resourceVersion: '1.8',
},
location: {
uuid: 'Sf8ctjA7qjo',
name: 'Opuwo Clinic',
},
patient: {
uuid: '1ffd8210-0098-4a9a-8706-e87bac8d1b32',
display: '10350A230066 - Maria Niilenge',
},
encounterProviders: [
{
uuid: '751cf65d-6238-4f06-8245-c2d6d002d4e0',
provider: {
uuid: 'f677d8be-4961-4f37-a361-7f5f8977495d',
name: 'PTracker_PMTCT PTracker_PMTCT',
},
},
],
obs: [],
form: {
uuid: '60252155-f40d-3084-9d8e-5b7c8fafc68a',
name: 'Labour & Delivery Form',
},
},
];
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ import { OTable } from '../data-table/o-table.component';
import { Button, Link, OverflowMenu, OverflowMenuItem, Pagination, DataTableSkeleton } from '@carbon/react';
import { Add } from '@carbon/react/icons';
import { OHRIFormSchema } from '@openmrs/openmrs-form-engine-lib';
import { launchEncounterForm } from './helpers';
import { launchEncounterForm, findEncounterLatestDateIndex } from './helpers';
import { useEncounterRows } from '../../hooks/useEncounterRows';
import { OpenmrsEncounter } from '../../api/types';
import { useFormsJson } from '../../hooks/useFormsJson';
import { usePatientDeathStatus } from '../../hooks/usePatientDeathStatus';
import { fetchPatientLastEncounter } from '../../api/api';

export interface EncounterListColumn {
key: string;
Expand Down Expand Up @@ -40,6 +41,7 @@ export interface EncounterListProps {
workspaceWindowSize?: 'minimized' | 'maximized';
};
filter?: (encounter: any) => boolean;
disableEdit?: boolean;
}

export const EncounterList: React.FC<EncounterListProps> = ({
Expand All @@ -51,6 +53,7 @@ export const EncounterList: React.FC<EncounterListProps> = ({
formList,
filter,
launchOptions,
disableEdit,
}) => {
const { t } = useTranslation();
const [paginatedRows, setPaginatedRows] = useState([]);
Expand Down Expand Up @@ -131,7 +134,7 @@ export const EncounterList: React.FC<EncounterListProps> = ({
paginatedEncounters.push(encounters[i]);
}
}
const rows = paginatedEncounters.map((encounter) => {
const rows = paginatedEncounters.map((encounter, encounterIndex: number) => {
const tableRow: { id: string; actions: any } = { id: encounter.uuid, actions: null };
// inject launch actions
encounter['launchFormActions'] = {
Expand Down Expand Up @@ -198,6 +201,11 @@ export const EncounterList: React.FC<EncounterListProps> = ({
workspaceWindowSize,
);
}}
disabled={
index == 1 && disableEdit == true && findEncounterLatestDateIndex(encounters) != encounterIndex
}
// disabled={index == 1 && findEncounterLatestDateIndex(encounters) != encounterIndex}
// disabled={actionItem.label == 'Edit form' && findEncounterLatestDateIndex(encounters) != encounterIndex}
/>
))}
</OverflowMenu>
Expand Down
20 changes: 20 additions & 0 deletions packages/esm-commons-lib/src/components/encounter-list/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { OHRIFormSchema, SessionMode } from '@openmrs/openmrs-form-engine-lib';
import { launchForm } from '../../utils/ohri-forms-commons';
import { capitalize } from 'lodash-es';
import { OpenmrsEncounter } from '../../api/types';

type LaunchAction = 'add' | 'view' | 'edit';

Expand All @@ -26,3 +27,22 @@ export function launchEncounterForm(
workspaceWindowSize,
);
}

export const findEncounterLatestDateIndex = (encounters: OpenmrsEncounter[]) => {
if (!encounters || !encounters.length) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could use the inNull function from lodash

return;
}

let latestDateIndex: number = 0;

for (let i = 1; i < encounters?.length; i++) {
const currentDate = new Date(encounters[i].encounterDatetime);
const latestDate = new Date(encounters[latestDateIndex].encounterDatetime);

if (currentDate > latestDate) {
latestDateIndex = i;
}
}

return latestDateIndex;
};
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ const InfantPostnatalList: React.FC<InfantPostnatalListProps> = ({ patientUuid }
displayText: t('add', 'Add'),
moduleName: moduleName,
}}
disableEdit={true}
/>
);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ const AntenatalCareList: React.FC<AntenatalCareListProps> = ({ patientUuid }) =>
displayText: t('add', 'Add'),
moduleName: moduleName,
}}
disableEdit={true}
/>
);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ const LabourDeliveryList: React.FC<LabourDeliveryListProps> = ({ patientUuid })
displayText: t('add', 'Add'),
moduleName: moduleName,
}}
disableEdit={true}
/>
);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ const PostnatalCareList: React.FC<PostnatalCareListProps> = ({ patientUuid }) =>
displayText: t('add', 'Add'),
moduleName: moduleName,
}}
disableEdit={true}
/>
);
};
Expand Down