donal
2018-04-04 3b0900d98adcb4a95950610c0f295d46fb57c53d
Tidy up of the delete flow
2 files modified
39 ■■■■■ changed files
src/components/ListOfTodos.vue 5 ●●●●● patch | view | raw | blame | history
src/store/actions.js 34 ●●●●● patch | view | raw | blame | history
src/components/ListOfTodos.vue
@@ -54,10 +54,11 @@
      );
    },
    clearDoneTodos() {
      this.$store.dispatch("clearAllDoneTodos");
      this.$store.dispatch("clearTodos");
    },
    clearTodos() {
      this.$store.dispatch("clearAllTodos");
      // NOTE - true = all todos
      this.$store.dispatch("clearTodos", true);
    }
  }
};
src/store/actions.js
@@ -64,20 +64,28 @@
  clearNewTodo({ commit }) {
    commit("CLEAR_NEW_TODO");
  },
  clearAllTodos({ commit }) {
    commit("CLEAR_ALL_TODOS");
  },
  clearAllDoneTodos({ commit, state }) {
  clearTodos({ commit, state }, all) {
    // 1 fire and forget or
    state.todos.map(todo => {
      // axios remove all done by the id
      if (todo.completed){
        axios.delete(config.todoEndpoint + '/' + todo._id).then(data => {
          console.info("INFO - item " + todo._id + " deleted", data);
        });
      }
    });
    const deleteStuff = (id) => {
      axios.delete(config.todoEndpoint + '/' + id).then(data => {
        console.info("INFO - item " + id + " deleted", data);
      });
    };
    if (all) {
      state.todos.map(todo => {
        deleteStuff(todo._id)
      });
      commit("CLEAR_ALL_TODOS");
    } else {
      state.todos.map(todo => {
        // axios remove all done by the id
        if (todo.completed) {
          deleteStuff(todo._id)
        }
      });
      commit("CLEAR_ALL_DONE_TODOS");
    }
    //  2 return array of promises and resolve all
    commit("CLEAR_ALL_DONE_TODOS");
  }
};