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
Hiding the already enrolled lists completed
  • Loading branch information
vasharma05 committed Jul 16, 2021
commit 3d6883b968be7c3aaab6e4bd4408def613f6e187
54 changes: 34 additions & 20 deletions packages/esm-patient-list-app/src/AddPatientToList/index.tsx
Original file line number Diff line number Diff line change
@@ -13,17 +13,25 @@ import { toOmrsIsoString } from '@openmrs/esm-framework';
const AddPatient: React.FC<{ close: () => void; patientUuid: string }> = ({ close, patientUuid }) => {
const { t } = useTranslation();
const [searchValue, setSearchValue] = useState('');
const { loading, data } = usePatientListData(undefined, undefined, undefined, undefined);
const { loading, data } = usePatientListData(undefined);
const [selectedLists, setSelectedList] = useState({});

useEffect(() => {
const lists = {};
if (data) {
const lists = {};
data.map((patientList) => {
lists[patientList.uuid] = false;
lists[patientList.uuid] = {
visible: true,
selected: false,
};
});
getPatientListsForPatient(patientUuid).then((enrolledPatientLists) => {
enrolledPatientLists.forEach((patientList) => {
lists[patientList.cohort.uuid].visible = false;
});
setSelectedList(lists);
});
}
setSelectedList(lists);
}, [data]);

const searchResults = useMemo(() => {
@@ -42,21 +50,24 @@ const AddPatient: React.FC<{ close: () => void; patientUuid: string }> = ({ clos
const handleChange = useCallback((uuid, e) => {
setSelectedList((selectedLists) => ({
...selectedLists,
[uuid]: e,
[uuid]: {
...selectedLists[uuid],
selected: e,
},
}));
}, []);

const handleSubmit = useCallback(() => {
data.map((patientList) => {
if (selectedLists[patientList.uuid]) {
Object.keys(selectedLists).forEach((patientListUuid) => {
if (selectedLists[patientListUuid].selected) {
addPatientToList({
patient: patientUuid,
cohort: patientList.uuid,
cohort: patientListUuid,
startDate: toOmrsIsoString(new Date()),
});
}
});
}, []);
}, [selectedLists]);

return (
<div className={styles.modalContent}>
@@ -87,17 +98,20 @@ const AddPatient: React.FC<{ close: () => void; patientUuid: string }> = ({ clos
<p className="bx--label">Patient Lists</p>
{!loading && searchResults ? (
searchResults.length > 0 ? (
searchResults.map((patientList, ind) => (
<div key={ind} className={styles.checkbox}>
<Checkbox
key={ind}
onChange={(e) => handleChange(patientList.uuid, e)}
checked={selectedLists[patientList.uuid]}
labelText={patientList.name}
id={patientList.uuid}
/>
</div>
))
searchResults.map(
(patientList, ind) =>
selectedLists[patientList.uuid]?.visible && (
<div key={ind} className={styles.checkbox}>
<Checkbox
key={ind}
onChange={(e) => handleChange(patientList.uuid, e)}
checked={selectedLists[patientList.uuid]?.selected}
labelText={patientList.name}
id={patientList.uuid}
/>
</div>
),
)
) : (
<p className={styles.bodyLong01}>No patient list found</p>
)
4 changes: 2 additions & 2 deletions packages/esm-patient-list-app/src/patientListData/api.ts
Original file line number Diff line number Diff line change
@@ -89,10 +89,10 @@ export async function getPatientListsForPatient(patientUuid: string) {
results,
error,
}: {
results: Array<OpenmrsCohortMember>;
results: Array<{ cohort: OpenmrsCohortMember }>;
error: Error;
} = await (
Copy link
Contributor

Choose a reason for hiding this comment

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

await await?

Copy link
Contributor

Choose a reason for hiding this comment

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

(the data is already resolved - no need for the outer await)

Copy link
Member Author

Choose a reason for hiding this comment

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

Sure! I'll remove the outer one.

await openmrsFetch<{ results: Array<OpenmrsCohortMember>; error: Error }>(
await openmrsFetch<{ results: Array<{ cohort: OpenmrsCohortMember }>; error: Error }>(
`/ws/rest/v1/cohortm/cohortmember?patient=${patientUuid}&v=default`,
)
).data;