Skip to content

Commit 53a7758

Browse files
authored
implement filtering by completion status (#180)
* implement filter dropdown based on checkbox * move local storage access out of filter function * clean up function * move difficulty map code so it gets computed properly * Revert "clean up function" This reverts commit 4073748. * replace br with css
1 parent fb8b26e commit 53a7758

File tree

3 files changed

+58
-10
lines changed

3 files changed

+58
-10
lines changed

Diff for: src/components/Table/filters.js

+13-1
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+
}

Diff for: src/components/Table/index.js

+44-9
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,13 +54,30 @@ const Table = () => {
5454
window.localStorage.setItem('checked', JSON.stringify(checkedList));
5555
}
5656

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+
5773
const difficultyMap = { Easy: 0, Medium: 0, Hard: 0 };
5874
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;
6278
}
6379

80+
const [data, setData] = useState(filteredByCheckbox());
6481
const [difficultyCount, setDifficultyCount] = useState(difficultyMap);
6582
const [checked, setChecked] = useState(checkedList);
6683
const [showPatterns, setShowPatterns] = useState(
@@ -174,7 +191,12 @@ const Table = () => {
174191
</span>
175192
);
176193
},
177-
id: 'Checkbox',
194+
accessor: 'checkbox',
195+
id: 'checkbox',
196+
filterByCheckbox: () => {
197+
setData(filteredByCheckbox());
198+
},
199+
disableSortBy: true,
178200
Cell: cellInfo => {
179201
return (
180202
<span data-tip={`Question #${Number(cellInfo.row.id) + 1}`}>
@@ -185,7 +207,14 @@ const Table = () => {
185207
checked[cellInfo.row.original.id] = !checked[
186208
cellInfo.row.original.id
187209
];
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+
}
189218
const additive = checked[cellInfo.row.original.id]
190219
? 1
191220
: -1;
@@ -194,11 +223,13 @@ const Table = () => {
194223
] += additive;
195224
setDifficultyCount(difficultyCount);
196225
setChecked([...checked]);
226+
setData(filteredByCheckbox());
197227
}}
198228
/>
199229
</span>
200230
);
201231
},
232+
Filter: SelectCheckedColumnFilter,
202233
},
203234
{
204235
Header: 'Questions',
@@ -384,6 +415,10 @@ const Table = () => {
384415
defaultColumn,
385416
initialState: {
386417
filters: [
418+
{
419+
id: 'checkbox',
420+
value: localStorage.getItem('checkbox') || '',
421+
},
387422
{
388423
id: 'difficulty',
389424
value: localStorage.getItem('difficulty') || '',

Diff for: src/components/Table/styles.scss

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747

4848
.reset-button {
4949
margin-top: 10px;
50+
margin-bottom: 10px;
5051
font-size: 0.7rem;
5152
}
5253
}

0 commit comments

Comments
 (0)