Skip to content

Commit df58082

Browse files
committed
implement filter dropdown based on checkbox
1 parent fb8b26e commit df58082

File tree

2 files changed

+57
-7
lines changed

2 files changed

+57
-7
lines changed

src/components/Table/filters.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ function CreateDropDownListHelper(options, filterValue, setFilter, id) {
55
<select
66
value={filterValue}
77
onChange={e => {
8-
setFilter(e.target.value || '');
98
localStorage.setItem(id, e.target.value);
9+
setFilter(e.target.value || '');
1010
}}
1111
>
1212
<option value="">All</option>
@@ -64,3 +64,15 @@ export function SelectColumnFilter({
6464

6565
return CreateDropDownListHelper(options, filterValue, setFilter, id);
6666
}
67+
68+
export function SelectCheckedColumnFilter({
69+
column: { filterValue, setFilter, id, filterByCheckbox },
70+
}) {
71+
const options = ['Checked', 'Unchecked'];
72+
const filter = val => {
73+
setFilter(val);
74+
filterByCheckbox();
75+
};
76+
77+
return CreateDropDownListHelper(options, filterValue, filter, id);
78+
}

src/components/Table/index.js

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
DefaultColumnFilter,
2020
SelectDifficultyColumnFilter,
2121
SelectColumnFilter,
22+
SelectCheckedColumnFilter,
2223
} from './filters';
2324
import { Event } from '../Shared/Tracking';
2425

@@ -31,20 +32,19 @@ import PatternFrequencies from '../PatternFrequencies';
3132
const iconPath = `${process.env.PUBLIC_URL}/assets/icons/`;
3233

3334
const Table = () => {
34-
const data = React.useMemo(() => questions, []);
3535
const [resetCount, setResetCount] = useState(0);
3636
let checkedList =
3737
JSON.parse(localStorage.getItem('checked')) ||
38-
new Array(data.length).fill(false);
38+
new Array(questions.length).fill(false);
3939

4040
/* If the user has previously visited the website, then an array in
4141
LocalStorage would exist of a certain length which corresponds to which
4242
questions they have/have not completed. In the event that we add new questions
4343
to the list, then we would need to resize and copy the existing 'checked'
4444
array before updating it in LocalStorage in order to transfer their saved
4545
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);
4848

4949
for (let i = 0; i < checkedList.length; i += 1) {
5050
resizedCheckedList[i] = checkedList[i];
@@ -54,6 +54,24 @@ const Table = () => {
5454
window.localStorage.setItem('checked', JSON.stringify(checkedList));
5555
}
5656

57+
const filteredByCheckbox = () => {
58+
return questions.filter(question => {
59+
const checkbox = localStorage.getItem('checkbox') || '';
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+
73+
const [data, setData] = useState(filteredByCheckbox());
74+
5775
const difficultyMap = { Easy: 0, Medium: 0, Hard: 0 };
5876
const totalDifficultyCount = { Easy: 0, Medium: 0, Hard: 0 };
5977
for (let i = 0; i < data.length; i += 1) {
@@ -160,6 +178,8 @@ const Table = () => {
160178
>
161179
Reset
162180
</Button>
181+
<br />
182+
<br />
163183
<Modal isOpen={resetModal} toggle={toggleResetModal}>
164184
<ModalHeader toggle={toggleResetModal}>
165185
Are you sure you want to reset your progress?
@@ -174,7 +194,12 @@ const Table = () => {
174194
</span>
175195
);
176196
},
177-
id: 'Checkbox',
197+
accessor: 'checkbox',
198+
id: 'checkbox',
199+
filterByCheckbox: () => {
200+
setData(filteredByCheckbox());
201+
},
202+
disableSortBy: true,
178203
Cell: cellInfo => {
179204
return (
180205
<span data-tip={`Question #${Number(cellInfo.row.id) + 1}`}>
@@ -185,7 +210,14 @@ const Table = () => {
185210
checked[cellInfo.row.original.id] = !checked[
186211
cellInfo.row.original.id
187212
];
188-
213+
const question = questions.find(
214+
q => q.id === cellInfo.row.original.id,
215+
);
216+
if (checked[cellInfo.row.original.id]) {
217+
question.checkbox = 'Checked';
218+
} else {
219+
question.checkbox = 'Unchecked';
220+
}
189221
const additive = checked[cellInfo.row.original.id]
190222
? 1
191223
: -1;
@@ -194,11 +226,13 @@ const Table = () => {
194226
] += additive;
195227
setDifficultyCount(difficultyCount);
196228
setChecked([...checked]);
229+
setData(filteredByCheckbox());
197230
}}
198231
/>
199232
</span>
200233
);
201234
},
235+
Filter: SelectCheckedColumnFilter,
202236
},
203237
{
204238
Header: 'Questions',
@@ -384,6 +418,10 @@ const Table = () => {
384418
defaultColumn,
385419
initialState: {
386420
filters: [
421+
{
422+
id: 'checkbox',
423+
value: localStorage.getItem('checkbox') || '',
424+
},
387425
{
388426
id: 'difficulty',
389427
value: localStorage.getItem('difficulty') || '',

0 commit comments

Comments
 (0)