Skip to content

Commit 5b6d47e

Browse files
committed
implement page changing
1 parent 4917627 commit 5b6d47e

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

app/javascript/components/users.tsx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
11
import { useState } from 'react'
22
import { GetUsersResponse } from '../generated_types/users'
33
import PaginationLinks from './pagination'
4+
import getApi from '../utils/get_api'
45

56
const Users = ({ pagination: initialPagination, users: initialUsers }: GetUsersResponse) => {
67
const [pagination, setPagination] = useState(initialPagination)
78
const [users, setUsers] = useState(initialUsers)
89

10+
// this methods loads the requested page & re-renders
11+
// the table/pagination using it
12+
const loadPage = async (pageNumber: number) => {
13+
const response = await getApi(`/users?page=${pageNumber}`)
14+
setUsers(response.users)
15+
setPagination(response.pagination)
16+
}
17+
918
return (
1019
<>
1120
<table>
@@ -26,7 +35,7 @@ const Users = ({ pagination: initialPagination, users: initialUsers }: GetUsersR
2635
))}
2736
</tbody>
2837
</table>
29-
<PaginationLinks onPageChanged={newPage => {}} {...pagination} />
38+
<PaginationLinks onPageChanged={newPage => loadPage(newPage)} {...pagination} />
3039
</>
3140
)
3241
}

0 commit comments

Comments
 (0)