Skip to content

Commit

Permalink
Fix investigation report pagination when first page is empty (ohcnetw…
Browse files Browse the repository at this point in the history
  • Loading branch information
Ashesh3 authored Feb 2, 2024
1 parent 542263e commit d259c20
Showing 1 changed file with 27 additions and 23 deletions.
50 changes: 27 additions & 23 deletions src/Components/Facility/Investigations/Reports/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(
{
Expand All @@ -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 },
});
}
}
});
},
Expand Down Expand Up @@ -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);
Expand All @@ -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();
Expand All @@ -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 ||
Expand Down Expand Up @@ -391,6 +394,11 @@ const InvestigationReports = ({ id }: any) => {
</ButtonV2>
</>
)}
{isLoading.tableData && (
<div className="flex w-full justify-center">
<CircularProgress className="text-primary-500" />
</div>
)}
<section id="reports_section">
{!!investigationTableData.length && (
<>
Expand All @@ -415,10 +423,6 @@ const InvestigationReports = ({ id }: any) => {
patientDetails={patientDetails}
/>

{!!isLoading.tableData && (
<CircularProgress className="text-primary-500" />
)}

{!loadMoreDisabled && (
<ButtonV2
disabled={loadMoreDisabled}
Expand Down

0 comments on commit d259c20

Please sign in to comment.