parsley/app/javascript/components/TheTaskListList.vue

114 lines
2.9 KiB
Vue
Raw Normal View History

2018-08-28 10:39:11 -05:00
<template>
<div>
<h1>Tasks</h1>
2018-08-28 16:52:56 -05:00
<app-dropdown button-class="is-primary" :open="showListDropdown" :label="listSelectLabel" @open="showListDropdown = true" @close="showListDropdown = false">
2018-09-05 11:00:35 -05:00
2018-09-05 17:49:21 -05:00
<task-list-dropdown-item v-for="l in taskLists" :task-list="l" :active="currentList !== null && currentList.id === l.id" @select="selectList" @delete="deleteList"></task-list-dropdown-item>
2018-09-05 11:00:35 -05:00
2018-08-28 16:52:56 -05:00
<hr class="dropdown-divider" v-if="taskLists.length > 0">
<div class="dropdown-item">
<task-list-mini-form :task-list="newList" :validation-errors="newListValidationErrors" @save="saveNewList"></task-list-mini-form>
</div>
</app-dropdown>
2018-08-29 16:58:07 -05:00
<div v-if="currentList !== null">
2018-09-05 17:49:21 -05:00
<task-item-list :task-items="currentList.task_items"></task-item-list>
2018-08-29 16:58:07 -05:00
</div>
2018-08-28 10:39:11 -05:00
</div>
</template>
<script>
2018-08-28 16:52:56 -05:00
import api from "../lib/Api";
import * as Errors from '../lib/Errors';
import TaskListMiniForm from "./TaskListMiniForm";
2018-09-05 17:49:21 -05:00
import TaskListDropdownItem from "./TaskListDropdownItem";
import TaskItemList from "./TaskItemList";
2018-08-28 16:52:56 -05:00
const newListTemplate = function() {
return {
name: ''
};
};
2018-08-28 10:39:11 -05:00
export default {
data() {
2018-08-28 16:52:56 -05:00
return {
taskLists: [],
showListDropdown: false,
currentList: null,
newList: newListTemplate(),
2018-09-05 17:49:21 -05:00
newListValidationErrors: {}
2018-08-28 16:52:56 -05:00
}
},
computed: {
listSelectLabel() {
if (this.currentList === null) {
return "Select or Create a List";
} else {
return this.currentList.name;
2018-08-28 10:39:11 -05:00
}
2018-08-28 16:52:56 -05:00
}
},
methods: {
2018-09-05 17:49:21 -05:00
selectList(list) {
this.currentList = list;
2018-08-29 16:58:07 -05:00
this.showListDropdown = false;
2018-08-28 16:52:56 -05:00
},
saveNewList() {
this.loadResource(
api.postTaskList(this.newList)
2018-09-05 11:00:35 -05:00
.then(l => this.currentList = l)
.then(() => this.showListDropdown = false)
2018-08-28 16:52:56 -05:00
.then(() => this.updateData())
.then(() => { this.newList = newListTemplate(); this.newListValidationErrors = {}; } )
.catch(Errors.onlyFor(Errors.ApiValidationError, err => this.newListValidationErrors = err.validationErrors()))
);
},
updateData() {
2018-08-29 16:58:07 -05:00
return this.loadResource(api.getTaskLists(data => this.setNewData(data)));
},
setNewData(list) {
this.taskLists = list;
if (this.currentList !== null) {
this.currentList = this.taskLists.find(l => this.currentList.id === l.id) || null;
}
if (this.currentList === null && this.taskLists.length > 0) {
this.currentList = this.taskLists[0];
}
2018-09-05 11:00:35 -05:00
},
2018-09-05 17:49:21 -05:00
deleteList(list) {
2018-09-05 11:00:35 -05:00
this.loadResource(
2018-09-05 17:49:21 -05:00
api.deleteTaskList(list)
2018-09-05 11:00:35 -05:00
.then(() => this.updateData())
);
2018-08-28 16:52:56 -05:00
}
},
created() {
this.updateData()
},
components: {
2018-09-05 17:49:21 -05:00
TaskListMiniForm,
TaskListDropdownItem,
TaskItemList
2018-08-28 10:39:11 -05:00
}
}
</script>
<style lang="scss" scoped>
</style>