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

MF-641: Patient List Modal #22

Closed
Prev Previous commit
Next Next commit
Change in structure of PatientListData
  • Loading branch information
vasharma05 committed Jul 12, 2021
commit 39fb226d891e79a73fc2c5f326474e6b809b6c16
58 changes: 58 additions & 0 deletions packages/esm-patient-list-app/src/patientListData/hooks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import React from 'react';
import { getPatientListMembers } from './mock';
import { PatientListBase, PatientListMember, State } from './types';
import { getAllPatientLists, OpenmrsCohort } from './api';

export function usePatientListData(redo: any, ...args: Parameters<typeof getAllPatientLists>) {
const [data, setData] = React.useState<State<Array<OpenmrsCohort & { id: string }>>>({
loading: true,
data: undefined,
error: undefined,
});

React.useEffect(() => {
setData({
loading: true,
data: undefined,
error: undefined,
});
getAllPatientLists(...args)
.then((y) => {
setData({
loading: false,
data: y.map((x) => ({ ...x, id: x.uuid })),
error: undefined,
});
})
.catch((err) => setData({ loading: false, data: undefined, error: err }));
}, [redo, ...args]);

return data;
}

export function useSingePatientListData(redo: any, ...args: Parameters<typeof getPatientListMembers>) {
const [data, setData] = React.useState<State<Array<PatientListMember>>>({
loading: true,
data: undefined,
error: undefined,
});

React.useEffect(() => {
setData({
loading: true,
data: undefined,
error: undefined,
});
getPatientListMembers(...args)
.then((data) => {
setData({
loading: false,
data,
error: undefined,
});
})
.catch((err) => setData({ loading: false, data: undefined, error: err }));
}, [redo, ...args]);

return data;
}
82 changes: 2 additions & 80 deletions packages/esm-patient-list-app/src/patientListData/index.ts
Original file line number Diff line number Diff line change
@@ -1,80 +1,2 @@
// export * from './mock';
import React from 'react';
import { getPatientListMembers } from './mock';
import { PatientListBase, PatientListMember } from './types';
import { getAllPatientLists, OpenmrsCohort } from './api';


interface LoadingState {
loading: true;
data: undefined;
error: undefined;
}

interface DataState<T> {
loading: false;
data: T;
error: undefined;
}

interface ErrorState {
loading: false;
data: undefined;
error: Error;
}

type State<T> = LoadingState | DataState<T> | ErrorState;

export function usePatientListData(redo: any, ...args: Parameters<typeof getAllPatientLists>) {
const [data, setData] = React.useState<State<Array<OpenmrsCohort & { id: string }>>>({
loading: true,
data: undefined,
error: undefined,
});

React.useEffect(() => {
setData({
loading: true,
data: undefined,
error: undefined,
});
getAllPatientLists(...args)
.then((y) => {
setData({
loading: false,
data: y.map((x) => ({ ...x, id: x.uuid })),
error: undefined,
});
})
.catch((err) => setData({ loading: false, data: undefined, error: err }));
}, [redo, ...args]);

return data;
}

export function useSingePatientListData(redo: any, ...args: Parameters<typeof getPatientListMembers>) {
const [data, setData] = React.useState<State<Array<PatientListMember>>>({
loading: true,
data: undefined,
error: undefined,
});

React.useEffect(() => {
setData({
loading: true,
data: undefined,
error: undefined,
});
getPatientListMembers(...args)
.then((data) => {
setData({
loading: false,
data,
error: undefined,
});
})
.catch((err) => setData({ loading: false, data: undefined, error: err }));
}, [redo, ...args]);

return data;
}
export * from './api';
export * from './hooks';
20 changes: 20 additions & 0 deletions packages/esm-patient-list-app/src/patientListData/types.ts
Original file line number Diff line number Diff line change
@@ -42,3 +42,23 @@ export interface PatientListMemberProperty {
export interface PatientListMemberFilter {
// ??
}

interface LoadingState {
loading: true;
data: undefined;
error: undefined;
}

interface DataState<T> {
loading: false;
data: T;
error: undefined;
}

interface ErrorState {
loading: false;
data: undefined;
error: Error;
}

export type State<T> = LoadingState | DataState<T> | ErrorState;
7 changes: 5 additions & 2 deletions packages/esm-patient-list-app/src/patientListList/index.tsx
Original file line number Diff line number Diff line change
@@ -3,7 +3,10 @@ import Add16 from '@carbon/icons-react/es/add/16';
import Button from 'carbon-components-react/lib/components/Button';
import { ExtensionSlot } from '@openmrs/esm-framework';

import { getAllPatientLists, updatePatientListDetails, usePatientListData } from '../patientListData';
import { getAllPatientLists, usePatientListData } from '../patientListData';
// Yet to be built
// import { updatePatientListDetails } from '../patientListData';

import Search from 'carbon-components-react/es/components/Search';
import Tab from 'carbon-components-react/es/components/Tab';
import Tabs from 'carbon-components-react/es/components/Tabs';
@@ -101,7 +104,7 @@ const PatientListList: React.FC = () => {
);

const setListStarred = React.useCallback((listUuid: string, star: boolean) => {
updatePatientListDetails(listUuid, { isStarred: star }).then(() => setChanged((c) => !c));
// updatePatientListDetails(listUuid, { isStarred: star }).then(() => setChanged((c) => !c));
}, []);

return (