Skip to content

Commit 175dca4

Browse files
committed
wip: Pagination
1 parent 02298c9 commit 175dca4

File tree

6 files changed

+38
-42
lines changed

6 files changed

+38
-42
lines changed

.eslintrc.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ module.exports = {
155155
},
156156
],
157157
'unicorn/filename-case': [
158-
1,
158+
0,
159159
{
160160
cases: {
161161
kebabCase: true,

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ npm run dev
4747
## Todo
4848

4949
- pagination - https://tailwindow.github.io/component-tailwindcss/components/button/pagination/index.html#
50+
https://ahooks.js.org/hooks/use-pagination
5051
- get rid of class-validator (use zod)
5152
- switch to react location
5253
- https://realworld-docs.netlify.app/docs/specs/backend-specs/endpoints

src/app/App.tsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { SessionServiceInterface, Tokens } from '@application';
44
import { Footer, Loader, Loading, Navbar } from '@components';
55
import { inject } from 'njct';
66
import React, { Suspense } from 'react';
7-
import { HashRouter, Route, Routes } from 'react-router-dom';
7+
import { BrowserRouter, Route, Routes } from 'react-router-dom';
88
import { RecoilRoot } from 'recoil';
99

1010
import { AppErrorBoundary } from './AppErrorBoundary';
@@ -27,7 +27,7 @@ export function App() {
2727
<React.StrictMode>
2828
<RecoilRoot>
2929
<AppErrorBoundary>
30-
<HashRouter>
30+
<BrowserRouter>
3131
<Navbar user={user} />
3232
<Routes>
3333
<Route path="/" element={<Home />} />
@@ -44,7 +44,7 @@ export function App() {
4444
/>
4545
</Routes>
4646
<Footer />
47-
</HashRouter>
47+
</BrowserRouter>
4848
</AppErrorBoundary>
4949
<Loader />
5050
</RecoilRoot>

src/app/home/useHome.ts

+23-15
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,31 @@ import { Tokens } from '@application';
22
import { ArticleServiceInterface } from '@application/article';
33
import { useRequest } from 'ahooks';
44
import { inject } from 'njct';
5+
import { useEffect } from 'react';
56
import { useSearchParams } from 'react-router-dom';
67

78
export function useHome() {
8-
const [searchParams, setSearchParams] = useSearchParams();
9-
const { data: articleList } = useRequest(async () => {
10-
const page = Number(searchParams.get('page') || 1);
11-
const take = 5;
12-
const skip = take * (page - 1);
13-
const articleService = inject<ArticleServiceInterface>(
14-
Tokens.ArticleService,
15-
);
16-
const result = await articleService.findMany({
17-
skip,
18-
take,
19-
});
20-
return result.unwrap();
21-
});
9+
const [searchParams] = useSearchParams();
10+
const { data, run } = useRequest(
11+
async () => {
12+
const page = Number(searchParams.get('page') || 1);
13+
const take = 5;
14+
const skip = take * (page - 1);
15+
const articleService = inject<ArticleServiceInterface>(
16+
Tokens.ArticleService,
17+
);
18+
const result = await articleService.findMany({
19+
skip,
20+
take,
21+
});
22+
return result.unwrap();
23+
},
24+
{ manual: true },
25+
);
2226

23-
return { articleList };
27+
useEffect(() => {
28+
run();
29+
}, [run, searchParams]);
30+
31+
return { articleList: data };
2432
}

src/components/Pagination/PageItem.tsx

+7-14
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,26 @@
11
import classNames from 'clsx';
2-
import React, { useCallback } from 'react';
2+
import React from 'react';
33
import { NavLink, useSearchParams } from 'react-router-dom';
44

55
type PageItemProps = {
66
active?: boolean;
7-
link: number | string;
87
page: number;
98
};
109

1110
export function PageItem(props: PageItemProps) {
12-
const { active, page, link } = props;
13-
const [searchParams, setSearchParams] = useSearchParams();
14-
const clickHandler = useCallback(() => {
15-
searchParams.set('page', String(page));
16-
setSearchParams(searchParams);
17-
}, [page, searchParams, setSearchParams]);
11+
const { active, page } = props;
12+
const [searchParams] = useSearchParams();
13+
14+
searchParams.set('page', page.toString());
1815

1916
return (
2017
<li
2118
className={classNames('page-item', {
2219
active,
2320
})}
2421
>
25-
<NavLink
26-
className="page-link"
27-
onClick={clickHandler}
28-
to={`?page=${page}`}
29-
>
30-
{link}
22+
<NavLink className="page-link" to={`?${searchParams.toString()}`}>
23+
{page}
3124
</NavLink>
3225
</li>
3326
);

src/components/Pagination/Pagination.tsx

+3-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useState } from 'react';
1+
import React from 'react';
22

33
import { PageItem } from './PageItem';
44

@@ -8,17 +8,11 @@ type PaginationProps = {
88
};
99

1010
export function Pagination(props: PaginationProps) {
11-
const { total, current = 1 } = props;
12-
const [currentLink, setCurrentLink] = useState(current);
13-
1411
return (
1512
<nav>
1613
<ul className="pagination">
17-
<PageItem link="First" page={1} />
18-
<PageItem link="Previous" page={current - 1} />
19-
<PageItem link={current} page={current} />
20-
<PageItem link="Next" page={current + 1} />
21-
<PageItem link="Last" />
14+
<PageItem page={1} />
15+
<PageItem page={2} />
2216
</ul>
2317
</nav>
2418
);

0 commit comments

Comments
 (0)