Created
August 12, 2019 16:43
-
-
Save zaydek-old/a413b013a41d18baa4255255cffdadad to your computer and use it in GitHub Desktop.
This file contains hidden or 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 methods = (state: State) => ({ | |
clearAll() { | |
return initialState | |
}, | |
focus() { | |
state.focus = true | |
}, | |
blur() { | |
state.focus = false | |
}, | |
input(value: string) { | |
state.input = value | |
}, | |
addTodo() { | |
if (state.focus && state.input) { | |
state.todos.unshift({id: shortID(), value: state.input, complete: false}) | |
state.input = "" | |
} | |
}, | |
toggleTodo(id: string) { | |
const todo = state.todos.find(todo => id === todo.id) | |
if (todo) { | |
todo.complete = !todo.complete | |
} | |
}, | |
updateTodo(id: string, value: string) { | |
const todo = state.todos.find(todo => id === todo.id) | |
if (todo) { | |
todo.value = value | |
} | |
}, | |
deleteTodo(id: string) { | |
const index = state.todos.findIndex(todo => id === todo.id) | |
if (index !== -1) { | |
state.todos.splice(index, 1) | |
} | |
}, | |
deleteAllTodos() { | |
// Use `splice` backwards to avoid destructive resizing. | |
// See djave.co.uk/blog/read/splice-doesnt-work-very-well-in-a-javascript-for-loop for reference. | |
for (let index = state.todos.length; index >= 0; index--) { | |
const todo = state.todos[index] | |
if (todo && todo.complete) { | |
state.todos.splice(index, 1) | |
} | |
} | |
}, | |
saving() { | |
state.saved = false | |
}, | |
saved() { | |
state.saved = true | |
}, | |
undo() { | |
}, | |
redo() { | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment