Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: change browser.storage.local structure #243

Merged
merged 3 commits into from
Dec 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 49 additions & 10 deletions src/js/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,14 +186,24 @@ function assignments (node) {
*/
async function getSavedGrades (username) {
const courses = [];
const output = await browser.storage.local.get("USERDATA_" + username);
if (output !== undefined && output["USERDATA_" + username] !== undefined) {
const course_list = output["USERDATA_" + username].courses || [];
for (let i = 0; i < course_list.length; i++) {
const course = course_list[i];
courses.push(new Course(course.name, course.link, course.grade, course.finalPercent, course.assignments));
const user_data = (await browser.storage.local.get("user_data")).user_data;
if (user_data !== undefined) {
const user = user_data["USERDATA_" + username];
if (user !== undefined) {
const course_list = user.courses || [];
for (let ind = 0; ind < course_list.length; ind++) {
const course = course_list[ind];
courses.push(new Course(
course.name,
course.link,
course.grade,
course.finalPercent,
course.assignments,
));
}

return courses;
}
return courses;
}
}

Expand All @@ -204,14 +214,43 @@ async function getSavedGrades (username) {
* @param {Course[]} courses list of course objects to save
*/
async function saveGradesLocally (username, courses) {
const user_data = {};
const data = await getLocalConfig() || {};

if (data.opted_in === undefined) {
data.opted_in = {
value: true,
changed: false,
};
}

if (data.showExtensionInfo === undefined) {
data.showExtensionInfo = {
value: true,
changed: false,
};
}

const user_data = await browser.storage.local.get("user_data") || {};
const course_list = [];
for (let i = 0; i < courses.length; i++) {
course_list.push(courses[i].toObject());
}
user_data["USERDATA_" + username] = { "courses": course_list };
user_data.most_recent_user = username;
browser.storage.local.set(user_data);

data.user_data = user_data;
data.most_recent_user = username;

browser.storage.local.set(data);
}

/**
* Retrieves the config from the browser's local storage
* @async
* @returns {Config} an object representing the user's config from the browser's local storage
*/
async function getLocalConfig () {
const data = await browser.storage.local.get(null);
return data;
}

/**
Expand Down
15 changes: 9 additions & 6 deletions src/js/saspowerschoolff.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,25 +119,28 @@ function class_page () {

async function login_page () {
$('<div id="saspes-info"></div>').insertAfter('div#content');
browser.storage.local.get({ showExtensionInfo: true }).then(result => {
browser.storage.local.get("showExtensionInfo").then(output => {
new (Vue.extend(ExtensionInfo))({
data: {
showInfo: result.showExtensionInfo,
showInfo: output.showExtensionInfo.value,
},
}).$mount('#saspes-info');
});

const LastGradesDiv = document.createElement('div');
LastGradesDiv.classList.add("last-grade-div-fixed");
LastGradesDiv.id = "saspes-last-grades";
document.body.appendChild(LastGradesDiv);
if ((await browser.storage.local.get({ save_last_grades: true })).save_last_grades) {

if ((await browser.storage.local.get("opted_in")).opted_in.value) {
(browser.storage.local.get("most_recent_user")).then(output => {
if (output.most_recent_user !== undefined) {
const most_recent_user = output.most_recent_user;
if (most_recent_user !== undefined) {
(async () => {
const courses = await getSavedGrades(output.most_recent_user);
const courses = await getSavedGrades(most_recent_user);
new (Vue.extend(LastSeenGrades))({
propsData: {
username: output.most_recent_user,
username: most_recent_user,
initialCourses: courses,
},
}).$mount(".last-grade-div-fixed");
Expand Down