diff --git a/app/frontend/src/components/Pagination/index.tsx b/app/frontend/src/components/Pagination/index.tsx index 90d61eda..15794164 100644 --- a/app/frontend/src/components/Pagination/index.tsx +++ b/app/frontend/src/components/Pagination/index.tsx @@ -10,6 +10,7 @@ type PaginationProps = { onClickPrev: () => void; onClickNext: () => void; onClickItem: React.MouseEventHandler; + className?: string; }; export function Pagination({ @@ -17,12 +18,13 @@ export function Pagination({ onClickPrev, onClickNext, onClickItem, + className, }: PaginationProps) { const [start, end] = get10UnitRange(currentPage); const array = createRangeArray(start, end); const arrow = ; return ( -
+
diff --git a/app/frontend/src/hooks/usePagination.ts b/app/frontend/src/hooks/usePagination.ts index 19ce0f64..546505aa 100644 --- a/app/frontend/src/hooks/usePagination.ts +++ b/app/frontend/src/hooks/usePagination.ts @@ -1,8 +1,26 @@ -import { useState } from 'react'; +import { useEffect, useState } from 'react'; +import { createSearchParams, useLocation, useNavigate } from 'react-router-dom'; + +const useNavigateSearch = () => { + const navigate = useNavigate(); + return (pathname: string, params: { page: string }) => + navigate({ pathname, search: `?${createSearchParams(params)}` }); +}; export const usePagination = () => { + const location = useLocation(); + const navigationSearch = useNavigateSearch(); + const [currentPage, setCurrentPage] = useState(1); + useEffect(() => { + const searchParams = new URLSearchParams(location.search); + const page = searchParams.get('page'); + if (page) { + setCurrentPage(Number(page)); + } + }, [location]); + const onClickNext = () => { setCurrentPage(currentPage + 1); }; @@ -12,7 +30,9 @@ export const usePagination = () => { }; const onClickItem = (e: React.MouseEvent) => { - setCurrentPage(Number(e.currentTarget.value)); + const page = e.currentTarget.value; + setCurrentPage(Number(page)); + navigationSearch('', { page }); }; return { currentPage, onClickItem, onClickNext, onClickPrev }; diff --git a/app/frontend/src/pages/Mogaco/MogacoList.tsx b/app/frontend/src/pages/Mogaco/MogacoList.tsx index 1567a38c..f024f19c 100644 --- a/app/frontend/src/pages/Mogaco/MogacoList.tsx +++ b/app/frontend/src/pages/Mogaco/MogacoList.tsx @@ -5,9 +5,13 @@ import { queryKeys } from '@/queries'; import * as styles from './MogacoList.css'; -export function MogacoList() { +type MogacoListProp = { + currentPage: number; +}; + +export function MogacoList({ currentPage }: MogacoListProp) { const { data: mogacoList, isLoading } = useQuery( - queryKeys.mogaco.list({ page: '1' }), + queryKeys.mogaco.list({ page: currentPage.toString() }), ); if (isLoading) { diff --git a/app/frontend/src/pages/Mogaco/index.css.ts b/app/frontend/src/pages/Mogaco/index.css.ts index fd6f48cf..1cf092a0 100644 --- a/app/frontend/src/pages/Mogaco/index.css.ts +++ b/app/frontend/src/pages/Mogaco/index.css.ts @@ -7,3 +7,7 @@ export const container = style({ maxWidth: '80rem', margin: '0 auto', }); + +export const pagination = style({ + marginTop: '2rem', +}); diff --git a/app/frontend/src/pages/Mogaco/index.tsx b/app/frontend/src/pages/Mogaco/index.tsx index 519b731c..a09ebf3d 100644 --- a/app/frontend/src/pages/Mogaco/index.tsx +++ b/app/frontend/src/pages/Mogaco/index.tsx @@ -1,12 +1,24 @@ +import { Pagination } from '@/components'; +import { usePagination } from '@/hooks'; + import * as styles from './index.css'; import { MogacoList } from './MogacoList'; import { MogacoListHeader } from './MogacoListHeader'; export function MogacoPage() { + const { currentPage, onClickItem, onClickNext, onClickPrev } = + usePagination(); return (
- + +
); }