feat: local storage added

This commit is contained in:
Mike 2023-12-26 11:03:20 -08:00
parent 5a9f303e80
commit f18b11a22b
3 changed files with 46 additions and 6 deletions

View file

@ -0,0 +1,14 @@
function save(data) {
let json = JSON.stringify(data);
localStorage.setItem('todoList', json);
}
function load() {
const data = localStorage.getItem('todoList');
if (data) {
return data;
} else
return null;
}
export { save, load };

View file

@ -21,10 +21,17 @@ function createProject(name) {
class todoHandler {
// Create default project during construction
constructor() {
this.projects = [new createProject('default')];
constructor(projects=null) {
if (projects) {
this.projects = projects;
} else {
this.projects = [new createProject('default')];
}
}
getEverything() { return this.projects }
getTodos() {
return this.projects.map(item => item.todos);
}
@ -67,7 +74,15 @@ class todoHandler {
return todoArr.find(todo => todo.title === title);
}
addProject = name => this.projects.push(createProject(name));
addProject(name) {
let exists = this.projects.find(x => x.name === name);
if (!exists) {
this.projects.push(createProject(name));
return
}
console.log(`Project ${name} already exists!`);
}
getProjects () {
return this.projects.map(item => item.name);

View file

@ -1,15 +1,26 @@
import { todoHandler } from "./components/todo";
import { save, load } from "./components/storage";
let todos = new todoHandler();
let todos; let data = load();
// if there's local data save it in the array
if (data) {
let jsonData = JSON.parse(data);
todos = new todoHandler(jsonData);
} else {
todos = new todoHandler();
}
// todoHandler().addTodo('default', 'test', 'some stuff', 'today', 5);
todos.addProject('chores');
todos.addProject('job');
todos.addTodo('default', 'test default 2', 'some stuff', 'today', 5);
todos.addTodo('default', 'default 2', 'some stuff', 'today', 5);
// todos.addTodo('chores', 'choretest', 'some stuff', 'today', 5);
console.log(todos.getTodos());
// todos.delProject('de');
// todos.editTodo('default', 'test', 'stuffing', 'tomorrow', 4);
// console.log(todos.getTodos());
// todos.deleteTodo('chores', 'choretest');
// console.log(todos.getTodos());
//
//
save(todos.getEverything());