From 3790e12b327a91aad7a39e07155ab4ded6d09037 Mon Sep 17 00:00:00 2001 From: Marc Abramowitz Date: Wed, 29 Jun 2022 00:27:16 -0700 Subject: [PATCH] Update views when new todo added Detect new todo by seeing whether the `id` already exists rather than checking if the `id === 0`, which is not true. Fixes: GH-3 --- frontend/stores/store.ts | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/frontend/stores/store.ts b/frontend/stores/store.ts index cb009b1..6e51293 100644 --- a/frontend/stores/store.ts +++ b/frontend/stores/store.ts @@ -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]; }