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">
|
|
|
|
<a class="dropdown-item" href="#" v-for="l in taskLists" @click="selectList(l)">{{l.name}}</a>
|
|
|
|
<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">
|
|
|
|
<table class="table">
|
|
|
|
<tr>
|
|
|
|
<th></th>
|
|
|
|
<th>Name</th>
|
|
|
|
<th>Quantity</th>
|
|
|
|
<th></th>
|
|
|
|
</tr>
|
|
|
|
<tr v-if="showAddItem">
|
|
|
|
<th></th>
|
|
|
|
<th><input type="text"></th>
|
|
|
|
<th><input type="text"></th>
|
|
|
|
<th><button type="button" class="button">Add</button></th>
|
|
|
|
</tr>
|
|
|
|
<tr v-for="i in currentList.task_items">
|
|
|
|
<td></td>
|
|
|
|
<td>{{i.name}}</td>
|
|
|
|
<td>{{i.quantity}}</td>
|
|
|
|
</tr>
|
|
|
|
</table>
|
|
|
|
|
|
|
|
<div v-if="currentList.task_items.length === 0">
|
|
|
|
No Items
|
|
|
|
</div>
|
|
|
|
</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";
|
|
|
|
|
|
|
|
const newListTemplate = function() {
|
|
|
|
return {
|
|
|
|
name: ''
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2018-08-29 16:58:07 -05:00
|
|
|
const newItemTemplate = function() {
|
|
|
|
return {
|
|
|
|
name: '',
|
|
|
|
quantity: ''
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
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-08-29 16:58:07 -05:00
|
|
|
newListValidationErrors: {},
|
|
|
|
showAddItem: false,
|
|
|
|
newItem: newItemTemplate(),
|
|
|
|
newItemValidationErrors: {}
|
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: {
|
|
|
|
selectList(l) {
|
|
|
|
this.currentList = l;
|
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)
|
|
|
|
.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-08-28 16:52:56 -05:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
created() {
|
|
|
|
this.updateData()
|
|
|
|
},
|
|
|
|
|
|
|
|
components: {
|
|
|
|
TaskListMiniForm
|
2018-08-28 10:39:11 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<style lang="scss" scoped>
|
|
|
|
|
|
|
|
</style>
|