Skip to content
Open
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@
"react-google-recaptcha": "^3.1.0",
"react-hook-form": "^7.55.0",
"react-i18next": "^15.2.0",
"react-intersection-observer": "^9.15.1",
"react-intersection-observer": "^10.0.3",
"react-pdf": "^9.2.1",
"react-phone-number-input": "^3.4.12",
"react-resizable-panels": "^3.0.2",
Expand Down
62 changes: 50 additions & 12 deletions src/components/Medicine/PrescriptionListSelector.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { cn } from "@/lib/utils";
import { useQuery } from "@tanstack/react-query";
import * as React from "react";

import { CardListSkeleton } from "@/components/Common/SkeletonLoading";
Expand All @@ -18,8 +17,10 @@ import prescriptionApi from "@/types/emr/prescription/prescriptionApi";
import { TagConfig } from "@/types/emr/tagConfig/tagConfig";
import query from "@/Utils/request/query";
import { formatDateTime, formatName } from "@/Utils/utils";
import { useInfiniteQuery } from "@tanstack/react-query";
import { ChevronDown, ReceiptTextIcon } from "lucide-react";
import { useTranslation } from "react-i18next";
import { useOnInView } from "react-intersection-observer";
Comment thread
NikhilA8606 marked this conversation as resolved.
Comment thread
NikhilA8606 marked this conversation as resolved.

function PrescriptionTags({ tags }: { tags?: TagConfig[] }) {
if (!tags || tags.length === 0) return null;
Expand Down Expand Up @@ -47,6 +48,8 @@ interface PrescriptionListSelectorProps {
onSelectPrescription: (prescription: PrescritionList | undefined) => void;
}

const PAGE_LIMIT = 14;
Comment thread
NikhilA8606 marked this conversation as resolved.
Outdated

Comment thread
NikhilA8606 marked this conversation as resolved.
export default function PrescriptionListSelector({
patientId,
encounterId,
Expand All @@ -56,13 +59,38 @@ export default function PrescriptionListSelector({
}: PrescriptionListSelectorProps) {
const { t } = useTranslation();
const [openDrawer, setOpenDrawer] = React.useState(false);
const { data: prescriptions, isLoading } = useQuery({
queryKey: ["prescriptions", patientId, encounterId],
queryFn: query(prescriptionApi.list, {
pathParams: { patientId },
queryParams: { encounter: encounterId, facility: facilityId },
}),
enabled: !!patientId && !!encounterId,

const { data, isLoading, fetchNextPage, hasNextPage, isFetchingNextPage } =
useInfiniteQuery({
queryKey: ["infinite-prescriptions", patientId, encounterId, facilityId],
queryFn: async ({ pageParam = 0, signal }) => {
Comment thread
NikhilA8606 marked this conversation as resolved.
Comment thread
NikhilA8606 marked this conversation as resolved.
const response = await query(prescriptionApi.list, {
pathParams: { patientId },
queryParams: {
encounter: encounterId,
facility: facilityId,
limit: String(PAGE_LIMIT),
offset: String(pageParam),
},
})({
signal,
});
return response;
},
enabled: !!patientId && !!encounterId,
initialPageParam: 0,
getNextPageParam: (lastPage, allPages) => {
const currentOffset = allPages.length * PAGE_LIMIT;
return currentOffset < lastPage.count ? currentOffset : null;
},
});

const prescriptions = data?.pages.flatMap((page) => page.results) ?? [];

const loadMoreRef = useOnInView<HTMLDivElement>((inView) => {
if (inView && hasNextPage) {
fetchNextPage();
}
});
Comment thread
NikhilA8606 marked this conversation as resolved.
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Comment thread
NikhilA8606 marked this conversation as resolved.
Comment thread
Copilot marked this conversation as resolved.
Comment thread
NikhilA8606 marked this conversation as resolved.
Comment thread
NikhilA8606 marked this conversation as resolved.

const handleSelectPrescription = React.useCallback(
Expand All @@ -82,7 +110,7 @@ export default function PrescriptionListSelector({
}

const selectedPrescription = selectedPrescriptionId
? prescriptions?.results.find((pres) => pres.id === selectedPrescriptionId)
? prescriptions.find((pres) => pres.id === selectedPrescriptionId)
: undefined;
Comment thread
NikhilA8606 marked this conversation as resolved.

const isAllSelected = selectedPrescriptionId === undefined;
Expand All @@ -91,9 +119,11 @@ export default function PrescriptionListSelector({
<>
<div className="hidden lg:block h-full overflow-y-auto pr-1">
<PrescriptionList
prescriptions={prescriptions?.results ?? []}
prescriptions={prescriptions}
selectedPrescriptionId={selectedPrescriptionId}
onSelectPrescription={onSelectPrescription}
ref={loadMoreRef}
isFetchingNextPage={isFetchingNextPage}
/>
</div>
<div className="lg:hidden">
Expand Down Expand Up @@ -154,7 +184,9 @@ export default function PrescriptionListSelector({
</DrawerHeader>
<div className="overflow-y-auto pr-2">
<PrescriptionList
prescriptions={prescriptions?.results ?? []}
ref={loadMoreRef}
isFetchingNextPage={isFetchingNextPage}
prescriptions={prescriptions}
selectedPrescriptionId={selectedPrescriptionId}
onSelectPrescription={handleSelectPrescription}
/>
Expand All @@ -176,10 +208,14 @@ function PrescriptionList({
prescriptions,
selectedPrescriptionId,
onSelectPrescription,
isFetchingNextPage,
ref,
}: {
prescriptions: PrescritionList[];
selectedPrescriptionId: string | undefined;
onSelectPrescription: (prescription: PrescritionList | undefined) => void;
isFetchingNextPage: boolean;
ref: React.Ref<HTMLDivElement>;
}) {
const { t } = useTranslation();

Expand All @@ -199,7 +235,7 @@ function PrescriptionList({

return (
<div className="space-y-2 p-2">
{items.map((item) => {
{items.map((item, i) => {
const isSelected = selectedPrescriptionId === item.id;

return (
Expand All @@ -214,6 +250,7 @@ function PrescriptionList({
onClick={() =>
onSelectPrescription(prescriptions.find((p) => p.id === item.id))
}
ref={i === items.length - 1 ? ref : undefined}
>
{isSelected && (
<div className="absolute right-0 h-8 w-1 bg-primary-600 rounded-l inset-y-1/2 -translate-y-1/2" />
Expand All @@ -240,6 +277,7 @@ function PrescriptionList({
</Card>
);
})}
{isFetchingNextPage && <CardListSkeleton count={5} />}
</div>
);
}
Loading