From f18b11a22b5dbe89646e950fda4b7cd30fb63d3f Mon Sep 17 00:00:00 2001 From: Mike Smith <89040888+smiggiddy@users.noreply.github.com> Date: Tue, 26 Dec 2023 11:03:20 -0800 Subject: [PATCH] feat: local storage added --- todo/src/components/storage.js | 14 ++++++++++++++ todo/src/components/todo.js | 21 ++++++++++++++++++--- todo/src/index.js | 17 ++++++++++++++--- 3 files changed, 46 insertions(+), 6 deletions(-) create mode 100644 todo/src/components/storage.js diff --git a/todo/src/components/storage.js b/todo/src/components/storage.js new file mode 100644 index 0000000..74b626f --- /dev/null +++ b/todo/src/components/storage.js @@ -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 }; diff --git a/todo/src/components/todo.js b/todo/src/components/todo.js index 77fd15c..198f09f 100644 --- a/todo/src/components/todo.js +++ b/todo/src/components/todo.js @@ -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); diff --git a/todo/src/index.js b/todo/src/index.js index a6247df..cd90e47 100644 --- a/todo/src/index.js +++ b/todo/src/index.js @@ -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());