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