donal
2018-04-16 78a403c450425ea86fad7df5737a4a53ed4c6714
commit | author | age
82de6c 1 import axios from "axios";
D 2 import config from "@/config";
3
e38844 4 const dummyData = [
D 5   {
12f5dd 6     _id: 0,
1d0cfc 7     title: "Learn awesome things about Labs ðŸ”¬",
e38844 8     completed: false,
D 9     important: false
10   },
11   {
12f5dd 12     _id: 1,
1d0cfc 13     title: "Learn about my friend Jenkins ðŸŽ‰",
e38844 14     completed: true,
D 15     important: false
16   },
17   {
12f5dd 18     _id: 2,
1d0cfc 19     title: "Drink Coffee â˜•ðŸ’©",
e38844 20     completed: false,
D 21     important: true
22   }
23 ];
82de6c 24 export default {
D 25   loadTodos({ commit }) {
efef31 26     return axios
82de6c 27       .get(config.todoEndpoint)
D 28       .then(r => r.data)
29       .then(todos => {
30         commit("SET_TODOS", todos);
31         commit("SET_LOADING", false);
ba91cd 32       })
D 33       .catch(err => {
e38844 34         if (err) {
ba91cd 35           console.info("INFO - setting dummy data because of ", err);
e38844 36           commit("SET_TODOS", dummyData);
D 37           commit("SET_LOADING", false);
38         }
82de6c 39       });
D 40   },
41   addTodo({ commit, state }) {
42     if (!state.newTodo) {
43       // do not add empty todos
44       return;
45     }
e38844 46     // debugger
82de6c 47     const todo = {
D 48       title: state.newTodo,
0517f2 49       completed: false
82de6c 50     };
0517f2 51     // console.info("TESTINT BLAH BLAH ", todo);
D 52     return axios
1d0cfc 53       .post(config.todoEndpoint, todo)
D 54       .then(mongoTodo => {
55         commit("ADD_TODO", mongoTodo.data);
56       })
57       .catch(err => {
58         if (err) {
59           console.info("INFO - Adding dummy todo because of ", err);
60           let mongoTodo = todo;
61           mongoTodo._id = "fake-todo-item-" + Math.random();
62           commit("ADD_TODO", mongoTodo);
63         }
64       });
82de6c 65   },
e38844 66   setNewTodo({ commit }, todo) {
8f43d3 67     // debugger
e38844 68     commit("SET_NEW_TODO", todo);
530b0a 69   },
82de6c 70   clearNewTodo({ commit }) {
D 71     commit("CLEAR_NEW_TODO");
fbee7b 72   },
3b0900 73   clearTodos({ commit, state }, all) {
68b54e 74     // 1 fire and forget or
ba91cd 75     const deleteStuff = id => {
D 76       axios.delete(config.todoEndpoint + "/" + id).then(data => {
3b0900 77         console.info("INFO - item " + id + " deleted", data);
D 78       });
79     };
80
81     if (all) {
ba91cd 82       state.todos.map(todo => {
D 83         deleteStuff(todo._id);
3b0900 84       });
D 85       commit("CLEAR_ALL_TODOS");
86     } else {
87       state.todos.map(todo => {
88         // axios remove all done by the id
89         if (todo.completed) {
ba91cd 90           deleteStuff(todo._id);
3b0900 91         }
D 92       });
93       commit("CLEAR_ALL_DONE_TODOS");
94     }
68b54e 95     //  2 return array of promises and resolve all
ba91cd 96   },
a663e9 97   /* eslint: ignore */
D 98   updateTodo({ commit, state }, { id, important }) {
ba91cd 99     let i = state.todos.findIndex(todo => todo._id === id);
6191f0 100     if (important) {
78a403 101       // TODO - add commit imporant here!
6191f0 102     } else {
A 103       commit("MARK_TODO_COMPLETED", i);
104     }
12f5dd 105     // Fire and forget style backend update ;)
0517f2 106     return axios
ba91cd 107       .put(config.todoEndpoint + "/" + state.todos[i]._id, state.todos[i])
D 108       .then(data => {
12f5dd 109         console.log("INFO - item " + id + " updated");
ba91cd 110       });
e38844 111   }
82de6c 112 };