@@ -35,10 +35,10 @@ import {
35
35
/**
36
36
* defines the State of the page whe a user is deleing an object. Has 3 general states:
37
37
* - {}: user is not deleting anything. The page's normal state
38
- * - {index: number , deleteConfirmed: false}: Modal popup asking whether to delete the question, pending user's decision to confirm or cancel
39
- * - {index: number , deleteConfirmed: true}: Currently deleting the question and reloading the database
38
+ * - {index: docref string , deleteConfirmed: false}: Modal popup asking whether to delete the question, pending user's decision to confirm or cancel
39
+ * - {index: docref string , deleteConfirmed: true}: Currently deleting the question and reloading the database
40
40
*/
41
- type DeletionStage = { } | { index : number , deleteConfirmed : boolean }
41
+ type DeletionStage = { } | { index : Question , deleteConfirmed : boolean }
42
42
43
43
function DeleteModal ( { isDeleting, questionTitle, okHandler, cancelHandler} : { questionTitle : string , okHandler : ( ) => void , cancelHandler : ( ) => void , isDeleting : boolean } ) {
44
44
const title : string = `Delete Question \"${ questionTitle } \"?`
@@ -65,12 +65,15 @@ export default function Home() {
65
65
const [ questions , setQuestions ] = useState < Question [ ] | undefined > ( undefined ) ; // Store the questions
66
66
const [ totalCount , setTotalCount ] = useState < number | undefined > ( undefined ) ; // Store the total count of questions
67
67
const [ totalPages , setTotalPages ] = useState < number | undefined > ( undefined ) ; // Store the total number of pages
68
- const [ currentPage , setCurrentPage ] = useState < number | undefined > ( undefined ) ; // Store the current page
69
- const [ limit , setLimit ] = useState < number | undefined > ( 10 ) ; // Store the quantity of questions to be displayed
68
+ const [ currentPage , setCurrentPage ] = useState < number > ( 1 ) ; // Store the current page
69
+ const [ limit , setLimit ] = useState < number > ( 10 ) ; // Store the quantity of questions to be displayed
70
70
const [ isLoading , setIsLoading ] = useState < boolean > ( true ) ; // Store the states related to table's loading
71
71
72
72
// Filtering States
73
73
const [ search , setSearch ] = useState < string | undefined > ( undefined ) ; // Store the search
74
+ const [ delayedSearch , setDelayedSearch ] = useState < string | undefined > (
75
+ undefined
76
+ ) ; // Store the delayed search value
74
77
const [ categories , setCategories ] = useState < string [ ] > ( [ ] ) ; // Store the selected filter categories
75
78
const [ difficulty , setDifficulty ] = useState < string [ ] > ( [ ] ) ; // Store the selected difficulty level
76
79
const [ sortBy , setSortBy ] = useState < string | undefined > ( undefined ) ; // Store the selected sorting parameter
@@ -104,7 +107,7 @@ export default function Home() {
104
107
setIsLoading ( true ) ;
105
108
}
106
109
107
- let data = await GetQuestions ( currentPage , limit ) ;
110
+ let data = await GetQuestions ( currentPage , limit , sortBy , difficulty , delayedSearch ) ;
108
111
setQuestions ( data . questions ) ;
109
112
setTotalCount ( data . totalCount ) ;
110
113
setTotalPages ( data . totalPages ) ;
@@ -113,7 +116,7 @@ export default function Home() {
113
116
setIsLoading ( false ) ;
114
117
}
115
118
116
- useEffect ( ( ) => { loadQuestions ( ) } , [ limit , currentPage ] ) ;
119
+ useEffect ( ( ) => { loadQuestions ( ) } , [ limit , currentPage , sortBy , difficulty , delayedSearch ] ) ;
117
120
118
121
// Table column specification
119
122
const columns : TableProps < Question > [ "columns" ] = [
@@ -156,7 +159,7 @@ export default function Home() {
156
159
title : "Actions" ,
157
160
key : "actions" ,
158
161
dataIndex : "id" ,
159
- render : ( id : number ) => (
162
+ render : ( _ : number , question : Question ) => (
160
163
< div >
161
164
{ /* TODO (Sean): Include Logic to handle retrieving of editable data here and display in a modal component */ }
162
165
< Button className = "edit-button" icon = { < EditOutlined /> } > </ Button >
@@ -166,15 +169,7 @@ export default function Home() {
166
169
danger
167
170
icon = { < DeleteOutlined /> }
168
171
onClick = { ( ) => {
169
- if ( questions === undefined ) {
170
- throw new Error ( "questions is undefined" )
171
- }
172
- let toDelete = questions . findIndex ( row => row . id === id )
173
- if ( toDelete === - 1 ) {
174
- error ( "Could not find id" ) ;
175
- return ;
176
- }
177
- setDeletionStage ( { index : toDelete , deleteConfirmed : false } ) }
172
+ setDeletionStage ( { index : question , deleteConfirmed : false } ) }
178
173
}
179
174
> </ Button >
180
175
</ div >
@@ -185,6 +180,7 @@ export default function Home() {
185
180
// Handler for change in multi-select categories option
186
181
const handleCategoriesChange = ( value : string [ ] ) => {
187
182
setCategories ( value ) ;
183
+ setCurrentPage ( 1 ) ; // Reset the current page
188
184
} ;
189
185
190
186
// Handler for clearing the filtering options
@@ -196,17 +192,15 @@ export default function Home() {
196
192
} ;
197
193
198
194
// Handler for filtering (TODO)
199
- const handleFilter = async ( ) => {
200
- success ( "Filtered Successfully!" ) ;
201
- } ;
195
+ // const handleFilter = async () => {
196
+ // success("Filtered Successfully!");
197
+ // };
202
198
203
199
// Handler for show size change for pagination
204
200
const onShowSizeChange : PaginationProps [ "onShowSizeChange" ] = (
205
201
current ,
206
202
pageSize
207
203
) => {
208
- console . log ( current ) ;
209
- console . log ( pageSize ) ;
210
204
setCurrentPage ( current ) ;
211
205
setLimit ( pageSize ) ;
212
206
} ;
@@ -225,18 +219,16 @@ export default function Home() {
225
219
error ( "Cannot delete: questions does not exist" ) ;
226
220
return ;
227
221
}
228
- if ( currentPage == undefined ) {
229
- error ( "Cannot delete: currentPage does not exist" ) ;
230
- return ;
231
- }
232
222
233
223
setDeletionStage ( { index : deletionStage . index , deleteConfirmed : true } ) ;
234
- await DeleteQuestionByDocref ( questions [ deletionStage . index ] . docRefId ) ;
224
+ await DeleteQuestionByDocref ( deletionStage . index . docRefId ) ;
235
225
if ( questions . length == 1 && currentPage > 1 ) {
236
226
setCurrentPage ( currentPage - 1 ) ;
227
+ success ( "Question deleted successfully" ) ;
237
228
} else {
238
229
try {
239
230
await loadQuestions ( ) ;
231
+ success ( "Question deleted successfully" ) ;
240
232
} catch ( err ) {
241
233
if ( typeof err == 'string' ) {
242
234
error ( err ) ;
@@ -271,6 +263,7 @@ export default function Home() {
271
263
prefix = { < SearchOutlined /> }
272
264
onChange = { ( e ) => setSearch ( e . target . value ) }
273
265
value = { search }
266
+ allowClear
274
267
/>
275
268
</ Col >
276
269
< Col span = { 6 } >
@@ -284,12 +277,15 @@ export default function Home() {
284
277
value = { categories }
285
278
/>
286
279
</ Col >
287
- < Col span = { 4 } >
280
+ < Col span = { 6 } >
288
281
< Select
289
282
mode = "multiple"
290
283
allowClear
291
284
placeholder = "Difficulty"
292
- onChange = { ( value : string [ ] ) => setDifficulty ( value ) }
285
+ onChange = { ( value : string [ ] ) => {
286
+ setDifficulty ( value ) ;
287
+ setCurrentPage ( 1 ) ; //Reset the currentpage since filter params changed
288
+ } }
293
289
options = { DifficultyOption }
294
290
className = "difficulty-select"
295
291
value = { difficulty }
@@ -305,15 +301,17 @@ export default function Home() {
305
301
value = { sortBy }
306
302
/>
307
303
</ Col >
308
- < Col span = { 4 } >
309
- < Button onClick = { handleClear } > Clear</ Button >
310
- < Button
304
+ < Col span = { 2 } >
305
+ < Button onClick = { handleClear } className = "clear-button" >
306
+ Clear
307
+ </ Button >
308
+ { /* <Button
311
309
type="primary"
312
310
className="filter-button"
313
311
onClick={handleFilter}
314
312
>
315
313
Filter
316
- </ Button >
314
+ </Button> */ }
317
315
</ Col >
318
316
</ Row >
319
317
</ div >
@@ -323,6 +321,7 @@ export default function Home() {
323
321
columns = { columns }
324
322
loading = { isLoading }
325
323
pagination = { {
324
+ current : currentPage ,
326
325
total : totalCount ,
327
326
showSizeChanger : true ,
328
327
onShowSizeChange : onShowSizeChange ,
@@ -337,7 +336,7 @@ export default function Home() {
337
336
{ ( "index" in deletionStage && questions != undefined ) && < DeleteModal
338
337
okHandler = { confirmDeleteHandler }
339
338
cancelHandler = { ( ) => setDeletionStage ( { } ) }
340
- questionTitle = { questions [ deletionStage . index ] . title }
339
+ questionTitle = { deletionStage . index . title }
341
340
isDeleting = { deletionStage . deleteConfirmed } /> }
342
341
</ div >
343
342
) ;
0 commit comments