Skip to content

Commit

Permalink
Fixes:abruptly-discharged-page-jumping-to-page1
Browse files Browse the repository at this point in the history
  • Loading branch information
Sulochan-khadka committed Oct 10, 2024
1 parent 8e6a2f2 commit 5941563
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions src/Components/Common/Pagination.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useState } from "react";
import { useEffect, useState } from "react";
import CareIcon from "../../CAREUI/icons/CareIcon";
import { useAbortableEffect, statusType } from "../../Common/utils";
import ButtonV2 from "./components/ButtonV2";
Expand Down Expand Up @@ -33,7 +33,14 @@ const Pagination = ({
},
[defaultPerPage, cPage],
);

useEffect(() => {
const urlParams = new URLSearchParams(window.location.search);
const pageFromUrl = parseInt(urlParams.get("page") || "1", 10);
if (pageFromUrl !== currentPage) {
setCurrentPage(pageFromUrl);
onChange(pageFromUrl, rowsPerPage);
}
}, [onChange, rowsPerPage, currentPage]);
const getPageNumbers = () => {
const totalPage = Math.ceil(data.totalCount / rowsPerPage);
if (totalPage === 0) return [1];
Expand Down Expand Up @@ -77,12 +84,17 @@ const Pagination = ({
const goToPage = (page: number) => {
setCurrentPage(page);
onChange(page, rowsPerPage);
const url = new URL(window.location.href);
url.searchParams.set("page", page.toString());
window.history.pushState({}, "", url.toString());
const pageContainer = window.document.getElementById("pages");
pageContainer?.scroll({
top: 0,
left: 0,
behavior: "smooth",
});
if (pageContainer) {
pageContainer.scroll({
top: 0,
left: 0,
behavior: "smooth",
});
}
};

return (
Expand Down

0 comments on commit 5941563

Please sign in to comment.