Skip to content

Instantly share code, notes, and snippets.

@zaydek-old
Created August 12, 2019 16:43
Show Gist options
  • Save zaydek-old/a413b013a41d18baa4255255cffdadad to your computer and use it in GitHub Desktop.
Save zaydek-old/a413b013a41d18baa4255255cffdadad to your computer and use it in GitHub Desktop.
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