@@ -36,12 +36,15 @@ export default function Home() {
36
36
const [ questions , setQuestions ] = useState < Question [ ] | undefined > ( undefined ) ; // Store the questions
37
37
const [ totalCount , setTotalCount ] = useState < number | undefined > ( undefined ) ; // Store the total count of questions
38
38
const [ totalPages , setTotalPages ] = useState < number | undefined > ( undefined ) ; // Store the total number of pages
39
- const [ currentPage , setCurrentPage ] = useState < number | undefined > ( undefined ) ; // Store the current page
39
+ const [ currentPage , setCurrentPage ] = useState < number | undefined > ( 1 ) ; // Store the current page
40
40
const [ limit , setLimit ] = useState < number | undefined > ( 10 ) ; // Store the quantity of questions to be displayed
41
41
const [ isLoading , setIsLoading ] = useState < boolean > ( true ) ; // Store the states related to table's loading
42
42
43
43
// Filtering States
44
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
45
48
const [ categories , setCategories ] = useState < string [ ] > ( [ ] ) ; // Store the selected filter categories
46
49
const [ difficulty , setDifficulty ] = useState < string [ ] > ( [ ] ) ; // Store the selected difficulty level
47
50
const [ sortBy , setSortBy ] = useState < string | undefined > ( undefined ) ; // Store the selected sorting parameter
@@ -79,15 +82,26 @@ export default function Home() {
79
82
setIsLoading ( true ) ;
80
83
}
81
84
82
- GetQuestions ( currentPage , limit , sortBy ) . then ( ( data ) => {
83
- setQuestions ( data . questions ) ;
84
- setTotalCount ( data . totalCount ) ;
85
- setTotalPages ( data . totalPages ) ;
86
- setCurrentPage ( data . currentPage ) ;
87
- setLimit ( data . limit ) ;
88
- setIsLoading ( false ) ;
89
- } ) ;
90
- } , [ limit , currentPage , sortBy ] ) ;
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 ] ) ;
91
105
92
106
// Table column specification
93
107
const columns : TableProps < Question > [ "columns" ] = [
@@ -148,6 +162,7 @@ export default function Home() {
148
162
// Handler for change in multi-select categories option
149
163
const handleCategoriesChange = ( value : string [ ] ) => {
150
164
setCategories ( value ) ;
165
+ setCurrentPage ( 1 ) ; // Reset the current page
151
166
} ;
152
167
153
168
// Handler for clearing the filtering options
@@ -159,9 +174,9 @@ export default function Home() {
159
174
} ;
160
175
161
176
// Handler for filtering (TODO)
162
- const handleFilter = async ( ) => {
163
- success ( "Filtered Successfully!" ) ;
164
- } ;
177
+ // const handleFilter = async () => {
178
+ // success("Filtered Successfully!");
179
+ // };
165
180
166
181
// Handler for show size change for pagination
167
182
const onShowSizeChange : PaginationProps [ "onShowSizeChange" ] = (
@@ -202,6 +217,7 @@ export default function Home() {
202
217
prefix = { < SearchOutlined /> }
203
218
onChange = { ( e ) => setSearch ( e . target . value ) }
204
219
value = { search }
220
+ allowClear
205
221
/>
206
222
</ Col >
207
223
< Col span = { 6 } >
@@ -215,12 +231,15 @@ export default function Home() {
215
231
value = { categories }
216
232
/>
217
233
</ Col >
218
- < Col span = { 4 } >
234
+ < Col span = { 6 } >
219
235
< Select
220
236
mode = "multiple"
221
237
allowClear
222
238
placeholder = "Difficulty"
223
- onChange = { ( value : string [ ] ) => setDifficulty ( value ) }
239
+ onChange = { ( value : string [ ] ) => {
240
+ setDifficulty ( value ) ;
241
+ setCurrentPage ( 1 ) ; //Reset the currentpage since filter params changed
242
+ } }
224
243
options = { DifficultyOption }
225
244
className = "difficulty-select"
226
245
value = { difficulty }
@@ -236,15 +255,17 @@ export default function Home() {
236
255
value = { sortBy }
237
256
/>
238
257
</ Col >
239
- < Col span = { 4 } >
240
- < Button onClick = { handleClear } > Clear</ Button >
241
- < Button
258
+ < Col span = { 2 } >
259
+ < Button onClick = { handleClear } className = "clear-button" >
260
+ Clear
261
+ </ Button >
262
+ { /* <Button
242
263
type="primary"
243
264
className="filter-button"
244
265
onClick={handleFilter}
245
266
>
246
267
Filter
247
- </ Button >
268
+ </Button> */ }
248
269
</ Col >
249
270
</ Row >
250
271
</ div >
@@ -254,6 +275,7 @@ export default function Home() {
254
275
columns = { columns }
255
276
loading = { isLoading }
256
277
pagination = { {
278
+ current : currentPage ,
257
279
total : totalCount ,
258
280
showSizeChanger : true ,
259
281
onShowSizeChange : onShowSizeChange ,
0 commit comments