-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
111 lines (111 loc) · 4.02 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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>ToDo App</title>
<style>
.completed {
text-decoration: line-through;
}
</style>
</head>
<body>
<div id="app">
<h1>ToDo App</h1>
<input type="text" v-model="newTaskName" placeholder="Nova Tarefa">
<button @click="addTask">Adicionar</button>
<ul>
<li v-for="task in tasks" :key="task._id" :class="{ 'completed': task.completed }">
<span @click="markTaskAsCompleted(task._id)" v-if="!task.completed">{{ task.name }}</span>
<del v-else>{{ task.name }}</del>
<button @click="deleteTask(task._id)">Excluir</button>
<button @click="markTaskAsCompleted(task._id)" v-if="!task.completed">Concluir</button>
</li>
</ul>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.min.js"></script>
<script>
new Vue({
el: '#app',
data: {
tasks: [],
newTaskName: ''
},
methods: {
addTask() {
if (!this.newTaskName) {
console.error('Nome da tarefa não especificado');
return;
}
fetch('http://localhost:8080/tasks', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
name: this.newTaskName
})
})
.then(response => {
if (!response.ok) {
throw new Error('Erro ao adicionar a tarefa');
}
this.fetchTasks();
this.newTaskName = '';
})
.catch(error => {
console.error('Erro ao adicionar a tarefa:', error);
});
},
deleteTask(taskId) {
fetch(`http://localhost:8080/tasks/${taskId}`, {
method: 'DELETE',
headers: {
'Content-Type': 'application/json'
}
})
.then(response => {
if (!response.ok) {
throw new Error('Erro ao deletar a tarefa');
}
this.fetchTasks();
})
.catch(error => {
console.error('Erro ao deletar a tarefa:', error);
});
},
markTaskAsCompleted(taskId) {
fetch(`http://localhost:8080/tasks/${taskId}`, {
method: 'PATCH',
headers: {
'Content-Type': 'application/json'
}
})
.then(response => {
if (!response.ok) {
throw new Error('Erro ao marcar a tarefa como concluída');
}
this.fetchTasks();
})
.catch(error => {
console.error('Erro ao marcar a tarefa como concluída:', error);
});
},
fetchTasks() {
fetch('http://localhost:8080/tasks')
.then(response => response.json())
.then(data => {
this.tasks = data;
})
.catch(error => {
console.error('Erro ao buscar as tarefas:', error);
});
}
},
mounted() {
this.fetchTasks();
}
});
</script>
</body>
</html>