I’m doing simple to-do app and want to add the data of my projects to the local storage but since my array of Projects is made of project object which itself has array of tasks,I have trouble figuring out how to add tasks to right projects when accesing Local storage.
export class Task{
constructor(title,description,dueDate,priority){
this.title=title;
this.description=description;
this.dueDate=new Date(dueDate);
this.priority=priority;
this.isDone=false;
}
setDone(){
this.isDone=true;
}
}
export class Project{
constructor(title){
this.title=title;
this.projectTasks=[];
}
addToProject(task){
this.projectTasks.push(task);
}
}
Here I succesfully add project to the LS but the array is obviously empty.
export function addNewProject(title){
const project=new Project(title);
projectList.push(project);
localStorage.setItem('projects',JSON.stringify(project));
displayProjects();
}
Here I’m adding new task to the project locally but don’t know how to add it to LS as well.
export function addNewTask(e, project) {
e.preventDefault();
let title = document.getElementById("ftitle").value;
let description = document.getElementById("fdesc").value;
let dueDate = document.getElementById("fdate").value;
let priority = document.getElementById("fpriority").value;
let task = new Task(title, description, dueDate, priority);
project.addToProject(task);
displayTasks(project);
}
I’d really apreciate some info about how to load it again correctly
1