Skip to content

Commit 125b303

Browse files
refactor: simplify
1 parent 45039ce commit 125b303

File tree

2 files changed

+33
-33
lines changed

2 files changed

+33
-33
lines changed

packages/curve-ui-kit/src/shared/ui/DataTable/TablePagination.tsx

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { last } from 'lodash'
1+
import { capitalize, last } from 'lodash'
22
import { useCallback, useId } from 'react'
33
import { range } from '@curvefi/prices-api/objects.util'
44
import IconButton from '@mui/material/IconButton'
@@ -50,6 +50,20 @@ const getPageOptions = (pageIndex: number, pageCount: number): [number[], number
5050
range(pageCount - 2, 2).filter((p) => p > pageIndex + 1),
5151
]
5252

53+
/**
54+
* A button component for navigating to the previous or next page in pagination.
55+
*/
56+
const NeighborButton = <T extends TableItem>({ table, type }: { table: Table<T>; type: 'previous' | 'next' }) => (
57+
<IconButton
58+
size="extraSmall"
59+
{...(table[`getCan${capitalize(type)}Page`]()
60+
? { 'data-testid': `btn-page-${type.substring(0, 4)}`, onClick: table[`${type}Page`] }
61+
: { disabled: true })}
62+
>
63+
<ChevronDownIcon sx={{ transform: `rotate(${{ previous: '-90', next: '90' }[type]}deg)` }} />
64+
</IconButton>
65+
)
66+
5367
/**
5468
* Table pagination component for navigating through pages of a data table.
5569
* Renders previous/next buttons and page number buttons with ellipses for skipped pages.
@@ -59,34 +73,20 @@ export const TablePagination = <T extends TableItem>({ table }: { table: Table<T
5973
const [firstPages, aroundPages, lastPages] = getPageOptions(pageIndex, table.getPageCount())
6074
return (
6175
<Stack justifyContent="center" direction="row" data-testid="table-pagination">
62-
<IconButton
63-
size="extraSmall"
64-
{...(table.getCanPreviousPage()
65-
? { 'data-testid': 'btn-page-prev', onClick: table.previousPage }
66-
: { disabled: true })}
67-
>
68-
<ChevronDownIcon sx={{ transform: `rotate(90deg)` }} />
69-
</IconButton>
76+
<NeighborButton table={table} type="previous" />
7077
<ToggleButtonGroup value={pageIndex} size="extraSmall" exclusive data-testid="page-buttons">
7178
{firstPages.map((o) => (
7279
<PageButton key={o} page={o} table={table} />
7380
))}
74-
{firstPages.length > 0 && last(firstPages) != aroundPages[0] - 1 && <Spacer />}
81+
{firstPages.length > 0 && last(firstPages) !== aroundPages[0] - 1 && <Spacer />}
7582
{aroundPages.map((o) => (
7683
<PageButton key={o} page={o} table={table} />
7784
))}
78-
{lastPages.length > 0 && lastPages[0] - 1 != last(aroundPages) && <Spacer />}
85+
{lastPages.length > 0 && lastPages[0] - 1 !== last(aroundPages) && <Spacer />}
7986
{lastPages.map((o) => (
8087
<PageButton key={o} page={o} table={table} />
8188
))}
82-
<IconButton
83-
size="extraSmall"
84-
{...(table.getCanNextPage()
85-
? { 'data-testid': 'btn-page-next', onClick: table.nextPage }
86-
: { disabled: true })}
87-
>
88-
<ChevronDownIcon sx={{ transform: `rotate(-90deg)` }} />
89-
</IconButton>
89+
<NeighborButton table={table} type="next" />
9090
</ToggleButtonGroup>
9191
</Stack>
9292
)

tests/cypress/e2e/main/dex-markets.cy.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { orderBy } from 'lodash'
12
import { oneOf } from '@cy/support/generators'
23
import { setShowSmallPools } from '@cy/support/helpers/user-profile'
34
import { API_LOAD_TIMEOUT, type Breakpoint, LOAD_TIMEOUT, oneViewport } from '@cy/support/ui'
@@ -33,19 +34,20 @@ function visitAndWait(width: number, height: number, options?: { page?: number }
3334

3435
type UsdValue = { text: string; parsed: number }
3536

36-
const expectOrder = (actual: UsdValue[], order: 'asc' | 'desc') => {
37-
const expected = [...actual].sort((a, b) => (a.parsed - b.parsed) * { asc: 1, desc: -1 }[order])
38-
expect(JSON.stringify(actual), `Table values should be in ${order} order`).to.equal(JSON.stringify(expected))
39-
}
40-
41-
const getTopUsdValues = (columnId: 'volume' | 'tvl', n = 5) =>
42-
cy.get(`[data-testid="data-table-cell-${columnId}"]`).then(($cells) =>
43-
Cypress.$.makeArray($cells)
44-
.slice(0, n)
45-
.map((el) => (el as HTMLElement).innerText)
46-
.map((text): UsdValue => ({ text, parsed: parseCompactUsd(text) })),
37+
const expectOrder = (actual: UsdValue[], order: 'asc' | 'desc') =>
38+
expect(JSON.stringify(actual), `Table values should be in ${order} order`).to.equal(
39+
JSON.stringify(orderBy(actual, 'parsed', order)),
4740
)
4841

42+
const getTopUsdValues = (columnId: 'volume' | 'tvl') =>
43+
cy
44+
.get(`[data-testid="data-table-cell-${columnId}"]`)
45+
.then(($cells) =>
46+
Cypress.$.makeArray($cells).map(
47+
({ innerText }): UsdValue => ({ text: innerText, parsed: parseCompactUsd(innerText) }),
48+
),
49+
)
50+
4951
describe('DEX Pools', () => {
5052
let breakpoint: Breakpoint
5153
let width: number, height: number
@@ -55,9 +57,7 @@ describe('DEX Pools', () => {
5557
})
5658

5759
describe('First page', () => {
58-
beforeEach(() => {
59-
visitAndWait(width, height)
60-
})
60+
beforeEach(() => visitAndWait(width, height))
6161

6262
function sortBy(field: string, expectedOrder: 'asc' | 'desc' | false) {
6363
if (breakpoint === 'mobile') {

0 commit comments

Comments
 (0)