diff --git a/src/Components/Shifting/ShiftingBoard.tsx b/src/Components/Shifting/ShiftingBoard.tsx index 3203b5027f5..84a4ac419d3 100644 --- a/src/Components/Shifting/ShiftingBoard.tsx +++ b/src/Components/Shifting/ShiftingBoard.tsx @@ -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({ @@ -268,7 +257,8 @@ export default function ShiftingBoard({ }: boardProps) { const containerRef = useRef(null); const [offset, setOffSet] = useState(0); - const [isLoading, setIsLoading] = useState({ board: "BOARD", more: false }); + const [pages, setPages] = useState[]>([]); + const [isLoading, setIsLoading] = useState(true); const [{ isOver }, drop] = useDrop(() => ({ accept: "shift-card", drop: (item: any) => { @@ -278,23 +268,24 @@ export default function ShiftingBoard({ }, collect: (monitor) => ({ isOver: !!monitor.isOver() }), })); - const [data, setData] = useState>(); - 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, @@ -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({ @@ -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) => ( @@ -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 (
- {data?.count || "0"} + {pages[0] ? pages[0].count : "..."}
- {isLoading.board ? ( + {pages[0]?.count > 0 + ? patientFilter(board) + : !isLoading && ( +

{t("no_patients_to_show")}

+ )} + {isLoading ? (
@@ -395,25 +388,13 @@ export default function ShiftingBoard({
- ) : data?.count ?? 0 > 0 ? ( - patientFilter(board) ) : ( -

{t("no_patients_to_show")}

+ pages.at(-1)?.next && ( + handlePagination()} className="m-2 block"> + Load More + + ) )} - {!isLoading.board && - (data?.count ?? 0) < (data?.results.length || 0) && - (isLoading.more ? ( -
- {t("loading")} -
- ) : ( - - ))}
);