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