From 71eec436bdcbb5c86c11929033b6053a986e857b Mon Sep 17 00:00:00 2001
From: Donal Spring <donalspring@gmail.com>
Date: Sun, 03 Jun 2018 21:20:25 +0200
Subject: [PATCH] Merge pull request #1 from Tompage1994/exercise3/tdd-fe

---
 src/store/actions.js |   44 ++++++++++++++++++++++++++++++++------------
 1 files changed, 32 insertions(+), 12 deletions(-)

diff --git a/src/store/actions.js b/src/store/actions.js
index ce60e19..a1618a7 100644
--- a/src/store/actions.js
+++ b/src/store/actions.js
@@ -3,24 +3,27 @@
 
 const dummyData = [
   {
-    title: "Learn awesome things about Labs",
+    _id: 0,
+    title: "Learn awesome things about Labs ����",
     completed: false,
     important: false
   },
   {
-    title: "Learn about my friend Jenkins",
+    _id: 1,
+    title: "Learn about my friend Jenkins ����",
     completed: true,
     important: false
   },
   {
-    title: "Have a poop",
+    _id: 2,
+    title: "Drink Coffee �������",
     completed: false,
     important: true
   }
 ];
 export default {
   loadTodos({ commit }) {
-    axios
+    return axios
       .get(config.todoEndpoint)
       .then(r => r.data)
       .then(todos => {
@@ -46,9 +49,20 @@
       completed: false,
       important: false
     };
-    axios.post(config.todoEndpoint, todo).then(mongoTodo => {
-      commit("ADD_TODO", mongoTodo.data);
-    });
+    // console.info("TESTINT BLAH BLAH ", todo);
+    return axios
+      .post(config.todoEndpoint, todo)
+      .then(mongoTodo => {
+        commit("ADD_TODO", mongoTodo.data);
+      })
+      .catch(err => {
+        if (err) {
+          console.info("INFO - Adding dummy todo because of ", err);
+          let mongoTodo = todo;
+          mongoTodo._id = "fake-todo-item-" + Math.random();
+          commit("ADD_TODO", mongoTodo);
+        }
+      });
   },
   setNewTodo({ commit }, todo) {
     // debugger
@@ -81,14 +95,20 @@
     }
     //  2 return array of promises and resolve all
   },
-  markTodoCompleted({ commit, state }, id) {
+  /* eslint: ignore */
+  updateTodo({ commit, state }, { id, important }) {
     let i = state.todos.findIndex(todo => todo._id === id);
-    // todo - add back end
-    axios
+    if (important) {
+      // TODO - add commit imporant here!
+      commit("MARK_TODO_IMPORTANT", i);
+    } else {
+      commit("MARK_TODO_COMPLETED", i);
+    }
+    // Fire and forget style backend update ;)
+    return axios
       .put(config.todoEndpoint + "/" + state.todos[i]._id, state.todos[i])
       .then(data => {
-        console.info("INFO - item " + id + " updated", data);
+        console.log("INFO - item " + id + " updated");
       });
-    commit("MARK_TODO_COMPLETED", i);
   }
 };

--
Gitblit v1.9.3