Skip to content

Commit

Permalink
Update views when new todo added
Browse files Browse the repository at this point in the history
Detect new todo by seeing whether the `id` already exists rather than
checking if the `id === 0`, which is not true.

Fixes: marcushellbergGH-3
  • Loading branch information
msabramo committed Jun 29, 2022
1 parent 5a7a16a commit 3790e12
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion frontend/stores/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,21 @@ class Store {

async saveTodo(todo: Todo) {
const saved = await endpoint.saveTodo(todo);
if (todo.id === 0) {
if (this.isNewTodo(todo)) {
this.addTodo(saved);
} else {
this.updateTodo(saved);
}
}

private todoExists(todo: Todo) {
return this.todos.some((t) => t.id === todo.id);
}

private isNewTodo(todo: Todo) {
return !this.todoExists(todo);
}

private addTodo(todo: Todo) {
this.todos = [...this.todos, todo];
}
Expand Down

0 comments on commit 3790e12

Please sign in to comment.