Skip to content

Commit

Permalink
Merge pull request #159 from mozillabrasil/issue-153
Browse files Browse the repository at this point in the history
Permanently store questions for 24 hours
  • Loading branch information
WesleyBranton authored Sep 22, 2020
2 parents 678ac12 + f930e64 commit b98e703
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 29 deletions.
40 changes: 31 additions & 9 deletions src/js/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,13 @@ function handleMessage(message, sender, sendResponse) {
*/
function dataLoaded(data) {
// Load question list
if (data.questions) {
if (data.questions && data.questionVersion == 1) {
questionList = data.questions;
} else {
questionList = [];
browser.storage.local.set({
questionVersion: 1
});
}

// Load question check frequency
Expand Down Expand Up @@ -299,6 +302,7 @@ function loadRequest(request) {

let responseSUMO = request.response;
let newQuestionList = [];
let reviveQuestionList = [];

for (i = 0; i < responseSUMO.results.length; i++) {
// Check if question should be shown on the question list
Expand All @@ -310,6 +314,7 @@ function loadRequest(request) {
let qTitle = responseSUMO.results[i].title;
let qLocale = responseSUMO.results[i].locale;
let qProduct = responseSUMO.results[i].product;
let qTime = responseSUMO.results[i].created;
let x = 0;
let questionExists = false;

Expand All @@ -319,13 +324,21 @@ function loadRequest(request) {
x++;
}

// If the question is on the list, make sure it's shown
if (questionExists && x < questionList.length) {
questionList[x].show = true;
reviveQuestionList.push(questionList[x]);
}

// Add to the question list (if needed)
if (!questionExists) {
let newItem = {
product: qProduct,
title: qTitle,
id: qID,
locale: qLocale,
created: qTime,
show: true,
new: true
}
newQuestionList.push(newItem);
Expand Down Expand Up @@ -355,7 +368,7 @@ function loadRequest(request) {
updateQuestionList();
browser.runtime.sendMessage({
task: 'add_new_questions',
questions: newQuestionList,
questions: newQuestionList.concat(reviveQuestionList),
isFinishedLoading: isFinishedLoading
});
}
Expand All @@ -373,7 +386,7 @@ function updateQuestionCount() {

// Count new questions
for (var i = 0; i < questionList.length; i++) {
if (questionList[i].new) {
if (questionList[i].new && questionList[i].show) {
numberOfQuestionsOpened++;
}
}
Expand Down Expand Up @@ -422,30 +435,39 @@ function removeOld(questions, productToCheck, localeToCheck) {
let i = 0;
while (i < questionList.length) {
let x = 0;
let found = false;
let skip = false;
let keep = false;
let matchesList = (questionList[i].product.toLowerCase() == productToCheck) && (questionList[i].locale.toLowerCase() == localeToCheck.toLowerCase());
let isPossible = productList.includes(questionList[i].product.toLowerCase()) && localeList.includes(questionList[i].locale.toLowerCase());

// Question is for a product on the watch list, but not for the product currently being checked (keep the question in the list)
if (isPossible && !matchesList) {
found = true;
keep = true;
}

// Check if question is still on list
while (x < questions.length && !found && matchesList) {
while (x < questions.length && !keep && matchesList) {
if (questionList[i].id == questions[x].id &&
questions[x].num_answers == 0 &&
questions[x].is_locked == false &&
questions[x].is_spam == false &&
isWithinTimeRange(questions[x].created)) {
found = true;
keep = true;
}
x++;
}

// Check if question is within 24 hours
if (!keep && isWithinTimeRange(questionList[i].created)) {
keep = true;
questionList[i].show = false;
browser.runtime.sendMessage({
task: 'remove_question',
id: questionList[i].id
});
}

// Remove from question list (if not valid)
if (!found || !isPossible) {
if (!keep || !isPossible) {
browser.runtime.sendMessage({
task: 'remove_question',
id: questionList[i].id
Expand Down
32 changes: 12 additions & 20 deletions src/js/popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,18 +124,8 @@ function dataLoaded(data) {
showLocaleLabels(data.chooseLanguage.length != 1);

questionList = data.questions;

for (i = 0; i < questionList.length; i++) {
createQuestionUI(
questionList[i].product.toLowerCase(),
questionList[i].title,
questionList[i].id,
questionList[i].locale,
questionList[i].new
);
}

toggleQuestionList();
addQuestions(questionList, false);

document.getElementById('page-loader').style.display = 'none';
callAPI();
}
Expand Down Expand Up @@ -186,13 +176,15 @@ function callAPI(event) {
*/
function addQuestions(questions, isFinishedLoading) {
for (i = 0; i < questions.length; i++) {
createQuestionUI(
questions[i].product,
questions[i].title,
questions[i].id,
questions[i].locale,
true
);
if (document.getElementsByClassName('item--' + questions[i].id)[0] == undefined && questions[i].show) {
createQuestionUI(
questions[i].product,
questions[i].title,
questions[i].id,
questions[i].locale,
questions[i].new
);
}
}

toggleQuestionList();
Expand Down Expand Up @@ -315,7 +307,7 @@ async function markAllAsRead(event) {
*/
function removeQuestion(id) {
let question = document.getElementsByClassName('item--' + id)[0];
question.parentElement.removeChild(question);
if (question) question.parentElement.removeChild(question);
}

/**
Expand Down

0 comments on commit b98e703

Please sign in to comment.