2018-09-08 23:33:23 +02:00
|
|
|
<template>
|
|
|
|
<div>
|
|
|
|
<div class="full-loader-wrapper" v-if="loading">
|
|
|
|
<div class="half-circle-spinner">
|
|
|
|
<div class="circle circle-1"></div>
|
|
|
|
<div class="circle circle-2"></div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div class="content">
|
|
|
|
<h1>{{ list.title }}</h1>
|
2018-09-09 22:09:20 +02:00
|
|
|
|
|
|
|
<form @submit.prevent="addTask()">
|
|
|
|
<div class="field is-grouped">
|
|
|
|
<p class="control has-icons-left is-expanded" v-bind:class="{ 'is-loading': loading}">
|
|
|
|
<input class="input" v-bind:class="{ 'disabled': loading}" v-model="newTask" type="text" placeholder="Add a new task...">
|
|
|
|
<span class="icon is-small is-left">
|
|
|
|
<icon icon="tasks"/>
|
|
|
|
</span>
|
|
|
|
</p>
|
|
|
|
<p class="control">
|
|
|
|
<button type="submit" class="button is-success">
|
|
|
|
<span class="icon is-small">
|
|
|
|
<icon icon="plus"/>
|
|
|
|
</span>
|
|
|
|
Add
|
|
|
|
</button>
|
|
|
|
</p>
|
|
|
|
</div>
|
|
|
|
</form>
|
|
|
|
|
|
|
|
<div class="box tasks">
|
|
|
|
<label class="task" v-for="l in list.tasks" v-bind:key="l.id" v-bind:for="l.id">
|
2018-09-10 08:19:57 +02:00
|
|
|
<input @change="markAsDone" type="checkbox" v-bind:id="l.id" v-bind:checked="l.done">
|
2018-09-09 22:09:20 +02:00
|
|
|
{{l.text}}
|
|
|
|
</label>
|
|
|
|
</div>
|
2018-09-08 23:33:23 +02:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
|
|
|
import auth from '../../auth'
|
|
|
|
import router from '../../router'
|
|
|
|
import {HTTP} from '../../http-common'
|
|
|
|
import message from '../../message'
|
|
|
|
|
|
|
|
export default {
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
listID: this.$route.params.id,
|
2018-09-09 22:09:20 +02:00
|
|
|
list: {},
|
|
|
|
newTask: '',
|
2018-09-08 23:33:23 +02:00
|
|
|
error: '',
|
|
|
|
loading: false
|
|
|
|
}
|
|
|
|
},
|
|
|
|
beforeMount() {
|
|
|
|
// Check if the user is already logged in, if so, redirect him to the homepage
|
|
|
|
if (!auth.user.authenticated) {
|
|
|
|
router.push({name: 'home'})
|
|
|
|
}
|
|
|
|
},
|
2018-09-09 22:09:20 +02:00
|
|
|
created() {
|
2018-09-08 23:33:23 +02:00
|
|
|
this.loadList()
|
|
|
|
},
|
|
|
|
watch: {
|
|
|
|
// call again the method if the route changes
|
|
|
|
'$route': 'loadList'
|
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
loadList() {
|
|
|
|
this.loading = true
|
|
|
|
|
|
|
|
HTTP.get(`lists/` + this.$route.params.id, {headers: {'Authorization': 'Bearer ' + localStorage.getItem('token')}})
|
|
|
|
.then(response => {
|
|
|
|
this.loading = false
|
|
|
|
// This adds a new elemednt "list" to our object which contains all lists
|
|
|
|
this.$set(this, 'list', response.data)
|
2018-09-09 22:09:20 +02:00
|
|
|
if (this.list.tasks === null) {
|
|
|
|
this.list.tasks = []
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(e => {
|
|
|
|
this.handleError(e)
|
|
|
|
})
|
|
|
|
},
|
|
|
|
addTask() {
|
|
|
|
this.loading = true
|
|
|
|
|
|
|
|
HTTP.put(`lists/` + this.$route.params.id, {text: this.newTask}, {headers: {'Authorization': 'Bearer ' + localStorage.getItem('token')}})
|
|
|
|
.then(response => {
|
|
|
|
this.list.tasks.push(response.data)
|
|
|
|
this.handleSuccess({message: 'The task was successfully created.'})
|
2018-09-08 23:33:23 +02:00
|
|
|
})
|
|
|
|
.catch(e => {
|
|
|
|
this.handleError(e)
|
|
|
|
})
|
2018-09-09 22:09:20 +02:00
|
|
|
|
|
|
|
this.newTask = ''
|
2018-09-08 23:33:23 +02:00
|
|
|
},
|
2018-09-10 07:37:10 +02:00
|
|
|
markAsDone(e) {
|
|
|
|
|
|
|
|
this.loading = true
|
|
|
|
|
2018-09-10 08:19:57 +02:00
|
|
|
HTTP.post(`tasks/` + e.target.id, {done: e.target.checked}, {headers: {'Authorization': 'Bearer ' + localStorage.getItem('token')}})
|
2018-09-10 07:37:10 +02:00
|
|
|
.then(response => {
|
2018-09-10 08:19:57 +02:00
|
|
|
for (const t in this.list.tasks) {
|
|
|
|
if (this.list.tasks[t].id === response.data.id) {
|
|
|
|
this.$set(this.list.tasks, t, response.data)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2018-09-10 07:37:10 +02:00
|
|
|
this.handleSuccess({message: 'The task was successfully marked as done.'})
|
|
|
|
})
|
|
|
|
.catch(e => {
|
|
|
|
this.handleError(e)
|
|
|
|
})
|
|
|
|
},
|
2018-09-08 23:33:23 +02:00
|
|
|
handleError(e) {
|
|
|
|
this.loading = false
|
|
|
|
message.error(e, this)
|
2018-09-09 22:09:20 +02:00
|
|
|
},
|
|
|
|
handleSuccess(e) {
|
|
|
|
this.loading = false
|
|
|
|
message.success(e, this)
|
2018-09-08 23:33:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
</script>
|
|
|
|
|
2018-09-09 22:09:20 +02:00
|
|
|
<style scoped lang="scss">
|
|
|
|
.tasks {
|
|
|
|
margin-top: 1rem;
|
|
|
|
padding: 0;
|
|
|
|
|
|
|
|
.task {
|
|
|
|
display: block;
|
|
|
|
padding: 0.5rem 1rem;
|
|
|
|
border-bottom: 1px solid darken(#fff, 10%);
|
|
|
|
cursor: pointer;
|
|
|
|
|
|
|
|
input[type="checkbox"] {
|
|
|
|
vertical-align: middle;
|
|
|
|
}
|
|
|
|
}
|
2018-09-08 23:33:23 +02:00
|
|
|
|
2018-09-09 22:09:20 +02:00
|
|
|
.task:last-child {
|
|
|
|
border-bottom: none;
|
|
|
|
}
|
|
|
|
}
|
2018-09-08 23:33:23 +02:00
|
|
|
</style>
|