Skip to content

Commit dcf51a7

Browse files
authored
fix(twap): fix navigation for big count of orders (#6237)
1 parent 95d699a commit dcf51a7

File tree

2 files changed

+60
-34
lines changed

2 files changed

+60
-34
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { ReactNode } from 'react'
2+
3+
import { ArrowButton, PageButtonLink } from './styled'
4+
5+
interface PageNavigationButtonProps {
6+
index: number
7+
getPageUrl?(index: number): Partial<{ pathname: string; search: string }>
8+
goToPage(index: number): void
9+
children: ReactNode
10+
active?: boolean
11+
}
12+
13+
export function PageNavigationButton({
14+
index,
15+
active = false,
16+
getPageUrl,
17+
goToPage,
18+
children,
19+
}: PageNavigationButtonProps): ReactNode {
20+
return getPageUrl ? (
21+
<PageButtonLink to={getPageUrl(index)} $active={active}>
22+
{children}
23+
</PageButtonLink>
24+
) : (
25+
<ArrowButton onClick={() => goToPage(index)} $active={active}>
26+
{children}
27+
</ArrowButton>
28+
)
29+
}

apps/cowswap-frontend/src/modules/ordersTable/pure/OrdersTablePagination/index.tsx

Lines changed: 31 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import { useCallback, useMemo } from 'react'
1+
import { ReactNode, useCallback, useMemo } from 'react'
22

33
import { ChevronLeft, ChevronRight } from 'react-feather'
44

5-
import { ArrowButton, BlankButton, PageButton, PageButtonLink, PaginationBox } from './styled'
5+
import { PageNavigationButton } from './PageNavigationButton'
6+
import { BlankButton, PaginationBox } from './styled'
67

78
const PAGES_LIMIT = 14
89

@@ -15,17 +16,14 @@ export interface OrdersTablePaginationProps {
1516
className?: string
1617
}
1718

18-
// TODO: Break down this large function into smaller functions
19-
// TODO: Add proper return type annotation
20-
// eslint-disable-next-line max-lines-per-function, @typescript-eslint/explicit-function-return-type
2119
export function OrdersTablePagination({
2220
pageSize,
2321
totalCount,
2422
currentPage,
2523
getPageUrl,
2624
onPageChange,
2725
className,
28-
}: OrdersTablePaginationProps) {
26+
}: OrdersTablePaginationProps): ReactNode {
2927
const pagesCount = Math.ceil(totalCount / pageSize)
3028

3129
const pagesArray = useMemo(() => [...new Array(pagesCount)].map((item, i) => i), [pagesCount])
@@ -42,12 +40,8 @@ export function OrdersTablePagination({
4240
(page: number) => {
4341
if (onPageChange) {
4442
onPageChange(page)
45-
return
46-
}
47-
48-
if (getPageUrl) {
43+
} else if (getPageUrl) {
4944
getPageUrl(page)
50-
return
5145
}
5246
},
5347
[onPageChange, getPageUrl],
@@ -57,12 +51,15 @@ export function OrdersTablePagination({
5751
<PaginationBox className={className}>
5852
{isListBig && (
5953
<>
60-
<ArrowButton onClick={() => goToPage(Math.max(currentPage - 1, 1))}>
54+
<PageNavigationButton goToPage={goToPage} getPageUrl={getPageUrl} index={Math.max(currentPage - 1, 1)}>
6155
<ChevronLeft size={20} />
62-
</ArrowButton>
56+
</PageNavigationButton>
57+
6358
{!isFirstPagesBatch && (
6459
<>
65-
<PageButton onClick={() => goToPage(1)}>1</PageButton>
60+
<PageNavigationButton goToPage={goToPage} getPageUrl={getPageUrl} index={1}>
61+
1
62+
</PageNavigationButton>
6663
<BlankButton>...</BlankButton>
6764
</>
6865
)}
@@ -71,35 +68,35 @@ export function OrdersTablePagination({
7168
{pagesArray.slice(batchStart, batchEnd).map((i) => {
7269
const index = i + 1
7370

74-
if (onPageChange) {
75-
return (
76-
<PageButton key={index} $active={index === currentPage} onClick={() => onPageChange(index)}>
77-
{index}
78-
</PageButton>
79-
)
80-
}
81-
82-
if (getPageUrl) {
83-
return (
84-
<PageButtonLink key={index} $active={index === currentPage} to={getPageUrl(index)}>
85-
{index}
86-
</PageButtonLink>
87-
)
88-
}
89-
90-
return null
71+
return (
72+
<PageNavigationButton
73+
key={index}
74+
goToPage={goToPage}
75+
getPageUrl={getPageUrl}
76+
index={index}
77+
active={index === currentPage}
78+
>
79+
{index}
80+
</PageNavigationButton>
81+
)
9182
})}
9283
{isListBig && (
9384
<>
9485
{!isLastPagesBatch && (
9586
<>
9687
<BlankButton>...</BlankButton>
97-
<PageButton onClick={() => goToPage(pagesCount)}>{pagesCount}</PageButton>
88+
<PageNavigationButton goToPage={goToPage} getPageUrl={getPageUrl} index={pagesCount}>
89+
{pagesCount}
90+
</PageNavigationButton>
9891
</>
9992
)}
100-
<ArrowButton onClick={() => goToPage(Math.min(currentPage + 1, pagesCount))}>
93+
<PageNavigationButton
94+
goToPage={goToPage}
95+
getPageUrl={getPageUrl}
96+
index={Math.min(currentPage + 1, pagesCount)}
97+
>
10198
<ChevronRight size={20} />
102-
</ArrowButton>
99+
</PageNavigationButton>
103100
</>
104101
)}
105102
</PaginationBox>

0 commit comments

Comments
 (0)