-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReactTable.js
164 lines (154 loc) · 5.12 KB
/
ReactTable.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import React, { useMemo, useState } from 'react'
import Skeleton from 'react-loading-skeleton'
import axios from 'axios'
import { useQuery } from 'react-query'
import { useTable, usePagination } from 'react-table'
import './ReactTable.css'
const fetchPeople = async (key, pageIndex) => {
let peopleList = await axios.get(
`https://swapi.dev/api/people/?page=${pageIndex + 1}`,
)
return peopleList
}
const PeopleTable = ({ columns }) => {
const [pageIndex, setPageIndex] = useState(0)
const { data: peopleList, isLoading, refetch, isFetching } = useQuery(
['fetchPeople', pageIndex],
fetchPeople,
)
const {
getTableProps,
getTableBodyProps,
headerGroups,
page,
prepareRow,
pageOptions,
} = useTable(
{
columns,
data: peopleList ? peopleList.data.results : [],
manualPagination: true,
pageCount: peopleList ? peopleList.data.count : 0,
},
usePagination,
)
const previousPagePossible = () => {
if(pageIndex === 0) return true
return false
}
const nextPagePossible = () => {
if(pageIndex === pageOptions.length) return true
return false
}
const handleRefetch = () => {
setPageIndex(0)
refetch()
}
console.log({isLoading, isFetching})
return (
<div className = 'people-table'>
<div className = 'refetch-test' onClick = { () => handleRefetch() }>Refetch testing</div>
<table { ...getTableProps() }>
<thead>
{headerGroups.map((headerGroup, headerIndex) => (
<tr
{ ...headerGroup.getHeaderGroupProps() }
key = { headerIndex }
>
{headerGroup.headers.map((column, columnIndex) => {
return (
<th
key = { columnIndex }
{ ...column.getHeaderProps() }
>
{column.render('Header')}
</th>
)
})}
</tr>
))}
</thead>
<tbody { ...getTableBodyProps() }>
{(!isLoading || !isFetching ) &&
page.map((row, rowIndex) => {
prepareRow(row)
return (
<tr { ...row.getRowProps() } key = { rowIndex }>
{row.cells.map((cell, cellIndex) => {
return (
<td key = { cellIndex }>
{cell.render('Cell')}
</td>
)
})}
</tr>
)
})}
</tbody>
</table>
{(!isLoading || !isFetching) && (
<div className = 'pagination'>
<button
disabled = { previousPagePossible() }
onClick = { () => setPageIndex(i => i - 1) }
>
{'<'}
</button>{' '}
<button
disabled = { nextPagePossible() }
onClick = { () => setPageIndex(i => i + 1) }
>
{'>'}
</button>{' '}
<span>
Page{' '}
<strong>
{pageIndex + 1} of {pageOptions.length}
</strong>{' '}
</span>
</div>
)}
{(isLoading || isFetching) && <Skeleton
count = { 10 } height = { 50 }
width = { '100%' }
/>}
</div>
)
}
const ReactTable = () => {
let columns = useMemo(
() => [
{
Header: 'Name',
accessor: 'name',
},
{
Header: 'Gender',
accessor: 'gender',
},
{
Header: 'Height',
accessor: 'height',
},
{
Header: 'Mass',
accessor: 'mass',
},
{
Header: 'Hair Color',
accessor: 'hair_color',
},
{
Header: 'Skin Color',
accessor: 'skin_color',
},
{
Header: 'Eye Color',
accessor: 'eye_color',
},
],
[],
)
return <PeopleTable columns = { columns } />
}
export default ReactTable