Skip to content

Commit

Permalink
Changed implementation to remove extra slot in prescription table
Browse files Browse the repository at this point in the history
  • Loading branch information
print-Sathvik committed Sep 28, 2023
1 parent 07a6035 commit 6806b9c
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 74 deletions.
4 changes: 2 additions & 2 deletions src/Common/hooks/useRangePagination.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ const useRangePagination = ({ bounds, perPage, ...props }: Props) => {
for (let i = 0; i < props.slots; i++) {
if (props.snapToLatest) {
slots.push({
start: new Date(end.valueOf() - delta * (i - 1)),
end: new Date(end.valueOf() - delta * i),
start: new Date(end.valueOf() - delta * i),
end: new Date(end.valueOf() - delta * (i + 1)),
});
} else {
slots.push({
Expand Down
75 changes: 11 additions & 64 deletions src/Components/Medicine/PrescriptionAdministrationsTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ export default function PrescriptionAdministrationsTable({

const [showDiscontinued, setShowDiscontinued] = useState(false);
const [discontinuedCount, setDiscontinuedCount] = useState<number>();
const [latestAdministration, setLatestAdministration] = useState<Date>();

const pagination = useRangePagination({
bounds: state?.administrationsTimeBounds ?? {
Expand Down Expand Up @@ -79,42 +78,6 @@ export default function PrescriptionAdministrationsTable({
list(showDiscontinued ? filters : { ...filters, discontinued: false })
);

const prescriptions = res.data.results as Prescription[];
let latestDateAllPresc: Date | undefined;
prescriptions.forEach(async (presc, index) => {
const actions = prescription(presc.id ?? "");
const res = await dispatch(
actions.listAdministrations(
{
administered_date_after: formatDateTime(
pagination.slots[0].start,
"YYYY-MM-DD"
),
administered_date_before: formatDateTime(
pagination.slots[pagination.slots.length - 1].end,
"YYYY-MM-DD"
),
},
`toGetDate-${index}`
)
);
if (res.data.results.length > 0) {
const latestDateThisPresc = new Date(
res.data.results.reduce((prevMax: any, current: any) =>
prevMax.administered_date > current.administered_date
? prevMax
: current
).administered_date
);
latestDateThisPresc.setMinutes(0, 0, 0);
latestDateAllPresc =
latestDateAllPresc && latestDateAllPresc > latestDateThisPresc
? latestDateAllPresc
: latestDateThisPresc;
setLatestAdministration(latestDateThisPresc);
}
});

setState({
prescriptions: (res.data.results as Prescription[]).sort(
(a, b) => (a.discontinued ? 1 : 0) - (b.discontinued ? 1 : 0)
Expand All @@ -134,19 +97,6 @@ export default function PrescriptionAdministrationsTable({
refetch();
}, [refetch]);

useEffect(() => {
setState({
prescriptions: state?.prescriptions ?? [],
administrationsTimeBounds: {
start: state?.administrationsTimeBounds.start ?? new Date(),
end:
latestAdministration ??
state?.administrationsTimeBounds.end ??
new Date(),
},
});
}, [latestAdministration]);

return (
<div>
{state?.prescriptions && (
Expand Down Expand Up @@ -359,19 +309,16 @@ const PrescriptionRow = ({ prescription, ...props }: PrescriptionRowProps) => {

const getAdministrations = async () => {
const res = await dispatch(
props.actions.listAdministrations(
{
administered_date_after: formatDateTime(
props.intervals[0].start,
"YYYY-MM-DD"
),
administered_date_before: formatDateTime(
props.intervals[props.intervals.length - 1].end,
"YYYY-MM-DD"
),
},
`toSetRow-${prescription.id}`
)
props.actions.listAdministrations({
administered_date_after: formatDateTime(
props.intervals[0].start,
"YYYY-MM-DD"
),
administered_date_before: formatDateTime(
props.intervals[props.intervals.length - 1].end,
"YYYY-MM-DD"
),
})
);

setAdministrations(res.data.results);
Expand Down Expand Up @@ -662,10 +609,10 @@ function getAdministrationBounds(prescriptions: Prescription[]) {

// floor start to previous hour
start.setMinutes(0, 0, 0);
start.setHours(start.getHours() - 1);

// ceil end to next hour
end.setMinutes(0, 0, 0);
end.setHours(end.getHours() + 1);

return { start, end };
}
13 changes: 5 additions & 8 deletions src/Redux/actions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1040,19 +1040,16 @@ export const PrescriptionActions = (consultation_external_id: string) => {
`administer-medicine-${external_id}`
),

listAdministrations: (
query?: {
administered_date_after?: string;
administered_date_before?: string;
},
key?: string
) =>
listAdministrations: (query?: {
administered_date_after?: string;
administered_date_before?: string;
}) =>
fireRequest(
"listAdministrations",
[],
{ prescription: external_id, ...query },
pathParams,
`list-administrations-${external_id}-${key}`
`list-administrations-${external_id}`
),

/** Discontinue a prescription */
Expand Down

0 comments on commit 6806b9c

Please sign in to comment.