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

Fixes pagination for shifting board #8041

Merged
merged 2 commits into from
Jun 18, 2024
Merged
Changes from all commits
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
71 changes: 26 additions & 45 deletions src/Components/Shifting/ShiftingBoard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,6 @@ interface boardProps {
containerHeight: number;
}

const reduceLoading = (action: string, current: any) => {
switch (action) {
case "MORE":
return { ...current, more: true };
case "BOARD":
return { ...current, board: true };
case "COMPLETE":
return { board: false, more: false };
}
};

const ShiftCard = ({ shift, filter }: any) => {
const { wartime_shifting } = useConfig();
const [modalFor, setModalFor] = useState({
Expand Down Expand Up @@ -268,7 +257,8 @@ export default function ShiftingBoard({
}: boardProps) {
const containerRef = useRef<HTMLDivElement>(null);
const [offset, setOffSet] = useState(0);
const [isLoading, setIsLoading] = useState({ board: "BOARD", more: false });
const [pages, setPages] = useState<PaginatedResponse<IShift>[]>([]);
const [isLoading, setIsLoading] = useState(true);
const [{ isOver }, drop] = useDrop(() => ({
accept: "shift-card",
drop: (item: any) => {
Expand All @@ -278,23 +268,24 @@ export default function ShiftingBoard({
},
collect: (monitor) => ({ isOver: !!monitor.isOver() }),
}));
const [data, setData] = useState<PaginatedResponse<IShift>>();

useQuery(routes.listShiftRequests, {
const query = useQuery(routes.listShiftRequests, {
query: formatFilter({
...filterProp,
status: board,
}),
onResponse: ({ res, data: listShiftData }) => {
setIsLoading(false);
if (res?.ok && listShiftData) {
setData(listShiftData);
setPages((prev) => [...prev, listShiftData]);
}
setIsLoading((loading) => reduceLoading("COMPLETE", loading));
},
});

useEffect(() => {
setIsLoading((loading) => reduceLoading("BOARD", loading));
setPages([]);
setIsLoading(true);
query.refetch();
}, [
filterProp.facility,
filterProp.origin_facility,
Expand All @@ -316,7 +307,7 @@ export default function ShiftingBoard({
]);

const handlePagination = async () => {
setIsLoading((loading) => reduceLoading("MORE", loading));
setIsLoading(true);
setOffSet(offset + 14);
const { res, data: newPageData } = await request(routes.listShiftRequests, {
query: formatFilter({
Expand All @@ -326,18 +317,15 @@ export default function ShiftingBoard({
}),
});
if (res?.ok && newPageData) {
setData((prev) =>
prev
? { ...prev, results: [...prev.results, ...newPageData.results] }
: newPageData,
);
setPages((prev) => [...prev, newPageData]);
}
setIsLoading((loading) => reduceLoading("COMPLETE", loading));
setIsLoading(false);
};
const { t } = useTranslation();

const patientFilter = (filter: string) => {
return data?.results
return pages
.flatMap((p) => p.results)
.filter(({ status }) => status === filter)
.map((shift: any) => (
<ShiftCard key={`shift_${shift.id}`} shift={shift} filter={filter} />
Expand All @@ -350,7 +338,7 @@ export default function ShiftingBoard({
const { height } = container.getBoundingClientRect();
containerHeight < height && setContainerHeight(height);
}
}, [containerRef.current, data?.results.length]);
}, [containerRef.current, pages.flatMap((p) => p.results).length]);

return (
<div
Expand All @@ -376,12 +364,17 @@ export default function ShiftingBoard({
/>
</h3>
<span className="ml-2 rounded-lg bg-primary-500 px-2 text-white">
{data?.count || "0"}
{pages[0] ? pages[0].count : "..."}
</span>
</div>
</div>
<div ref={containerRef} className="mt-2 flex flex-col pb-2 text-sm">
{isLoading.board ? (
{pages[0]?.count > 0
? patientFilter(board)
: !isLoading && (
<p className="mx-auto p-4">{t("no_patients_to_show")}</p>
)}
{isLoading ? (
<div className="m-1">
<div className="mx-auto w-full max-w-sm rounded-md border border-gray-300 bg-white p-4 shadow">
<div className="flex animate-pulse space-x-4 ">
Expand All @@ -395,25 +388,13 @@ export default function ShiftingBoard({
</div>
</div>
</div>
) : data?.count ?? 0 > 0 ? (
patientFilter(board)
) : (
<p className="mx-auto p-4">{t("no_patients_to_show")}</p>
pages.at(-1)?.next && (
<ButtonV2 onClick={(_) => handlePagination()} className="m-2 block">
Load More
</ButtonV2>
)
)}
{!isLoading.board &&
(data?.count ?? 0) < (data?.results.length || 0) &&
(isLoading.more ? (
<div className="mx-auto my-4 rounded-md bg-gray-100 p-2 px-4 hover:bg-white">
{t("loading")}
</div>
) : (
<button
onClick={(_) => handlePagination()}
className="mx-auto my-4 rounded-md bg-gray-100 p-2 px-4 hover:bg-white"
>
More...
</button>
))}
</div>
</div>
);
Expand Down
Loading