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, |
b3852f
|
49 |
completed: false, |
T |
50 |
important: false |
82de6c
|
51 |
}; |
0517f2
|
52 |
// console.info("TESTINT BLAH BLAH ", todo); |
D |
53 |
return axios |
1d0cfc
|
54 |
.post(config.todoEndpoint, todo) |
D |
55 |
.then(mongoTodo => { |
|
56 |
commit("ADD_TODO", mongoTodo.data); |
|
57 |
}) |
|
58 |
.catch(err => { |
|
59 |
if (err) { |
|
60 |
console.info("INFO - Adding dummy todo because of ", err); |
|
61 |
let mongoTodo = todo; |
|
62 |
mongoTodo._id = "fake-todo-item-" + Math.random(); |
|
63 |
commit("ADD_TODO", mongoTodo); |
|
64 |
} |
|
65 |
}); |
82de6c
|
66 |
}, |
e38844
|
67 |
setNewTodo({ commit }, todo) { |
8f43d3
|
68 |
// debugger |
e38844
|
69 |
commit("SET_NEW_TODO", todo); |
530b0a
|
70 |
}, |
82de6c
|
71 |
clearNewTodo({ commit }) { |
D |
72 |
commit("CLEAR_NEW_TODO"); |
fbee7b
|
73 |
}, |
3b0900
|
74 |
clearTodos({ commit, state }, all) { |
68b54e
|
75 |
// 1 fire and forget or |
ba91cd
|
76 |
const deleteStuff = id => { |
D |
77 |
axios.delete(config.todoEndpoint + "/" + id).then(data => { |
3b0900
|
78 |
console.info("INFO - item " + id + " deleted", data); |
D |
79 |
}); |
|
80 |
}; |
|
81 |
|
|
82 |
if (all) { |
ba91cd
|
83 |
state.todos.map(todo => { |
D |
84 |
deleteStuff(todo._id); |
3b0900
|
85 |
}); |
D |
86 |
commit("CLEAR_ALL_TODOS"); |
|
87 |
} else { |
|
88 |
state.todos.map(todo => { |
|
89 |
// axios remove all done by the id |
|
90 |
if (todo.completed) { |
ba91cd
|
91 |
deleteStuff(todo._id); |
3b0900
|
92 |
} |
D |
93 |
}); |
|
94 |
commit("CLEAR_ALL_DONE_TODOS"); |
|
95 |
} |
68b54e
|
96 |
// 2 return array of promises and resolve all |
ba91cd
|
97 |
}, |
a663e9
|
98 |
/* eslint: ignore */ |
D |
99 |
updateTodo({ commit, state }, { id, important }) { |
ba91cd
|
100 |
let i = state.todos.findIndex(todo => todo._id === id); |
6191f0
|
101 |
if (important) { |
e351bf
|
102 |
// TODO - add commit imporant here! |
6191f0
|
103 |
commit("MARK_TODO_IMPORTANT", i); |
A |
104 |
} else { |
|
105 |
commit("MARK_TODO_COMPLETED", i); |
|
106 |
} |
12f5dd
|
107 |
// Fire and forget style backend update ;) |
0517f2
|
108 |
return axios |
ba91cd
|
109 |
.put(config.todoEndpoint + "/" + state.todos[i]._id, state.todos[i]) |
D |
110 |
.then(data => { |
12f5dd
|
111 |
console.log("INFO - item " + id + " updated"); |
ba91cd
|
112 |
}); |
e38844
|
113 |
} |
82de6c
|
114 |
}; |