Skip to content

Commit 86d381d

Browse files
committed
persist starred questions after refreshing page
1 parent ca299f5 commit 86d381d

File tree

1 file changed

+24
-11
lines changed

1 file changed

+24
-11
lines changed

src/components/Table/index.js

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,22 @@ const Table = () => {
7979
window.localStorage.setItem('checkedAt', JSON.stringify(checkedAtList));
8080
}
8181

82+
let importantList =
83+
JSON.parse(localStorage.getItem('importantProblems')) ||
84+
new Array(questions.length).fill(false);
85+
86+
if (importantList.length !== questions.length) {
87+
const resizedImportantList = new Array(questions.length).fill(false);
88+
for (let i = 0; i < importantList.length; i += 1) {
89+
resizedImportantList[i] = importantList[i];
90+
}
91+
importantList = resizedImportantList;
92+
window.localStorage.setItem(
93+
'importantProblems',
94+
JSON.stringify(importantList),
95+
);
96+
}
97+
8298
const filteredByCheckbox = () => {
8399
const checkbox = localStorage.getItem('checkbox') || '';
84100
return questions.filter(question => {
@@ -123,14 +139,6 @@ const Table = () => {
123139
);
124140
const [starAnimation, setStarAnimation] = useState({});
125141

126-
// Returns an array of question objects that are starred
127-
const getStarredQuestions = () => {
128-
return questions.filter((q, idx) => important[idx]);
129-
};
130-
useEffect(() => {
131-
localStorage.setItem('importantProblems', JSON.stringify(important));
132-
}, [important]);
133-
134142
useEffect(() => {
135143
window.localStorage.setItem('checked', JSON.stringify(checked));
136144
}, [checked]);
@@ -143,6 +151,10 @@ const Table = () => {
143151
window.localStorage.setItem('showPatterns', JSON.stringify(showPatterns));
144152
}, [showPatterns]);
145153

154+
useEffect(() => {
155+
window.localStorage.setItem('importantProblems', JSON.stringify(important));
156+
}, [important]);
157+
146158
const defaultColumn = React.useMemo(
147159
() => ({
148160
Filter: DefaultColumnFilter,
@@ -619,11 +631,12 @@ const Table = () => {
619631
const [showOnlyStarred, setShowOnlyStarred] = useState(false);
620632

621633
useEffect(() => {
634+
// Always start from the full questions list
635+
let filtered = filteredByCheckbox();
622636
if (showOnlyStarred) {
623-
setData(getStarredQuestions());
624-
} else {
625-
setData(filteredByCheckbox());
637+
filtered = filtered.filter(q => important[q.id]);
626638
}
639+
setData(filtered);
627640
// eslint-disable-next-line
628641
}, [showOnlyStarred, important, checked, resetCount]);
629642

0 commit comments

Comments
 (0)