-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
72 lines (70 loc) · 1.98 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script src="https://cdn.jsdelivr.net/npm/vue"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<link href="main.css" rel="stylesheet">
</head>
<body>
<div id="app">
<div id="layout-add">
<span v-show="apiResponse">{{apiResponse}}</span>
<add-new-form @new-todo-item="newTodoItem"></add-new-form>
</div>
<div id="layout-list">
<todo-item v-for="todo in orderByCompleted" :key="todo.id"
:todo="todo"
@update-todo="updateTodo"
@delete-todo="deleteTodo">
</todo-item>
</div>
</div>
<script src="components.js"></script>
<script>
const app = new Vue({
el: '#app',
data: {
apiResponse: '',
todos: []
},
mounted(){
this.fetchAll();
},
computed:{
orderByCompleted(){
return this.todos.sort( (a,b) => Math.max(a.completed, a.created) - Math.max(b.completed, b.created));
}
},
methods:{
newTodoItem(txt){
axios.post('/api/todo', {text:txt}).then( (response) => {
this.fetchAll();
});
},
updateTodo(todo){
axios.patch(`/api/todo/${todo.id}`, todo).then( (response) => {
this.fetchAll();
});
},
deleteTodo(id){
axios.delete(`/api/todo/${id}`).then( (response) => {
this.fetchAll();
});
},
fetchAll(){
axios.get('/api/todo').then( ({data}) => {
this.todos = data.data;
}).catch( (res) =>{
this.apiResponse = 'We had a problem talking to the api.';
});
}
}
});
</script>
</body>
</html>