@@ -19,6 +19,7 @@ import {
19
19
DefaultColumnFilter ,
20
20
SelectDifficultyColumnFilter ,
21
21
SelectColumnFilter ,
22
+ SelectCheckedColumnFilter ,
22
23
} from './filters' ;
23
24
import { Event } from '../Shared/Tracking' ;
24
25
@@ -31,20 +32,19 @@ import PatternFrequencies from '../PatternFrequencies';
31
32
const iconPath = `${ process . env . PUBLIC_URL } /assets/icons/` ;
32
33
33
34
const Table = ( ) => {
34
- const data = React . useMemo ( ( ) => questions , [ ] ) ;
35
35
const [ resetCount , setResetCount ] = useState ( 0 ) ;
36
36
let checkedList =
37
37
JSON . parse ( localStorage . getItem ( 'checked' ) ) ||
38
- new Array ( data . length ) . fill ( false ) ;
38
+ new Array ( questions . length ) . fill ( false ) ;
39
39
40
40
/* If the user has previously visited the website, then an array in
41
41
LocalStorage would exist of a certain length which corresponds to which
42
42
questions they have/have not completed. In the event that we add new questions
43
43
to the list, then we would need to resize and copy the existing 'checked'
44
44
array before updating it in LocalStorage in order to transfer their saved
45
45
progress. */
46
- if ( checkedList . length !== data . length ) {
47
- const resizedCheckedList = new Array ( data . length ) . fill ( false ) ;
46
+ if ( checkedList . length !== questions . length ) {
47
+ const resizedCheckedList = new Array ( questions . length ) . fill ( false ) ;
48
48
49
49
for ( let i = 0 ; i < checkedList . length ; i += 1 ) {
50
50
resizedCheckedList [ i ] = checkedList [ i ] ;
@@ -54,13 +54,30 @@ const Table = () => {
54
54
window . localStorage . setItem ( 'checked' , JSON . stringify ( checkedList ) ) ;
55
55
}
56
56
57
+ const filteredByCheckbox = ( ) => {
58
+ const checkbox = localStorage . getItem ( 'checkbox' ) || '' ;
59
+ return questions . filter ( question => {
60
+ if ( ! checkbox ) return true ;
61
+ return question . checkbox === checkbox ;
62
+ } ) ;
63
+ } ;
64
+
65
+ for ( let i = 0 ; i < questions . length ; i += 1 ) {
66
+ if ( checkedList [ questions [ i ] . id ] ) {
67
+ questions [ i ] . checkbox = 'Checked' ;
68
+ } else {
69
+ questions [ i ] . checkbox = 'Unchecked' ;
70
+ }
71
+ }
72
+
57
73
const difficultyMap = { Easy : 0 , Medium : 0 , Hard : 0 } ;
58
74
const totalDifficultyCount = { Easy : 0 , Medium : 0 , Hard : 0 } ;
59
- for ( let i = 0 ; i < data . length ; i += 1 ) {
60
- difficultyMap [ data [ i ] . difficulty ] += checkedList [ data [ i ] . id ] ;
61
- totalDifficultyCount [ data [ i ] . difficulty ] += 1 ;
75
+ for ( let i = 0 ; i < questions . length ; i += 1 ) {
76
+ difficultyMap [ questions [ i ] . difficulty ] += checkedList [ questions [ i ] . id ] ;
77
+ totalDifficultyCount [ questions [ i ] . difficulty ] += 1 ;
62
78
}
63
79
80
+ const [ data , setData ] = useState ( filteredByCheckbox ( ) ) ;
64
81
const [ difficultyCount , setDifficultyCount ] = useState ( difficultyMap ) ;
65
82
const [ checked , setChecked ] = useState ( checkedList ) ;
66
83
const [ showPatterns , setShowPatterns ] = useState (
@@ -174,7 +191,12 @@ const Table = () => {
174
191
</ span >
175
192
) ;
176
193
} ,
177
- id : 'Checkbox' ,
194
+ accessor : 'checkbox' ,
195
+ id : 'checkbox' ,
196
+ filterByCheckbox : ( ) => {
197
+ setData ( filteredByCheckbox ( ) ) ;
198
+ } ,
199
+ disableSortBy : true ,
178
200
Cell : cellInfo => {
179
201
return (
180
202
< span data-tip = { `Question #${ Number ( cellInfo . row . id ) + 1 } ` } >
@@ -185,7 +207,14 @@ const Table = () => {
185
207
checked [ cellInfo . row . original . id ] = ! checked [
186
208
cellInfo . row . original . id
187
209
] ;
188
-
210
+ const question = questions . find (
211
+ q => q . id === cellInfo . row . original . id ,
212
+ ) ;
213
+ if ( checked [ cellInfo . row . original . id ] ) {
214
+ question . checkbox = 'Checked' ;
215
+ } else {
216
+ question . checkbox = 'Unchecked' ;
217
+ }
189
218
const additive = checked [ cellInfo . row . original . id ]
190
219
? 1
191
220
: - 1 ;
@@ -194,11 +223,13 @@ const Table = () => {
194
223
] += additive ;
195
224
setDifficultyCount ( difficultyCount ) ;
196
225
setChecked ( [ ...checked ] ) ;
226
+ setData ( filteredByCheckbox ( ) ) ;
197
227
} }
198
228
/>
199
229
</ span >
200
230
) ;
201
231
} ,
232
+ Filter : SelectCheckedColumnFilter ,
202
233
} ,
203
234
{
204
235
Header : 'Questions' ,
@@ -384,6 +415,10 @@ const Table = () => {
384
415
defaultColumn,
385
416
initialState : {
386
417
filters : [
418
+ {
419
+ id : 'checkbox' ,
420
+ value : localStorage . getItem ( 'checkbox' ) || '' ,
421
+ } ,
387
422
{
388
423
id : 'difficulty' ,
389
424
value : localStorage . getItem ( 'difficulty' ) || '' ,
0 commit comments