Skip to content

Commit

Permalink
fix: initial pagination index (#1862)
Browse files Browse the repository at this point in the history
  • Loading branch information
therealemjy authored Nov 27, 2023
1 parent 5cc89b6 commit 1b3c4aa
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/components/Pagination/usePagination.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { useTranslation } from 'packages/translations';
import { useEffect, useState } from 'react';
import { Location, useLocation } from 'react-router-dom';

type PaginationProps = {
itemsCount: number;
onChange: (newPageIndex: number) => void;
initialPageIndex?: number;
itemsPerPageCount?: number;
};

const PAGES_TO_SHOW_COUNT = 4;

const getInitialPageIndex = () => {
const searchParams = new URLSearchParams(window.location.search);
const getInitialPageIndex = ({ location }: { location: Location }) => {
const searchParams = new URLSearchParams(location.search);
const pageParam = searchParams.get('page');
const initialPageIndex = pageParam ? +pageParam - 1 : 0;
return initialPageIndex;
Expand All @@ -20,7 +20,8 @@ const getInitialPageIndex = () => {
export function usePagination({ itemsCount, onChange, itemsPerPageCount = 10 }: PaginationProps) {
const { t } = useTranslation();

const [activePageIndex, setActivePageIndex] = useState(getInitialPageIndex());
const location = useLocation();
const [activePageIndex, setActivePageIndex] = useState(getInitialPageIndex({ location }));
const [pagesCount, setPagesCount] = useState(0);

/* calculating items per page count */
Expand Down

0 comments on commit 1b3c4aa

Please sign in to comment.