Created
January 20, 2019 19:46
-
-
Save funador/45b6baba42e7ad126b526bd34b0c488a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const store = (() => { | |
const todos = [] | |
function addToStore(todo) { | |
todo.editing = false | |
this.todos = [todo, ...this.todos] | |
} | |
function deleteFromStore(id) { | |
this.todos = this.todos.filter(todo => todo.id !== id) | |
} | |
function setEditing(id) { | |
this.todos = this.todos.map(todo => { | |
if(todo.id == id) { | |
todo.editing = !todo.editing | |
} | |
return todo | |
}) | |
} | |
function findById(id) { | |
return this.todos.find(todo => todo.id == id) | |
} | |
function updateInStore(updated) { | |
this.todos = this.todos.map(todo => | |
todo.id == updated.id | |
? Object.assign(todo, updated) | |
: todo | |
) | |
} | |
function addTodosOnLoad(todos) { | |
this.todos = todos.reverse().map(todo => { | |
todo.editing = false | |
return todo | |
}) | |
} | |
return { | |
todos, | |
addToStore, | |
deleteFromStore, | |
setEditing, | |
findById, | |
updateInStore, | |
addTodosOnLoad | |
} | |
})() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment