Skip to content

Commit

Permalink
Provide pagination to user pages
Browse files Browse the repository at this point in the history
The route should be adapted to manage
the pagination numbers for the user main
pages.

Signed-off-by: Carla Martinez <[email protected]>
  • Loading branch information
carma12 committed Jun 4, 2024
1 parent 1123a5c commit 7735501
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 6 deletions.
4 changes: 1 addition & 3 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,10 @@ import {
updateBrowserTitle,
} from "src/store/Global/routes-slice";
// Router DOM
import { useLocation, useNavigate } from "react-router-dom";
import { useLocation } from "react-router-dom";

const App: React.FunctionComponent = () => {
const dispatch = useAppDispatch();
const navigate = useNavigate();
const { pathname } = useLocation();

// When accessing to a given URL directly from the browser, the routing data is loaded
Expand Down Expand Up @@ -70,7 +69,6 @@ const App: React.FunctionComponent = () => {
if (currentTitle) {
dispatch(updateBrowserTitle(currentTitle));
}
navigate(pathname);
}, []);

// [API Call] Get initial data
Expand Down
22 changes: 21 additions & 1 deletion src/pages/ActiveUsers/ActiveUsers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,12 @@ import { SerializedError } from "@reduxjs/toolkit";
import useApiError from "src/hooks/useApiError";
import GlobalErrors from "src/components/errors/GlobalErrors";
import ModalErrors from "src/components/errors/ModalErrors";
// Navigation
import { useSearchParams } from "react-router-dom";

const ActiveUsers = () => {
const [searchParams, setSearchParams] = useSearchParams();

// Dispatch (Redux)
const dispatch = useAppDispatch();

Expand All @@ -80,7 +84,9 @@ const ActiveUsers = () => {

// Main states - what user can define / what we could use in page URL
const [searchValue, setSearchValue] = React.useState("");
const [page, setPage] = useState<number>(1);
const [page, setPage] = useState<number>(
parseInt(searchParams.get("p") || "1")
);
const [perPage, setPerPage] = useState<number>(10);
const [totalCount, setUsersTotalCount] = useState<number>(0);
const [searchDisabled, setSearchIsDisabled] = useState<boolean>(false);
Expand Down Expand Up @@ -157,6 +163,20 @@ const ActiveUsers = () => {
}
}, [userDataResponse]);

// Handle URLs with pagination
React.useEffect(() => {
let searchParamsNew = {};

if (page > 1) {
searchParamsNew = {
...searchParamsNew,
p: page.toString(),
};
}

setSearchParams(searchParamsNew, { replace: true });
}, [page]);

// Refresh button handling
const refreshUsersData = () => {
// Hide table
Expand Down
22 changes: 21 additions & 1 deletion src/pages/PreservedUsers/PreservedUsers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,12 @@ import { useGettingPreservedUserQuery } from "../../services/rpcUsers";
import useApiError from "src/hooks/useApiError";
import GlobalErrors from "src/components/errors/GlobalErrors";
import ModalErrors from "src/components/errors/ModalErrors";
// Navigation
import { useSearchParams } from "react-router-dom";

const PreservedUsers = () => {
const [searchParams, setSearchParams] = useSearchParams();

// Initialize stage users list (Redux)
const dispatch = useAppDispatch();

Expand All @@ -69,7 +73,9 @@ const PreservedUsers = () => {

// Main states - what user can define / what we could use in page URL
const [searchValue, setSearchValue] = React.useState("");
const [page, setPage] = useState<number>(1);
const [page, setPage] = useState<number>(
parseInt(searchParams.get("p") || "1")
);
const [perPage, setPerPage] = useState<number>(10);
const [totalCount, setUsersTotalCount] = useState<number>(0);
const [searchDisabled, setSearchIsDisabled] = useState<boolean>(false);
Expand Down Expand Up @@ -141,6 +147,20 @@ const PreservedUsers = () => {
}
}, [userDataResponse]);

// Handle URLs with pagination
React.useEffect(() => {
let searchParamsNew = {};

if (page > 1) {
searchParamsNew = {
...searchParamsNew,
p: page.toString(),
};
}

setSearchParams(searchParamsNew, { replace: true });
}, [page]);

// Refresh button handling
const refreshUsersData = () => {
// Hide table
Expand Down
22 changes: 21 additions & 1 deletion src/pages/StageUsers/StageUsers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,12 @@ import { useGettingStageUserQuery } from "../../services/rpcUsers";
import useApiError from "src/hooks/useApiError";
import GlobalErrors from "src/components/errors/GlobalErrors";
import ModalErrors from "src/components/errors/ModalErrors";
// Navigation
import { useSearchParams } from "react-router-dom";

const StageUsers = () => {
const [searchParams, setSearchParams] = useSearchParams();

// Initialize stage users list (Redux)
const dispatch = useAppDispatch();

Expand All @@ -70,7 +74,9 @@ const StageUsers = () => {

// Main states - what user can define / what we could use in page URL
const [searchValue, setSearchValue] = React.useState("");
const [page, setPage] = useState<number>(1);
const [page, setPage] = useState<number>(
parseInt(searchParams.get("p") || "1")
);
const [perPage, setPerPage] = useState<number>(10);
const [totalCount, setUsersTotalCount] = useState<number>(0);
const [searchDisabled, setSearchIsDisabled] = useState<boolean>(false);
Expand Down Expand Up @@ -142,6 +148,20 @@ const StageUsers = () => {
}
}, [userDataResponse]);

// Handle URLs with pagination
React.useEffect(() => {
let searchParamsNew = {};

if (page > 1) {
searchParamsNew = {
...searchParamsNew,
p: page.toString(),
};
}

setSearchParams(searchParamsNew, { replace: true });
}, [page]);

// Refresh button handling
const refreshUsersData = () => {
// Hide table
Expand Down

0 comments on commit 7735501

Please sign in to comment.