Skip to content

Commit 598a29a

Browse files
committed
implement load user
1 parent 7b81840 commit 598a29a

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

app/javascript/components/users.tsx

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
import { useState } from 'react'
2-
import { GetUsersResponse } from '../generated_types/users'
2+
import { GetUsersResponse, UserDetail } from '../generated_types/users'
33
import PaginationLinks from './pagination'
44
import getApi from '../utils/get_api'
5+
import User from './user'
56

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

11+
const [loadedUser, setLoadedUser] = useState<UserDetail | undefined>()
12+
1013
// this methods loads the requested page & re-renders
1114
// the table/pagination using it
1215
const loadPage = async (pageNumber: number) => {
@@ -15,6 +18,11 @@ const Users = ({ pagination: initialPagination, users: initialUsers }: GetUsersR
1518
setPagination(response.pagination)
1619
}
1720

21+
const loadUser = async (userId: number) => {
22+
const response = await getApi(`/users/${userId}`)
23+
setLoadedUser(response.user)
24+
}
25+
1826
return (
1927
<>
2028
<table>
@@ -30,12 +38,16 @@ const Users = ({ pagination: initialPagination, users: initialUsers }: GetUsersR
3038
<tr key={u.id}>
3139
<td>{u.id}</td>
3240
<td>{u.name}</td>
33-
<td>Show</td>
41+
<td>
42+
<a onClick={() => loadUser(u.id)}>Show</a>
43+
</td>
3444
</tr>
3545
))}
3646
</tbody>
3747
</table>
3848
<PaginationLinks onPageChanged={newPage => loadPage(newPage)} {...pagination} />
49+
50+
{loadedUser && <User user={loadedUser} />}
3951
</>
4052
)
4153
}

0 commit comments

Comments
 (0)