-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtodoStore.js
More file actions
79 lines (67 loc) · 2.41 KB
/
todoStore.js
File metadata and controls
79 lines (67 loc) · 2.41 KB
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
import { toJS, observable, action, computed } from 'mobx'
class TodoStore {
@observable todos = null;
@action async addTodo(title) {
//this.todos.push({title,check:false});
try {
let response = await fetch('http://10.0.2.2:3000/add', {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({ title: title, }),
});
let responseJson = await response.json();
this.todos.push({ responseJson });
} catch (error) {
console.error(error);
}
}
@computed get allTodos() {
return toJS(this.todos);
//return this.todos.slice(0);
}
@action async updateCheck(index, check) {
try {
let response = await fetch(`http://10.0.2.2:3000/updateCheck/${index}`, {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({ check: check, }),
});
let responseJson = await response.json();
this.todos[index] = responseJson;
} catch (error) {
console.error(error);
}
}
@action async removeData(index) {
try {
let response = await fetch(`http://10.0.2.2:3000/remove`, {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({ id: index}),
});
//let responseJson = await response.json();
this.todos = this.todos.filter((v, i) => i !== index);
} catch (error) {
console.error(error);
}
}
@action async fetchAll() {
try {
let response = await fetch('http://10.0.2.2:3000/list');
let responseJson = await response.json();
this.todos = responseJson;
} catch (error) {
console.log(error);
}
}
}
export default new TodoStore();