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

Added the feature of locally storing the tasks in the browser using c… #10

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
95 changes: 89 additions & 6 deletions script.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,31 @@ let pomodoro = "pomodoro"
let pomodorosCompleted = 0
let selectedTaskElement


const templateClone = template.content.cloneNode(true)
const listItem = templateClone.querySelector('.list-item')
const checkbox = templateClone.querySelector('input[type=checkbox]')

const taskName = templateClone.querySelector('.task-name')
const pomodoroCount = templateClone.querySelector('.pomodoro-count')

chrome.storage.local.get('pomo', function (result) {
// console.log(result.pomo)
if(result){
for(let x=0; x<result.pomo.length; x++){
const newTask = {
name: result.pomo[x].newTask.name,
completedPomodoros: result.pomo[x].newTask.completedPomodoros,
totalPomodoros: result.pomo[x].newTask.totalPomodoros,
complete: result.pomo[x].newTask.complete,
id: result.pomo[x].newTask.id
}

addTask(newTask ,false);
}
}
});

let array = ["minutes","seconds","pause","countdownTimer","pbutton"];
chrome.storage.sync.get(array,function(value){
if(!chrome.runtime.error){
Expand Down Expand Up @@ -220,10 +245,28 @@ saveBtn.addEventListener('click', e => {
complete: false,
id: new Date().valueOf().toString()
}

// add task to array
tasks.push(newTask)
// render task
addTask(newTask)


// Local Chrome Storage

chrome.storage.local.get({pomo: []}, function (result) {

var pomo = result.pomo;
pomo.push({newTask: newTask});

chrome.storage.local.set({pomo: pomo}, function () {

chrome.storage.local.get('pomo', function (result) {
console.log(result.pomo)
});
});
});


addTask(newTask, true);

// clear inputs
taskNameInput.value = ""
Expand All @@ -240,6 +283,28 @@ document.addEventListener('click', e => {

// find the id of the task to remove the task object from the array
const taskId = listItem.dataset.taskId

// console.log(taskId) -> Correct Id

chrome.storage.local.get('pomo', function (result) {

var pomo = result.pomo;

pomo = pomo.filter((item, i) => {
// console.log(i);
// console.log(item.newTask.id);
return item.newTask.id !== taskId
})
// console.log("pomo" + pomo);

chrome.storage.local.set({pomo: pomo}, function () {

chrome.storage.local.get('pomo', function (result) {
console.log(result.pomo)
});
});
});

tasks = tasks.filter(task => task.id !== taskId )

// remove title when selected task is deleted
Expand Down Expand Up @@ -270,17 +335,35 @@ document.addEventListener('click', e => {
})

// add task as list item
function addTask(task) {
function addTask(task, append_pomo) {
const templateClone = template.content.cloneNode(true)
const listItem = templateClone.querySelector('.list-item')
listItem.dataset.taskId = task.id
const checkbox = templateClone.querySelector('input[type=checkbox]')
checkbox.checked = task.complete

const taskName = templateClone.querySelector('.task-name')
taskName.innerHTML = task.name
const pomodoroCount = templateClone.querySelector('.pomodoro-count')
pomodoroCount.innerHTML = task.completedPomodoros.toString() + '/' + task.totalPomodoros
tasksList.appendChild(templateClone)


if(append_pomo){
chrome.storage.local.get('pomo', function (result) {
console.log(result.pomo.length);
if(result){
taskName.innerHTML = task.name;
pomodoroCount.innerHTML = task.completedPomodoros.toString() + '/' + task.totalPomodoros;
tasksList.appendChild(templateClone)
console.log(task.id + "\n");
}
});
}else{
// console.log(task);
taskName.innerHTML = task.name

pomodoroCount.innerHTML = task.completedPomodoros.toString() + '/' + task.totalPomodoros
tasksList.appendChild(templateClone)
console.log(task.id + "\n");
}
}

// countdown function
Expand Down