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 [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 startIndex = (currentPage - 1) * itemsPerPage;
|
||||
const endIndex = startIndex + itemsPerPage;
|
||||
return data.slice(startIndex, endIndex);
|
||||
}, [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), []);
|
||||
|
||||
return {
|
||||
@ -36,6 +43,7 @@ const usePagination = (data, itemsPerPage) => {
|
||||
currentItems,
|
||||
paginate,
|
||||
resetPage,
|
||||
setCurrentPage,
|
||||
};
|
||||
};
|
||||
|
||||
@ -125,9 +133,16 @@ const AttendanceLog = ({ handleModalData, searchTerm, organizationId }) => {
|
||||
resetPage,
|
||||
} = usePagination(filteredSearchData, 20);
|
||||
|
||||
// useEffect(() => {
|
||||
// resetPage();
|
||||
// }, [filteredSearchData]);
|
||||
|
||||
useEffect(() => {
|
||||
resetPage();
|
||||
}, [filteredSearchData]);
|
||||
if (currentPage > totalPages) {
|
||||
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(
|
||||
(msg) => {
|
||||
@ -144,10 +159,9 @@ const AttendanceLog = ({ handleModalData, searchTerm, organizationId }) => {
|
||||
record.id === msg.response.id ? { ...record, ...msg.response } : record
|
||||
);
|
||||
});
|
||||
resetPage();
|
||||
}
|
||||
},
|
||||
[selectedProject, dateRange, resetPage]
|
||||
[selectedProject, dateRange]
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
@ -214,7 +228,7 @@ const AttendanceLog = ({ handleModalData, searchTerm, organizationId }) => {
|
||||
{isLoading ? (
|
||||
<div
|
||||
className="d-flex justify-content-center align-items-center"
|
||||
style={{ minHeight: "50vh" }}
|
||||
style={{ minHeight: "50vh" }}
|
||||
>
|
||||
<SpinnerLoader />
|
||||
</div>
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user