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" >
2018-09-12 08:42:07 +02:00
< router-link : to = "{ name: 'editList', params: { id: list.id } }" class = "icon settings is-medium" >
< icon icon = "cog" size = "2x" / >
< / router-link >
2018-09-08 23:33:23 +02:00
< h1 > { { list . title } } < / h1 >
2018-09-10 21:06:39 +02:00
< / div >
< 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 >
2018-09-09 22:09:20 +02:00
2018-09-10 21:06:39 +02:00
< div class = "columns" >
< div class = "column" >
< div class = "box tasks" v-if = "this.list.tasks && this.list.tasks.length > 0" >
< div class = "task" v-for = "l in list.tasks" v-bind:key="l.id" >
< label v -bind :for = "l.id" >
< input @change ="markAsDone" type = "checkbox" v -bind :id = "l.id" v -bind :checked = "l.done" >
{ { l . text } }
< / label >
< div @click ="editTask(l.id)" class = "icon settings" >
< icon icon = "cog" / >
< / div >
< / div >
2018-09-09 22:09:20 +02:00
< / div >
2018-09-10 21:06:39 +02:00
< / div >
< div class = "column is-4" v-if = "isTaskEdit" >
< div class = "card taskedit" >
< header class = "card-header" >
< p class = "card-header-title" >
Edit Task
< / p >
< a class = "card-header-icon" @ click = "isTaskEdit = false" >
< span class = "icon" >
< icon icon = "angle-right" / >
< / span >
< / a >
< / header >
< div class = "card-content" >
< div class = "content" >
< form @submit.prevent ="editTaskSubmit()" >
< div class = "field" >
< label class = "label" for = "tasktext" > Task Text < / label >
< div class = "control" >
2018-09-11 07:13:55 +02:00
< input : class = "{ 'disabled': loading}" :disabled = "loading" class = "input" type = "text" id = "tasktext" placeholder = "The task text is here..." v-model = "taskEditTask.text" >
2018-09-10 21:06:39 +02:00
< / div >
< / div >
< div class = "field" >
< label class = "label" for = "taskdescription" > Description < / label >
< div class = "control" >
2018-09-11 07:13:55 +02:00
< textarea : class = "{ 'disabled': loading}" :disabled = "loading" class = "textarea" placeholder = "The tasks description goes here..." id = "taskdescription" v-model = "taskEditTask.description" > < / textarea >
2018-09-10 21:06:39 +02:00
< / div >
< / div >
< div class = "columns" >
< div class = "column" >
< div class = "field" >
< label class = "label" for = "taskduedate" > Due Date < / label >
< div class = "control" >
2018-09-11 08:28:11 +02:00
< flat-pickr
: class = "{ 'disabled': loading}"
class = "input"
: disabled = "loading"
v - model = "taskEditTask.dueDate"
: config = "flatPickerConfig"
id = "taskduedate"
placeholder = "The tasks due date is here..." >
< / flat-pickr >
2018-09-10 21:06:39 +02:00
< / div >
< / div >
< / div >
< div class = "column" >
< div class = "field" >
< label class = "label" for = "taskreminderdate" > Reminder Date < / label >
< div class = "control" >
2018-09-11 08:28:11 +02:00
< flat-pickr
: class = "{ 'disabled': loading}"
class = "input"
: disabled = "loading"
v - model = "taskEditTask.reminderDate"
: config = "flatPickerConfig"
id = "taskreminderdate"
placeholder = "The tasks reminder date is here..." >
< / flat-pickr >
2018-09-10 21:06:39 +02:00
< / div >
< / div >
< / div >
< / div >
2018-09-09 22:09:20 +02:00
2018-09-11 07:13:55 +02:00
< button type = "submit" class = "button is-success is-fullwidth" : class = "{ 'is-loading': loading}" >
2018-09-10 21:06:39 +02:00
Save
< / button >
< / form >
< / div >
< / div >
< / div >
2018-09-09 22:09:20 +02:00
< / 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'
2018-09-11 08:28:11 +02:00
import flatPickr from 'vue-flatpickr-component' ;
import 'flatpickr/dist/flatpickr.css' ;
2018-09-08 23:33:23 +02:00
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 : '' ,
2018-09-10 21:06:39 +02:00
loading : false ,
isTaskEdit : false ,
taskEditTask : { } ,
2018-09-11 08:28:11 +02:00
flatPickerConfig : {
altFormat : 'j M Y H:i' ,
altInput : true ,
dateFormat : 'Y-m-d H:i' ,
enableTime : true ,
defaultDate : new Date ( ) ,
} ,
2018-09-08 23:33:23 +02:00
}
} ,
2018-09-11 08:28:11 +02:00
components : {
flatPickr
} ,
2018-09-08 23:33:23 +02:00
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 ( ) {
2018-09-10 21:06:39 +02:00
this . isTaskEdit = false
2018-09-08 23:33:23 +02:00
this . loading = true
HTTP . get ( ` lists/ ` + this . $route . params . id , { headers : { 'Authorization' : 'Bearer ' + localStorage . getItem ( 'token' ) } } )
. then ( response => {
this . loading = false
2018-09-11 08:28:11 +02:00
// Make date objects from timestamps
for ( const t in response . data . tasks ) {
let dueDate = new Date ( response . data . tasks [ t ] . dueDate * 1000 )
let reminderDate = new Date ( response . data . tasks [ t ] . reminderDate * 1000 )
response . data . tasks [ t ] . dueDate = dueDate
response . data . tasks [ t ] . reminderDate = reminderDate
}
2018-09-08 23:33:23 +02:00
// 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-11 07:10:32 +02:00
this . updateTaskByID ( parseInt ( e . target . id ) , response . data )
2018-09-10 19:44:14 +02:00
this . handleSuccess ( { message : 'The task was successfully ' + ( e . target . checked ? 'un-' : '' ) + 'marked as done.' } )
2018-09-10 07:37:10 +02:00
} )
. catch ( e => {
this . handleError ( e )
} )
} ,
2018-09-10 21:06:39 +02:00
editTask ( id ) {
// Find the slected task and set it to the current object
for ( const t in this . list . tasks ) {
if ( this . list . tasks [ t ] . id === id ) {
this . taskEditTask = this . list . tasks [ t ]
break
}
}
this . isTaskEdit = true
} ,
editTaskSubmit ( ) {
this . loading = true
// Convert the date in a unix timestamp
let duedate = ( + new Date ( this . taskEditTask . dueDate ) ) / 1000
let reminderdate = ( + new Date ( this . taskEditTask . reminderDate ) ) / 1000
this . taskEditTask . dueDate = duedate
this . taskEditTask . reminderDate = reminderdate
HTTP . post ( ` tasks/ ` + this . taskEditTask . id , this . taskEditTask , { headers : { 'Authorization' : 'Bearer ' + localStorage . getItem ( 'token' ) } } )
. then ( response => {
2018-09-11 08:28:11 +02:00
response . data . dueDate = new Date ( response . data . dueDate * 1000 )
response . data . reminderDate = new Date ( response . data . reminderDate * 1000 )
// Update the task in the list
2018-09-10 21:06:39 +02:00
this . updateTaskByID ( this . taskEditTask . id , response . data )
2018-09-11 08:28:11 +02:00
// Also update the current taskedit object so the ui changes
this . $set ( this , 'taskEditTask' , response . data )
2018-09-10 21:06:39 +02:00
this . handleSuccess ( { message : 'The task was successfully updated.' } )
} )
. catch ( e => {
this . handleError ( e )
} )
} ,
updateTaskByID ( id , updatedTask ) {
for ( const t in this . list . tasks ) {
if ( this . list . tasks [ t ] . id === id ) {
this . $set ( this . list . tasks , t , updatedTask )
break
}
}
} ,
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
}
}
}
2018-11-06 15:54:25 +01:00
< / script >