From d259c205ab130ed313e930d0eb0482ec5e3fb51f Mon Sep 17 00:00:00 2001 From: Ashesh <3626859+Ashesh3@users.noreply.github.com> Date: Fri, 2 Feb 2024 16:46:57 +0530 Subject: [PATCH] Fix investigation report pagination when first page is empty (#7153) --- .../Facility/Investigations/Reports/index.tsx | 50 ++++++++++--------- 1 file changed, 27 insertions(+), 23 deletions(-) diff --git a/src/Components/Facility/Investigations/Reports/index.tsx b/src/Components/Facility/Investigations/Reports/index.tsx index b868e8e8af0..6800a6208aa 100644 --- a/src/Components/Facility/Investigations/Reports/index.tsx +++ b/src/Components/Facility/Investigations/Reports/index.tsx @@ -95,7 +95,6 @@ const InvestigationReports = ({ id }: any) => { const [page, setPage] = useState(1); const [sessionPage, setSessionPage] = useState(1); const [isNextSessionDisabled, setIsNextSessionDisabled] = useState(false); - const [isLoadMoreDisabled, setIsLoadMoreDisabled] = useState(false); const [patientDetails, setPatientDetails] = useState<{ name: string; age: number; @@ -136,6 +135,17 @@ const InvestigationReports = ({ id }: any) => { .slice(pageStart, pageStart + RESULT_PER_PAGE) .join(","); + if (investigationsParams.length === 0) { + Notification.Error({ + msg: "No more reports to load", + }); + dispatch({ + type: "set_loading", + payload: { ...isLoading, tableData: false }, + }); + return; + } + dispatchAction( getPatientInvestigation( { @@ -145,15 +155,13 @@ const InvestigationReports = ({ id }: any) => { id ) ).then((res: any) => { + dispatch({ + type: "set_loading", + payload: { ...isLoading, tableData: false }, + }); if (res?.data?.results) { onSuccess(res.data, curPage); setPage(curPage + 1); - if (res.data.results.length !== 0 || curPage >= totalPage) { - dispatch({ - type: "set_loading", - payload: { ...isLoading, tableData: false }, - }); - } } }); }, @@ -249,7 +257,7 @@ const InvestigationReports = ({ id }: any) => { }, []); // eslint-disable-next-line - const handleLoadMore = (e: any) => { + const handleLoadMore = () => { const onSuccess = (data: any, pageNo: number) => { if (data.results.length === 0 && pageNo + 1 <= totalPage) { fetchInvestigationsData(onSuccess, pageNo + 1, sessionPage); @@ -270,20 +278,16 @@ const InvestigationReports = ({ id }: any) => { if (curSessionPage > 1 && !data.results.length) { setSessionPage(curSessionPage - 1); setIsNextSessionDisabled(true); - setIsLoadMoreDisabled(true); } else { setIsNextSessionDisabled(false); - setIsLoadMoreDisabled(false); - if (!data.results.length) { - Notification.Error({ - msg: "No Investigation data available!", + handleLoadMore(); + } else { + dispatch({ + type: "set_investigation_table_data", + payload: data.results, }); } - dispatch({ - type: "set_investigation_table_data", - payload: data.results, - }); } document.getElementById("reports_section")?.scrollIntoView(); @@ -304,8 +308,7 @@ const InvestigationReports = ({ id }: any) => { handleGenerateReports(count); }; - const loadMoreDisabled = - page - 1 >= totalPage || isLoading.tableData || isLoadMoreDisabled; + const loadMoreDisabled = page - 1 >= totalPage || isLoading.tableData; const getTestDisabled = !selectedGroup.length || isLoading.tableData || @@ -391,6 +394,11 @@ const InvestigationReports = ({ id }: any) => { )} + {isLoading.tableData && ( +
+ +
+ )}
{!!investigationTableData.length && ( <> @@ -415,10 +423,6 @@ const InvestigationReports = ({ id }: any) => { patientDetails={patientDetails} /> - {!!isLoading.tableData && ( - - )} - {!loadMoreDisabled && (