6
6
Input ,
7
7
Layout ,
8
8
message ,
9
+ Pagination ,
10
+ PaginationProps ,
9
11
Row ,
10
12
Select ,
11
13
Table ,
@@ -32,10 +34,17 @@ import {
32
34
export default function Home ( ) {
33
35
// Table States
34
36
const [ questions , setQuestions ] = useState < Question [ ] | undefined > ( undefined ) ; // Store the questions
37
+ const [ totalCount , setTotalCount ] = useState < number | undefined > ( undefined ) ; // Store the total count of questions
38
+ const [ totalPages , setTotalPages ] = useState < number | undefined > ( undefined ) ; // Store the total number of pages
39
+ const [ currentPage , setCurrentPage ] = useState < number | undefined > ( 1 ) ; // Store the current page
40
+ const [ limit , setLimit ] = useState < number | undefined > ( 10 ) ; // Store the quantity of questions to be displayed
35
41
const [ isLoading , setIsLoading ] = useState < boolean > ( true ) ; // Store the states related to table's loading
36
42
37
43
// Filtering States
38
44
const [ search , setSearch ] = useState < string | undefined > ( undefined ) ; // Store the search
45
+ const [ delayedSearch , setDelayedSearch ] = useState < string | undefined > (
46
+ undefined
47
+ ) ; // Store the delayed search value
39
48
const [ categories , setCategories ] = useState < string [ ] > ( [ ] ) ; // Store the selected filter categories
40
49
const [ difficulty , setDifficulty ] = useState < string [ ] > ( [ ] ) ; // Store the selected difficulty level
41
50
const [ sortBy , setSortBy ] = useState < string | undefined > ( undefined ) ; // Store the selected sorting parameter
@@ -66,17 +75,33 @@ export default function Home() {
66
75
67
76
// Include States for Create/Edit Modal (TODO: Sean)
68
77
69
- // When the page is initialised, fetch all the questions ONCE and display in table
78
+ // When the page is initialised, fetch all the questions and display in table
79
+ // When the dependencies/states change, the useEffect hook will trigger to re-fetch the questions
70
80
useEffect ( ( ) => {
71
81
if ( ! isLoading ) {
72
82
setIsLoading ( true ) ;
73
83
}
74
84
75
- GetQuestions ( ) . then ( ( data ) => {
76
- setQuestions ( data ) ;
77
- setIsLoading ( false ) ;
78
- } ) ;
79
- } , [ ] ) ;
85
+ GetQuestions ( currentPage , limit , sortBy , difficulty , delayedSearch ) . then (
86
+ ( data ) => {
87
+ setQuestions ( data . questions ) ;
88
+ setTotalCount ( data . totalCount ) ;
89
+ setTotalPages ( data . totalPages ) ;
90
+ setCurrentPage ( data . currentPage ) ;
91
+ setLimit ( data . limit ) ;
92
+ setIsLoading ( false ) ;
93
+ }
94
+ ) ;
95
+ } , [ limit , currentPage , sortBy , difficulty , delayedSearch ] ) ; // TODO: (Ryan) Add dependencies for categories and edit the GetQuestion service function
96
+
97
+ // Delay the fetching of data only after user stops typing for awhile
98
+ useEffect ( ( ) => {
99
+ const timeout = setTimeout ( ( ) => {
100
+ setDelayedSearch ( search ) ;
101
+ setCurrentPage ( 1 ) ; // Reset the current page
102
+ } , 800 ) ;
103
+ return ( ) => clearTimeout ( timeout ) ;
104
+ } , [ search ] ) ;
80
105
81
106
// Table column specification
82
107
const columns : TableProps < Question > [ "columns" ] = [
@@ -137,6 +162,7 @@ export default function Home() {
137
162
// Handler for change in multi-select categories option
138
163
const handleCategoriesChange = ( value : string [ ] ) => {
139
164
setCategories ( value ) ;
165
+ setCurrentPage ( 1 ) ; // Reset the current page
140
166
} ;
141
167
142
168
// Handler for clearing the filtering options
@@ -148,8 +174,22 @@ export default function Home() {
148
174
} ;
149
175
150
176
// Handler for filtering (TODO)
151
- const handleFilter = async ( ) => {
152
- success ( "Filtered Successfully!" ) ;
177
+ // const handleFilter = async () => {
178
+ // success("Filtered Successfully!");
179
+ // };
180
+
181
+ // Handler for show size change for pagination
182
+ const onShowSizeChange : PaginationProps [ "onShowSizeChange" ] = (
183
+ current ,
184
+ pageSize
185
+ ) => {
186
+ setCurrentPage ( current ) ;
187
+ setLimit ( pageSize ) ;
188
+ } ;
189
+
190
+ // Handler for change in page jumper
191
+ const onPageJump : PaginationProps [ "onChange" ] = ( pageNumber ) => {
192
+ setCurrentPage ( pageNumber ) ;
153
193
} ;
154
194
155
195
return (
@@ -177,6 +217,7 @@ export default function Home() {
177
217
prefix = { < SearchOutlined /> }
178
218
onChange = { ( e ) => setSearch ( e . target . value ) }
179
219
value = { search }
220
+ allowClear
180
221
/>
181
222
</ Col >
182
223
< Col span = { 6 } >
@@ -190,12 +231,15 @@ export default function Home() {
190
231
value = { categories }
191
232
/>
192
233
</ Col >
193
- < Col span = { 4 } >
234
+ < Col span = { 6 } >
194
235
< Select
195
236
mode = "multiple"
196
237
allowClear
197
238
placeholder = "Difficulty"
198
- onChange = { ( value : string [ ] ) => setDifficulty ( value ) }
239
+ onChange = { ( value : string [ ] ) => {
240
+ setDifficulty ( value ) ;
241
+ setCurrentPage ( 1 ) ; //Reset the currentpage since filter params changed
242
+ } }
199
243
options = { DifficultyOption }
200
244
className = "difficulty-select"
201
245
value = { difficulty }
@@ -211,15 +255,17 @@ export default function Home() {
211
255
value = { sortBy }
212
256
/>
213
257
</ Col >
214
- < Col span = { 4 } >
215
- < Button onClick = { handleClear } > Clear</ Button >
216
- < Button
258
+ < Col span = { 2 } >
259
+ < Button onClick = { handleClear } className = "clear-button" >
260
+ Clear
261
+ </ Button >
262
+ { /* <Button
217
263
type="primary"
218
264
className="filter-button"
219
265
onClick={handleFilter}
220
266
>
221
267
Filter
222
- </ Button >
268
+ </Button> */ }
223
269
</ Col >
224
270
</ Row >
225
271
</ div >
@@ -228,6 +274,14 @@ export default function Home() {
228
274
dataSource = { questions }
229
275
columns = { columns }
230
276
loading = { isLoading }
277
+ pagination = { {
278
+ current : currentPage ,
279
+ total : totalCount ,
280
+ showSizeChanger : true ,
281
+ onShowSizeChange : onShowSizeChange ,
282
+ // showQuickJumper: true,
283
+ onChange : onPageJump ,
284
+ } }
231
285
/>
232
286
</ div >
233
287
</ div >
0 commit comments