-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.html
More file actions
149 lines (147 loc) · 5.69 KB
/
app.html
File metadata and controls
149 lines (147 loc) · 5.69 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<!DOCTYPE html>
<html>
<head>
<link type="text/css" rel="stylesheet" href="https://bootswatch.com/5/quartz/bootstrap.min.css">
</head>
<body>
<div id="app">
<nav class="navbar navbar-expand-lg navbar-dark bg-primary">
<div class="container-fluid">
<a class="navbar-brand" href="#">Todo List</a>
</div>
<div class="d-flex" style="padding-right: 20px;" v-if="state === 'tasks'">
<b>{{ loginName }}</b>
</div>
<div class="d-flex" style="padding-right: 20px;" v-if="state === 'tasks'">
<button @click="logout()" class="btn btn-warning btn-sm">Logout</button>
</div>
</nav>
<div class="container mt-5">
<div v-if="state === 'loading'">
<h2>Loading...</h2>
</div>
<div v-if="state === 'login'">
<h2>Login</h2>
<form @submit.prevent="login()">
<label>Username</label><br>
<input v-model="username" class="form-control"><br><br>
<label>Password</label><br>
<input v-model="password" class="form-control" type="password"><br><br>
<button type="submit" class="btn btn-primary">Login</button>
</form><br>
<a href="" @click.prevent="state = 'register'">No Account yet? Register now!</a>
</div>
<div v-if="state === 'register'">
<h2>Register</h2>
<form @submit.prevent="register()">
<label>Username</label><br>
<input v-model="username" class="form-control"><br><br>
<label>Password</label><br>
<input v-model="password" class="form-control" type="password"><br><br>
<button type="submit" class="btn btn-primary">Register</button>
</form><br>
<a href="" @click.prevent="state = 'login'">Back to Login</a>
</div>
<div v-if="state === 'tasks'">
<h2 class="mt-3">Tasks</h2>
<ul class="list-group">
<li v-for="task in tasks" :key="task.id" class="list-group-item d-flex justify-content-between align-items-center">
<input type="checkbox" :checked="task.done" @input="e => updateTask(task.id, e.target.checked)" class="form-check-input"> {{ task.text }} <button @click="deleteTask(task.id)" class="btn btn-danger btn-sm">Delete</button>
</li>
</ul>
<h2 class="mt-3">New Task</h2>
<form @submit.prevent="createTask()">
<label>Text</label><br>
<input v-model="newTaskText" class="form-control"><br>
<button type="submit" class="btn btn-primary">Add</button>
</form>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue@2"></script>
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
<script>
const API_URL = '/api'
const app = new Vue({
el: '#app',
data: () => ({
state: 'loading',
tasks: [
{
id: 1,
text: 'First Task',
done: false
},
{
id: 2,
text: 'Second Task',
done: true
}
],
username: '',
password: '',
newTaskText: ''
}),
created() {
this.loadTasks()
},
computed: {
loginName() {
return JSON.parse(atob(localStorage.getItem('token').split('.')[1])).sub
}
},
methods: {
loadTasks() {
axios.get(API_URL + '/tasks', { headers: { Authorization: 'Bearer ' + localStorage.getItem('token') } }).then(res => {
this.tasks = res.data
this.state = 'tasks'
}, err => {
if(err.response && err.response.status === 401) {
localStorage.removeItem('token')
this.state = 'login'
return
}
alert(err.message)
})
},
login() {
this.state = 'loading'
axios.post(API_URL + '/login', { username: this.username, password: this.password }, { responseType: 'text' }).then(res => {
localStorage.setItem('token', res.data.token)
this.state = 'loading'
this.password = ''
this.loadTasks()
}, () => this.state = 'login')
},
register() {
axios.post(API_URL + '/register', { username: this.username, password: this.password }).then(res => {
this.password = ''
this.state = 'login'
}, () => this.state = 'register')
},
logout() {
localStorage.removeItem('token')
this.state = 'login'
},
createTask() {
axios.post(API_URL + '/tasks', { text: this.newTaskText }, { headers: { Authorization: 'Bearer ' + localStorage.getItem('token') } }).then(res => {
this.tasks.push((res.data))
this.newTaskText = ''
})
},
updateTask(id, done) {
axios.put(API_URL + '/tasks/' + id, { done }, { headers: { Authorization: 'Bearer ' + localStorage.getItem('token') } }).then(res => {
this.tasks.filter(t => t.id === id).forEach(t => t.done = res.data.done)
this.tasks = [...this.tasks] // Dirty way to force vue into updating
})
},
deleteTask(id) {
axios.delete(API_URL + '/tasks/' + id, { headers: { Authorization: 'Bearer ' + localStorage.getItem('token') } }).then(res => {
this.tasks = this.tasks.filter(t => t.id !== id)
})
}
}
})
</script>
</body>
</html>