Logs Tab Action Redirects Back to Page 1 Automatically
This commit is contained in:
parent
2109a5f1f1
commit
c975e54331
@ -20,14 +20,21 @@ import { SpinnerLoader } from "../common/Loader";
|
|||||||
|
|
||||||
const usePagination = (data, itemsPerPage) => {
|
const usePagination = (data, itemsPerPage) => {
|
||||||
const [currentPage, setCurrentPage] = useState(1);
|
const [currentPage, setCurrentPage] = useState(1);
|
||||||
const maxPage = Math.ceil(data.length / itemsPerPage);
|
// const maxPage = Math.ceil(data.length / itemsPerPage);
|
||||||
|
const maxPage = Math.max(1, Math.ceil(data.length / itemsPerPage));
|
||||||
const currentItems = useMemo(() => {
|
const currentItems = useMemo(() => {
|
||||||
const startIndex = (currentPage - 1) * itemsPerPage;
|
const startIndex = (currentPage - 1) * itemsPerPage;
|
||||||
const endIndex = startIndex + itemsPerPage;
|
const endIndex = startIndex + itemsPerPage;
|
||||||
return data.slice(startIndex, endIndex);
|
return data.slice(startIndex, endIndex);
|
||||||
}, [data, currentPage, itemsPerPage]);
|
}, [data, currentPage, itemsPerPage]);
|
||||||
|
|
||||||
const paginate = useCallback((pageNumber) => setCurrentPage(pageNumber), []);
|
// const paginate = useCallback((pageNumber) => setCurrentPage(pageNumber), []);
|
||||||
|
|
||||||
|
const paginate = useCallback((pageNumber) => {
|
||||||
|
// keep page within 1..maxPage
|
||||||
|
const p = Math.max(1, Math.min(pageNumber, maxPage));
|
||||||
|
setCurrentPage(p);
|
||||||
|
}, [maxPage]);
|
||||||
const resetPage = useCallback(() => setCurrentPage(1), []);
|
const resetPage = useCallback(() => setCurrentPage(1), []);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
@ -36,6 +43,7 @@ const usePagination = (data, itemsPerPage) => {
|
|||||||
currentItems,
|
currentItems,
|
||||||
paginate,
|
paginate,
|
||||||
resetPage,
|
resetPage,
|
||||||
|
setCurrentPage,
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -125,9 +133,16 @@ const AttendanceLog = ({ handleModalData, searchTerm, organizationId }) => {
|
|||||||
resetPage,
|
resetPage,
|
||||||
} = usePagination(filteredSearchData, 20);
|
} = usePagination(filteredSearchData, 20);
|
||||||
|
|
||||||
|
// useEffect(() => {
|
||||||
|
// resetPage();
|
||||||
|
// }, [filteredSearchData]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
resetPage();
|
if (currentPage > totalPages) {
|
||||||
}, [filteredSearchData]);
|
paginate(totalPages || 1);
|
||||||
|
}
|
||||||
|
// NOTE: do NOT force reset to page 1 here — keep the same page if still valid
|
||||||
|
}, [filteredSearchData, totalPages, currentPage, paginate]);
|
||||||
|
|
||||||
const handler = useCallback(
|
const handler = useCallback(
|
||||||
(msg) => {
|
(msg) => {
|
||||||
@ -144,10 +159,9 @@ const AttendanceLog = ({ handleModalData, searchTerm, organizationId }) => {
|
|||||||
record.id === msg.response.id ? { ...record, ...msg.response } : record
|
record.id === msg.response.id ? { ...record, ...msg.response } : record
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
resetPage();
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
[selectedProject, dateRange, resetPage]
|
[selectedProject, dateRange]
|
||||||
);
|
);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@ -214,7 +228,7 @@ const AttendanceLog = ({ handleModalData, searchTerm, organizationId }) => {
|
|||||||
{isLoading ? (
|
{isLoading ? (
|
||||||
<div
|
<div
|
||||||
className="d-flex justify-content-center align-items-center"
|
className="d-flex justify-content-center align-items-center"
|
||||||
style={{ minHeight: "50vh" }}
|
style={{ minHeight: "50vh" }}
|
||||||
>
|
>
|
||||||
<SpinnerLoader />
|
<SpinnerLoader />
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user