Skip to content

Commit cda3ae8

Browse files
authored
Last Solved On Column Added to Table (#320)
1 parent e672330 commit cda3ae8

File tree

3 files changed

+127
-58
lines changed

3 files changed

+127
-58
lines changed

package-lock.json

+80-58
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/components/Table/index.js

+43
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ const Table = () => {
4545
JSON.parse(localStorage.getItem('checked')) ||
4646
new Array(questions.length).fill(false);
4747

48+
let checkedAtList =
49+
JSON.parse(localStorage.getItem('checkedAt')) ||
50+
new Array(questions.length).fill('');
51+
4852
/* If the user has previously visited the website, then an array in
4953
LocalStorage would exist of a certain length which corresponds to which
5054
questions they have/have not completed. In the event that we add new questions
@@ -62,6 +66,17 @@ const Table = () => {
6266
window.localStorage.setItem('checked', JSON.stringify(checkedList));
6367
}
6468

69+
if (checkedAtList.length !== questions.length) {
70+
const resizedCheckedAtList = new Array(questions.length).fill('');
71+
72+
for (let i = 0; i < checkedAtList.length; i += 1) {
73+
resizedCheckedAtList[i] = checkedAtList[i];
74+
}
75+
76+
checkedAtList = resizedCheckedAtList;
77+
window.localStorage.setItem('checkedAt', JSON.stringify(checkedAtList));
78+
}
79+
6580
const filteredByCheckbox = () => {
6681
const checkbox = localStorage.getItem('checkbox') || '';
6782
return questions.filter(question => {
@@ -91,6 +106,7 @@ const Table = () => {
91106
totalDifficultyCount[questions[i].difficulty] += 1;
92107
}
93108

109+
const [checkedAt, setCheckedAt] = useState(checkedAtList);
94110
const [data, setData] = useState(filteredByCheckbox());
95111
const [difficultyCount, setDifficultyCount] = useState(difficultyMap);
96112
const [checked, setChecked] = useState(checkedList);
@@ -102,6 +118,10 @@ const Table = () => {
102118
window.localStorage.setItem('checked', JSON.stringify(checked));
103119
}, [checked]);
104120

121+
useEffect(() => {
122+
window.localStorage.setItem('checkedAt', JSON.stringify(checkedAt));
123+
}, [checkedAt]);
124+
105125
useEffect(() => {
106126
window.localStorage.setItem('showPatterns', JSON.stringify(showPatterns));
107127
}, [showPatterns]);
@@ -134,6 +154,8 @@ const Table = () => {
134154
const [resetModal, setResetModal] = React.useState(false);
135155
const toggleResetModal = () => {
136156
setResetModal(!resetModal);
157+
const clearedCheckedAt = checkedAt.map(() => null);
158+
setCheckedAt(clearedCheckedAt);
137159
};
138160

139161
return (
@@ -200,6 +222,13 @@ const Table = () => {
200222
checked[cellInfo.row.original.id] = !checked[
201223
cellInfo.row.original.id
202224
];
225+
const currentTime = new Date().toISOString().slice(0, 10);
226+
// const updatedCheckedAt = [...checkedAt];
227+
checkedAt[cellInfo.row.original.id] = checked[
228+
cellInfo.row.original.id
229+
]
230+
? currentTime
231+
: null;
203232
const question = questions.find(
204233
q => q.id === cellInfo.row.original.id,
205234
);
@@ -218,6 +247,7 @@ const Table = () => {
218247
setDifficultyCount(difficultyCount);
219248
setChecked([...checked]);
220249
setData(filteredByCheckbox());
250+
setCheckedAt([...checkedAt]);
221251
}}
222252
/>
223253
</span>
@@ -451,6 +481,19 @@ const Table = () => {
451481
},
452482
Filter: SelectColumnFilter,
453483
},
484+
{
485+
Header: 'Last Solved On',
486+
accessor: 'LastSolvedOn',
487+
disableSortBy: true,
488+
Cell: cellInfo => {
489+
return (
490+
<div className="lastSolvedOn">
491+
{checkedAt[cellInfo.row.original.id]}
492+
</div>
493+
);
494+
},
495+
disableFilters: true,
496+
},
454497
],
455498
},
456499
],

src/components/Table/styles.scss

+4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
background: white;
1313
position: sticky;
1414
top: 0;
15+
text-wrap: nowrap;
1516
}
1617
}
1718

@@ -67,4 +68,7 @@
6768
margin-bottom: 10px;
6869
font-size: 0.7rem;
6970
}
71+
.lastSolvedOn {
72+
text-wrap: nowrap;
73+
}
7074
}

0 commit comments

Comments
 (0)