1
1
import { useState } from 'react'
2
- import { GetUsersResponse } from '../generated_types/users'
2
+ import { GetUsersResponse , UserDetail } from '../generated_types/users'
3
3
import PaginationLinks from './pagination'
4
4
import getApi from '../utils/get_api'
5
+ import User from './user'
5
6
6
7
const Users = ( { pagination : initialPagination , users : initialUsers } : GetUsersResponse ) => {
7
8
const [ pagination , setPagination ] = useState ( initialPagination )
8
9
const [ users , setUsers ] = useState ( initialUsers )
9
10
11
+ const [ loadedUser , setLoadedUser ] = useState < UserDetail | undefined > ( )
12
+
10
13
// this methods loads the requested page & re-renders
11
14
// the table/pagination using it
12
15
const loadPage = async ( pageNumber : number ) => {
@@ -15,6 +18,11 @@ const Users = ({ pagination: initialPagination, users: initialUsers }: GetUsersR
15
18
setPagination ( response . pagination )
16
19
}
17
20
21
+ const loadUser = async ( userId : number ) => {
22
+ const response = await getApi ( `/users/${ userId } ` )
23
+ setLoadedUser ( response . user )
24
+ }
25
+
18
26
return (
19
27
< >
20
28
< table >
@@ -30,12 +38,16 @@ const Users = ({ pagination: initialPagination, users: initialUsers }: GetUsersR
30
38
< tr key = { u . id } >
31
39
< td > { u . id } </ td >
32
40
< td > { u . name } </ td >
33
- < td > Show</ td >
41
+ < td >
42
+ < a onClick = { ( ) => loadUser ( u . id ) } > Show</ a >
43
+ </ td >
34
44
</ tr >
35
45
) ) }
36
46
</ tbody >
37
47
</ table >
38
48
< PaginationLinks onPageChanged = { newPage => loadPage ( newPage ) } { ...pagination } />
49
+
50
+ { loadedUser && < User user = { loadedUser } /> }
39
51
</ >
40
52
)
41
53
}
0 commit comments