-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
129 lines (125 loc) · 3.55 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
BUG: Test 2x, nach Alert F5 kommt doch rein
*/
"use strict";
Vue.createApp({
data() {
return {
todos: [],
newTodoText: "",
apiUrl: "http://localhost:4730/todos/",
filter: "all", // Initial filter "all", "open", "done"
};
},
computed: {
filteredTodos() {
// selected filter
if (this.filter === "open") {
return this.todos.filter((todo) => !todo.done);
} else if (this.filter === "done") {
return this.todos.filter((todo) => todo.done);
} else {
// "all" or another filter
return this.todos;
}
},
},
methods: {
addTodo() {
const newTodoObject = {
description: this.newTodoText.trim(),
done: false,
};
const isDuplicate = this.todos.some(
(task) =>
task.description.toLowerCase() ===
this.newTodoText.trim().toLowerCase()
);
if (this.newTodoText === "") {
alert("Write something down!");
this.newTodoText = "";
} else if (isDuplicate) {
alert("You can't add a duplicate task!");
this.newTodoText = "";
} else {
fetch(this.apiUrl, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(newTodoObject),
})
.then((response) => response.json())
.then((jsonData) => {
this.todos.push(jsonData);
this.newTodoText = "";
});
}
},
updateTodo(todo) {
const cloneCurrentTodo = {
id: todo.id,
description: todo.description,
done: !todo.done,
};
fetch(this.apiUrl + cloneCurrentTodo.id, {
method: "PUT",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(cloneCurrentTodo),
})
.then((response) => response.json())
.then((jsonDataUpdatedTodo) => {
todo.done = !todo.done;
})
.catch((error) => {
console.error(
"Fehler beim Aktualisieren des Todos in der API:",
error
);
});
},
deleteTodo(todoId) {
fetch(this.apiUrl + todoId, {
method: "DELETE",
})
.then(() => {
this.todos = this.todos.filter((todo) => todo.id !== todoId);
})
.catch((error) => {
console.error("Fehler beim Löschen des Todos:", error);
});
},
deleteDoneTodos() {
if (confirm("Do you really want to delete all completed tasks?")) {
// Filter out completed todos from the current state
const doneTodos = this.todos.filter((todo) => todo.done);
if (doneTodos.length === 0) {
// No done tasks
console.log("No completed tasks to delete");
return;
}
// Use Promise.all for multiple API requests and delete many completed todos
Promise.all(
doneTodos.map((doneTodo) =>
fetch(this.apiUrl + `/${doneTodo.id}`, {
method: "DELETE",
headers: { "Content-Type": "application/json" },
})
)
)
.then(() => {
// Update the local state by filtering out completed todos
this.todos = this.todos.filter((todo) => !todo.done);
})
.catch((error) =>
console.error("Error removing done todos from API:", error)
);
}
},
},
created() {
fetch(this.apiUrl)
.then((response) => response.json())
.then((jsonData) => (this.todos = jsonData));
},
}).mount("#app");