From 5a31e0aa19052b4f3fb0cbaad586b6744b3434f3 Mon Sep 17 00:00:00 2001 From: make-github-pseudonymous-again <5165674+make-github-pseudonymous-again@users.noreply.github.com> Date: Tue, 10 Dec 2024 14:33:42 +0100 Subject: [PATCH] :recycle: refactor: Rewrite `` for readability. --- imports/lib/array/removeUndefined.ts | 6 ++ .../appointments/AppointmentsForPatient.tsx | 74 +++++++++---------- 2 files changed, 42 insertions(+), 38 deletions(-) create mode 100644 imports/lib/array/removeUndefined.ts diff --git a/imports/lib/array/removeUndefined.ts b/imports/lib/array/removeUndefined.ts new file mode 100644 index 000000000..45e0a78de --- /dev/null +++ b/imports/lib/array/removeUndefined.ts @@ -0,0 +1,6 @@ +const removeUndefined = (array: T) => + array.filter((value: T) => value !== undefined) as { + [K in keyof T]: T[K] extends {} ? T[K] : never; + }; + +export default removeUndefined; diff --git a/imports/ui/appointments/AppointmentsForPatient.tsx b/imports/ui/appointments/AppointmentsForPatient.tsx index a11df5a4d..75edb76af 100644 --- a/imports/ui/appointments/AppointmentsForPatient.tsx +++ b/imports/ui/appointments/AppointmentsForPatient.tsx @@ -1,10 +1,13 @@ -import React, {useState} from 'react'; +import React, {useState, useMemo} from 'react'; import PhoneDisabledIcon from '@mui/icons-material/PhoneDisabled'; import AlarmOffIcon from '@mui/icons-material/AlarmOff'; import startOfToday from 'date-fns/startOfToday'; +import removeUndefinedValuesFromArray from '../../lib/array/removeUndefined'; +import removeUndefinedValuesFromObject from '../../lib/object/removeUndefined'; + import usePatient from '../patients/usePatient'; import Loading from '../navigation/Loading'; @@ -18,6 +21,32 @@ type Props = { readonly patientId: string; }; +const _sort = {datetime: -1} as const; + +const _filter = (patientId: string, {showCancelled, showNoShow}) => { + return { + patientId, + isDone: false, + $or: removeUndefinedValuesFromArray([ + removeUndefinedValuesFromObject({ + isCancelled: { + $in: [false, null!], + }, + scheduledDatetime: showNoShow + ? undefined + : { + $ge: startOfToday(), // TODO make reactive? + }, + }), + showCancelled + ? { + isCancelled: true, + } + : undefined, + ]), + }; +}; + const AppointmentsForPatient = ({patientId}: Props) => { const [showCancelled, setShowCancelled] = useState(true); const [showNoShow, setShowNoShow] = useState(true); @@ -28,6 +57,11 @@ const AppointmentsForPatient = ({patientId}: Props) => { [patientId], ); + const filter = useMemo( + () => _filter(patientId, {showCancelled, showNoShow}), + [patientId, showCancelled, showNoShow], + ); + if (loading) { return ; } @@ -36,48 +70,12 @@ const AppointmentsForPatient = ({patientId}: Props) => { return Patient not found.; } - const $or: Array<{ - isCancelled: boolean | {$in: boolean[]}; - scheduledDatetime?: {$gt: Date}; - }> = [ - { - isCancelled: { - $in: [false, null!], - }, - scheduledDatetime: { - $gt: startOfToday(), // TODO make reactive? - }, - }, - ]; - - const filter = { - patientId, - isDone: false, - $or, - }; - - if (showCancelled) { - $or.push({ - isCancelled: true, - }); - } - - if (showNoShow) { - $or.push({ - isCancelled: { - $in: [false, null!], - }, - }); - } - - const sort = {datetime: -1}; - return ( <>