donal
2018-04-04 68b54eb47f70cfca04d8fe3d25edaf8a4545e405
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
import axios from "axios";
import config from "@/config";
 
const dummyData = [
  {
    title: "Learn awesome things about Labs",
    completed: false,
    important: false
  },
  {
    title: "Learn about my friend Jenkins",
    completed: true,
    important: false
  },
  {
    title: "Have a poop",
    completed: false,
    important: true
  }
];
export default {
  loadTodos({ commit }) {
    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,
      important: false
    };
    axios.post(config.todoEndpoint, todo).then(mongoTodo => {
      commit("ADD_TODO", mongoTodo.data);
    });
  },
  setNewTodo({ commit }, todo) {
    // debugger
    commit("SET_NEW_TODO", todo);
  },
  updateTodo({ commit, state }, todo) {
    // const todo = state.newTodo
    // debugger;
    const foundIndex = state.todos.findIndex(obj => obj.id === todo.id);
    state.todos[foundIndex] = todo;
    const newUpdatedArray = state.todos;
    commit("UPDATE_TODO", newUpdatedArray);
  },
  clearNewTodo({ commit }) {
    commit("CLEAR_NEW_TODO");
  },
  clearAllTodos({ commit }) {
    commit("CLEAR_ALL_TODOS");
  },
  clearAllDoneTodos({ commit, state }) {
    // 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);
        });
      }
    });
    //  2 return array of promises and resolve all
    commit("CLEAR_ALL_DONE_TODOS");
  }
};