Skip to content

Commit 88475fc

Browse files
committed
Add pagination and sort to teams page
1 parent 5337cf1 commit 88475fc

File tree

2 files changed

+68
-32
lines changed

2 files changed

+68
-32
lines changed

cypress/e2e/teams/index.cy.js

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,37 @@ const {
33
addZeroPadding,
44
} = require('../../../src/lib/utils')
55

6-
const team1Members = generateSequenceArray(25, 1).map((i) => ({
7-
id: i,
8-
name: `User ${addZeroPadding(i, 3)}`,
9-
}))
10-
11-
const [user1] = team1Members
12-
13-
const team1 = {
14-
name: 'Team 1',
15-
privacy: 'public',
6+
// Moderator user
7+
const user1 = {
8+
id: 1,
169
}
1710

18-
const team2 = {
19-
name: 'Team 2',
20-
privacy: 'private',
21-
}
11+
// Generate teams
12+
const TEAMS_COUNT = 35
13+
const teams = generateSequenceArray(TEAMS_COUNT, 1).map((i) => ({
14+
id: i,
15+
name: `Team ${addZeroPadding(i, 3)}`,
16+
}))
2217

2318
describe('Teams page', () => {
2419
before(() => {
2520
cy.task('db:reset')
2621
cy.task('db:seed:create-teams', {
27-
teams: [team1, team2],
22+
teams,
2823
moderatorId: user1.id,
2924
})
3025
})
3126

3227
it('Teams index is public and list teams', () => {
3328
cy.visit('/teams')
34-
cy.get('body').should('contain', 'Team 1')
35-
cy.get('body').should('contain', 'Team 2')
29+
30+
cy.get('body').should('contain', 'Team 001')
31+
cy.get("[data-cy='teams-table-pagination']").should('exist')
32+
cy.get('[data-cy=teams-table-pagination]').contains('Showing 1-10 of 35')
33+
34+
// Perform sort by team name
35+
cy.get('[data-cy=table-head-column-name]').click()
36+
cy.get('[data-cy=teams-table]').contains('Team 035')
37+
cy.get('[data-cy=teams-table]').contains('Team 026')
3638
})
3739
})

src/pages/teams/index.js

Lines changed: 49 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@ import Section from '../../components/section'
55
import Table from '../../components/tables/table'
66
import theme from '../../styles/theme'
77
import join from 'url-join'
8-
import { pick, map } from 'ramda'
8+
import { pick, map, sort, descend, ascend, prop } from 'ramda'
99
import { getTeams } from '../../lib/teams-api'
1010
import logger from '../../lib/logger'
11+
import { serverRuntimeConfig } from '../../../next.config.js'
12+
import Pagination from '../../components/pagination'
13+
const { DEFAULT_PAGE_SIZE } = serverRuntimeConfig
1114

1215
const Map = dynamic(import('../../components/list-map'), {
1316
ssr: false,
@@ -24,6 +27,11 @@ export default class TeamList extends Component {
2427
teams: [],
2528
searchOnMapMove: false,
2629
mapBounds: undefined,
30+
page: 1,
31+
sortOptions: {
32+
key: 'name',
33+
direction: 'asc',
34+
},
2735
}
2836
}
2937

@@ -51,24 +59,50 @@ export default class TeamList extends Component {
5159
}
5260

5361
renderTeams() {
54-
const { teams } = this.state
62+
const { teams, sortOptions, page } = this.state
5563
if (!teams) return null
5664

57-
if (teams.length === 0) {
58-
return <p>No teams created</p>
59-
}
65+
const columns = [
66+
{ key: 'name', sortable: true },
67+
{ key: 'id', sortable: true },
68+
{ key: 'hashtag', sortable: true },
69+
]
70+
71+
let rows = sort(
72+
sortOptions.direction === 'asc'
73+
? ascend(prop(sortOptions.key))
74+
: descend(prop(sortOptions.key)),
75+
teams
76+
)
77+
78+
// Calculate start and end index
79+
const pageStartIndex = (page - 1) * DEFAULT_PAGE_SIZE
80+
const pageEndIndex = pageStartIndex + DEFAULT_PAGE_SIZE
6081

6182
return (
62-
<Table
63-
rows={teams}
64-
columns={[{ key: 'name' }, { key: 'id' }, { key: 'hashtag' }]}
65-
onRowClick={(row) => {
66-
Router.push(
67-
join(URL, `/team?id=${row.id}`),
68-
join(URL, `/teams/${row.id}`)
69-
)
70-
}}
71-
/>
83+
<>
84+
<Table
85+
data-cy={`teams-table`}
86+
columns={columns}
87+
rows={rows.slice(pageStartIndex, pageEndIndex)}
88+
onRowClick={(row) => {
89+
Router.push(join(URL, `/teams/${row.id}`))
90+
}}
91+
emptyPlaceHolder={'No teams created yet.'}
92+
showRowNumbers
93+
sort={sortOptions}
94+
setSort={(s) => this.setState({ sortOptions: s })}
95+
/>
96+
<Pagination
97+
data-cy={`teams-table-pagination`}
98+
pagination={{
99+
perPage: DEFAULT_PAGE_SIZE,
100+
total: rows.length,
101+
currentPage: page,
102+
}}
103+
setPage={(p) => this.setState({ page: p })}
104+
/>
105+
</>
72106
)
73107
}
74108

0 commit comments

Comments
 (0)