parsley/app/javascript/components/TheTaskListList.vue

149 lines
4.0 KiB
Vue
Raw Normal View History

2018-08-28 10:39:11 -05:00
<template>
<div>
<h1>Tasks</h1>
2018-09-05 11:00:35 -05:00
<app-modal :open="listToDelete !== null" :title="'Delete ' + (listToDelete !== null ? listToDelete.name : '') + '?'" @dismiss="listToDelete = null">
<button class="button is-danger" @click="confirmListDelete">Confirm</button>
<button class="button is-primary" @click="listToDelete = null">Cancel</button>
</app-modal>
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
<div class="dropdown-item" v-for="l in taskLists">
<a href="#" @click="selectList(l)">{{l.name}}<button @click.stop="listToDelete = l" class="button is-small is-danger is-pulled-right"><app-icon icon="x" size="sm"></app-icon></button></a>
</div>
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">
<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(),
2018-09-05 11:00:35 -05:00
newItemValidationErrors: {},
listToDelete: null
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)
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
},
confirmListDelete() {
this.loadResource(
api.deleteTaskList(this.listToDelete)
.then(() => this.updateData())
.then(() => this.listToDelete = null)
);
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>