-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
93 lines (93 loc) · 2.39 KB
/
script.js
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
Vue.createApp({
data() {
return {
todos: [
// { description: "first todo", id: 1, done: false },
// { description: "learn anything", id: 2, done: true },
// { description: "go shopping", id: 3, done: false },
],
filter: "all",
todoInput: "",
};
},
methods: {
removeDoneTodos(e) {
e.preventDefault();
this.todos.forEach((todo) => {
if (todo.done) {
fetch(`http://localhost:4730/todos/${todo.id}`, {
method: "DELETE",
})
.then(() => {
this.getAllTodos();
})
.catch((error) => window.alert(error));
}
});
},
addNewTodo() {
let todoValue = this.todoInput;
if (!todoValue.trim()) {
window.alert("add todo pls!");
return;
}
if (
this.todos.findIndex(
(todo) =>
todo.description.toLowerCase().trim() ===
todoValue.toLowerCase().trim()
) !== -1
) {
window.alert("todo is already in list!");
} else {
fetch("http://localhost:4730/todos", {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({ description: todoValue, done: false }),
})
.then((response) => {
if (response.ok) {
this.todoInput.value = "";
}
})
.catch((error) => window.alert(error));
}
this.todoInput = "";
this.getAllTodos();
},
getAllTodos() {
fetch("http://localhost:4730/todos")
.then((res) => res.json())
.then((todos) => {
this.todos = todos;
});
},
changeDoneState(todo) {
fetch("http://localhost:4730/todos/" + todo.id, {
method: "PATCH",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ done: todo.done }),
}).then(this.getAllTodos());
},
},
computed: {
filteredTodos() {
if (this.filter === "all") {
return this.todos;
}
if (this.filter === "open") {
return this.todos.filter((todo) => {
return todo.done === false;
});
}
if (this.filter === "done") {
return this.todos.filter((todo) => {
return todo.done === true;
});
}
},
},
created() {
this.getAllTodos();
},
}).mount("#app");