-
Notifications
You must be signed in to change notification settings - Fork 2
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
Comments
OK, well I added this 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:
Note that the code in 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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
The text was updated successfully, but these errors were encountered: