Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

View doesn't update when new todo is added #3

Open
msabramo opened this issue Jun 29, 2022 · 1 comment · May be fixed by #4
Open

View doesn't update when new todo is added #3

msabramo opened this issue Jun 29, 2022 · 1 comment · May be fixed by #4

Comments

@msabramo
Copy link

The view doesn't update when I add a new todo. I have to refresh the page to see the new todo.

See video:

Screen.Recording.2022-06-28.at.10.51.31.PM.mov
@msabramo
Copy link
Author

msabramo commented Jun 29, 2022

OK, well I added this console.log:

diff --git a/frontend/stores/store.ts b/frontend/stores/store.ts
index cb009b1..a31a1d5 100644
--- a/frontend/stores/store.ts
+++ b/frontend/stores/store.ts
@@ -18,6 +18,7 @@ class Store {

   async saveTodo(todo: Todo) {
     const saved = await endpoint.saveTodo(todo);
+    console.log('saveTodo saved:>> ', saved);
     if (todo.id === 0) {
       this.addTodo(saved);
     } else {

and when I add a new todo, the output is:

saveTodo saved:>>  {id: 13, task: 'new todo', done: false}

Note that the code in saveTodo is expecting the id of a new todo to be 0 and it is not.

Furthermore, if I change the code to detect if the todo is new without relying on the id to be 0, then it works properly:

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];
   }

msabramo added a commit to msabramo/vaadin-fusion-mobx that referenced this issue Jun 29, 2022
Detect new todo by seeing whether the `id` already exists rather than
checking if the `id === 0`, which is not true.

Fixes: marcushellbergGH-3
@msabramo msabramo linked a pull request Jun 29, 2022 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant