-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
124 lines (112 loc) · 3.4 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
112
113
114
115
116
117
118
119
120
121
122
123
124
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0, shrink-to-fit=no">
<link rel="shortcut icon" href="https://alpinejs.dev/favicon.png">
<title>Alpine JS Demo</title>
<!-- Bulma -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bulma@0.9.3/css/bulma.min.css">
<!-- Font-awesome -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
<!-- Alpine -->
<script defer src="https://unpkg.com/alpinejs@latest/dist/cdn.min.js"></script>
<style>
.is-done {
text-decoration: line-through;
}
</style>
</head>
<body class="mt-3">
<div class="container" x-data="getTodos()" x-init="$watch('task', value => {if (value.length > 0) required=false})">
<div>
<form @submit.prevent="saveData" class="form-inline">
<div class="field is-grouped">
<div class="field">
<div class="control">
<input class="input" type="text" maxlength="20" class="input" :class="required ? 'is-danger' : ''"
placeholder="Content" x-model="task" autofocus>
</div>
<p class="help is-danger" x-show="required">
Content is required
</p>
<p class="help has-text-right" x-show="task.length" x-text="task.length"></p>
</div>
<p class="control">
<button class="button is-info ml-3" type="submit">
OK
</button>
</p>
</div>
</form>
<table>
<tbody>
<template x-for="todo in todos" :key="todo.id">
<tr>
<td :class="todo.done ? 'is-done' : ''" x-text="todo.task"
@click="toggleDone(todo.id, todo.done ? false : true)"></td>
<td>
<i class="fa fa-times has-text-danger ml-3" @click="deleteTask(todo.id)"></i>
</td>
</tr>
</template>
</tbody>
</table>
</div>
</div>
<script>
const getTodos = () => ({
url: 'http://localhost:3000/todos',
todos: [],
task: '',
required: false,
init() {
fetch(this.url)
.then(response => response.json())
.then(data => this.todos = data)
},
saveData() {
if (!this.task) {
this.required = true
return
}
fetch(this.url, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
task: this.task
})
})
.then(response => response.json())
.then(() => {
this.init()
this.task = ''
})
},
toggleDone(id, value) {
fetch(`http://localhost:3000/todos/${id}`, {
method: 'PATCH',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
done: value
})
})
.then(response => response.json())
.then(() => {
this.init()
})
},
deleteTask(id) {
fetch(`http://localhost:3000/todos/${id}`, {
method: 'DELETE',
})
.then(response => response.json())
.then(() => {
this.init()
})
},
})
</script>
</body>
</html>