parsley/app/javascript/components/TaskItemList.vue

145 lines
2.9 KiB
Vue
Raw Normal View History

2018-09-05 17:49:21 -05:00
<template>
<div>
<app-expand-transition name="fade">
2018-09-06 18:16:13 -05:00
<task-item-edit @save="save" :task-item="newItem" v-if="showAddItem" ref="itemEdit"></task-item-edit>
2018-09-05 17:49:21 -05:00
</app-expand-transition>
2018-09-06 18:16:13 -05:00
<table class="table">
2018-09-05 17:49:21 -05:00
<thead>
<tr>
<th></th>
<th>Name</th>
<th>Quantity</th>
<th>
<button class="button" @click="toggleShowAddItem">{{ showAddItem ? 'Done' : 'New Item' }}</button>
</th>
</tr>
</thead>
<tbody>
2018-09-06 18:16:13 -05:00
<tr v-for="i in taskItems" :key="i.id" @click="toggleItem(i)">
<td>
<div class="check">
<app-icon v-if="i.completed" icon="check"></app-icon>
</div>
</td>
2018-09-05 17:49:21 -05:00
<td>{{ i.name }}</td>
<td>{{ i.quantity }}</td>
<td></td>
</tr>
<tr v-if="taskItems.length === 0">
<td colspan="4">
No Items
</td>
</tr>
</tbody>
</table>
</div>
</template>
<script>
2018-09-06 18:16:13 -05:00
import * as Errors from '../lib/Errors';
import { mapActions } from "vuex";
import cloneDeep from "lodash/cloneDeep";
2018-09-05 17:49:21 -05:00
import TaskItemEdit from "./TaskItemEdit";
2018-09-06 18:16:13 -05:00
const newItemTemplate = function(listId) {
2018-09-05 17:49:21 -05:00
return {
2018-09-06 18:16:13 -05:00
task_list_id: listId,
2018-09-05 17:49:21 -05:00
name: '',
2018-09-06 18:16:13 -05:00
quantity: '',
completed: false
2018-09-05 17:49:21 -05:00
};
};
export default {
props: {
2018-09-06 18:16:13 -05:00
taskList: {
2018-09-05 17:49:21 -05:00
required: true,
2018-09-06 18:16:13 -05:00
type: Object
2018-09-05 17:49:21 -05:00
}
},
data() {
return {
showAddItem: false,
2018-09-06 18:16:13 -05:00
newItem: null,
2018-09-05 17:49:21 -05:00
newItemValidationErrors: {}
};
},
2018-09-06 18:16:13 -05:00
computed: {
taskItems() {
const top = [];
const bottom = [];
const list = (this.taskList ? this.taskList.task_items : []);
for (let i of list) {
if (!i.completed) {
top.push(i);
} else {
bottom.push(i);
}
}
return top.concat(bottom);
}
},
2018-09-05 17:49:21 -05:00
methods: {
2018-09-06 18:16:13 -05:00
...mapActions([
'createTaskItem',
'updateTaskItem'
]),
save() {
this.loadResource(
this.createTaskItem(this.newItem)
.then(() => {
this.newItem = newItemTemplate(this.taskList.id);
this.$refs.itemEdit.focus();
})
.catch(Errors.onlyFor(Errors.ApiValidationError, err => this.newItemValidationErrors = err.validationErrors()))
)
},
toggleItem(i) {
const item = cloneDeep(i);
item.completed = !item.completed;
this.loadResource(
this.updateTaskItem(item)
)
},
2018-09-05 17:49:21 -05:00
toggleShowAddItem() {
2018-09-06 18:16:13 -05:00
this.newItem = newItemTemplate(this.taskList.id);
2018-09-05 17:49:21 -05:00
this.showAddItem = !this.showAddItem;
}
},
components: {
TaskItemEdit
}
}
</script>
<style lang="scss" scoped>
.columns {
margin-top: 0;
margin-bottom: 0;
}
.column {
padding-top: 0;
padding-bottom: 0;
margin-top: 0;
margin-bottom: 0;
}
</style>