Skip to content

Commit 0dc2e45

Browse files
committed
fix: check if local data is missing config data
Added a function `getLocalConfig` to retrieve everything in browser.storage.local. According to MDN, passing a `null` or `undefined` value retrieves the entire storage contents, so that is what I did. Wrapped in a function for better readability and understanding. In `saveGradesLocally`, I used the `getLocalConfig` function to get the existing config. Then, checks are performed to see if the config values exist, and to add them if they don't. Before, `user_data` also erased previously stored user data. By attempting to retrieve it from the local storage first, this prevents that from happening. Signed-off-by: Lucas Sta Maria <[email protected]>
1 parent ef6b90e commit 0dc2e45

File tree

1 file changed

+27
-10
lines changed

1 file changed

+27
-10
lines changed

src/js/helpers.js

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -214,28 +214,45 @@ async function getSavedGrades (username) {
214214
* @param {Course[]} courses list of course objects to save
215215
*/
216216
async function saveGradesLocally (username, courses) {
217-
const data = {};
218-
const user_data = {};
217+
const data = await getLocalConfig() || {};
218+
219+
if (data.opted_in === undefined) {
220+
data.opted_in = {
221+
value: true,
222+
changed: false,
223+
};
224+
}
225+
226+
if (data.showExtensionInfo === undefined) {
227+
data.showExtensionInfo = {
228+
value: true,
229+
changed: false,
230+
};
231+
}
232+
233+
const user_data = await browser.storage.local.get("user_data") || {};
219234
const course_list = [];
220235
for (let i = 0; i < courses.length; i++) {
221236
course_list.push(courses[i].toObject());
222237
}
223238
user_data["USERDATA_" + username] = { "courses": course_list };
224239

225240
data.user_data = user_data;
226-
data.opted_in = {
227-
value: true,
228-
changed: false,
229-
};
230-
data.showExtensionInfo = {
231-
value: true,
232-
changed: false,
233-
};
234241
data.most_recent_user = username;
235242

236243
browser.storage.local.set(data);
237244
}
238245

246+
/**
247+
* Retrieves the config from the browser's local storage
248+
* @async
249+
* @returns {Config} an object representing the user's config from the browser's local storage
250+
*/
251+
async function getLocalConfig () {
252+
const data = await browser.storage.local.get(null);
253+
return data;
254+
}
255+
239256
/**
240257
* Send Analytics ping
241258
* @param {String} action_input the action being taken

0 commit comments

Comments
 (0)