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

PRN prescriptions table set upto latest administration #6345

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
75 changes: 64 additions & 11 deletions src/Components/Medicine/PrescriptionAdministrationsTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ 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 @@ -78,6 +79,42 @@ 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 @@ -97,6 +134,19 @@ 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 @@ -309,16 +359,19 @@ 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"
),
})
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}`
)
);

setAdministrations(res.data.results);
Expand Down Expand Up @@ -609,10 +662,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: 8 additions & 5 deletions src/Redux/actions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1040,16 +1040,19 @@ export const PrescriptionActions = (consultation_external_id: string) => {
`administer-medicine-${external_id}`
),

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

/** Discontinue a prescription */
Expand Down
Loading