commit | author | age
|
82de6c
|
1 |
import axios from "axios"; |
D |
2 |
import config from "@/config"; |
|
3 |
|
e38844
|
4 |
const dummyData = [ |
D |
5 |
{ |
|
6 |
title: "Learn awesome things about Labs", |
|
7 |
completed: false, |
|
8 |
important: false |
|
9 |
}, |
|
10 |
{ |
|
11 |
title: "Learn about my friend Jenkins", |
|
12 |
completed: true, |
|
13 |
important: false |
|
14 |
}, |
|
15 |
{ |
|
16 |
title: "Have a poop", |
|
17 |
completed: false, |
|
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); |
e38844
|
29 |
}).catch(err => { |
D |
30 |
if (err) { |
|
31 |
console.info("INFO - setting dummy data because of ", err) |
|
32 |
commit("SET_TODOS", dummyData); |
|
33 |
commit("SET_LOADING", false); |
|
34 |
} |
82de6c
|
35 |
}); |
D |
36 |
}, |
|
37 |
addTodo({ commit, state }) { |
|
38 |
if (!state.newTodo) { |
|
39 |
// do not add empty todos |
|
40 |
return; |
|
41 |
} |
e38844
|
42 |
// debugger |
82de6c
|
43 |
const todo = { |
D |
44 |
title: state.newTodo, |
e38844
|
45 |
completed: false, |
D |
46 |
important: false |
82de6c
|
47 |
}; |
D |
48 |
axios.post(config.todoEndpoint, todo).then(mongoTodo => { |
3f23cc
|
49 |
commit("ADD_TODO", mongoTodo.data); |
82de6c
|
50 |
}); |
D |
51 |
}, |
e38844
|
52 |
setNewTodo({ commit }, todo) { |
8f43d3
|
53 |
// debugger |
e38844
|
54 |
commit("SET_NEW_TODO", todo); |
530b0a
|
55 |
}, |
e38844
|
56 |
updateTodo({ commit, state }, todo) { |
1f415e
|
57 |
// const todo = state.newTodo |
e38844
|
58 |
// debugger; |
1f415e
|
59 |
const foundIndex = state.todos.findIndex(obj => obj.id === todo.id); |
A |
60 |
state.todos[foundIndex] = todo; |
e38844
|
61 |
const newUpdatedArray = state.todos; |
D |
62 |
commit("UPDATE_TODO", newUpdatedArray); |
1f415e
|
63 |
}, |
82de6c
|
64 |
clearNewTodo({ commit }) { |
D |
65 |
commit("CLEAR_NEW_TODO"); |
fbee7b
|
66 |
}, |
A |
67 |
clearAllTodos({ commit }) { |
e38844
|
68 |
commit("CLEAR_ALL_TODOS"); |
08cb74
|
69 |
}, |
A |
70 |
clearAllDoneTodos({ commit }) { |
e38844
|
71 |
commit("CLEAR_ALL_DONE_TODOS"); |
D |
72 |
} |
82de6c
|
73 |
}; |