parsley/app/javascript/components/TheFoodList.vue

163 lines
4.0 KiB
Vue
Raw Normal View History

2018-03-30 14:31:09 -05:00
<template>
2018-04-02 00:10:06 -05:00
<div>
<h1 class="title">Ingredients</h1>
2018-03-30 14:31:09 -05:00
2018-04-07 10:54:56 -05:00
<div class="buttons">
2018-09-11 22:56:26 -05:00
<router-link v-if="isLoggedIn" :to="{name: 'new_food'}" class="button is-primary">Create Ingredient</router-link>
2018-04-07 10:54:56 -05:00
</div>
2018-04-03 18:31:20 -05:00
2018-09-11 22:56:26 -05:00
<app-pager :current-page="currentPage" :total-pages="totalPages" paged-item-name="food" @changePage="changePage"></app-pager>
2018-04-02 00:10:06 -05:00
2018-04-07 10:54:56 -05:00
<table class="table is-fullwidth is-narrow">
2018-04-02 00:10:06 -05:00
<thead>
<tr>
<th>Name</th>
<th>USDA</th>
<th>KCal per 100g</th>
2018-04-03 18:31:20 -05:00
<th>Density (oz/cup)</th>
2018-04-02 00:10:06 -05:00
<th></th>
</tr>
<tr>
<th>
<div class="field">
<div class="control">
<input type="text" class="input" placeholder="search names" v-model="search.name">
</div>
</div>
</th>
<th colspan="4"></th>
</tr>
</thead>
<tbody>
2018-09-11 22:56:26 -05:00
<tr v-for="i in foods" :key="i.id">
<td><router-link :to="{name: 'food', params: { id: i.id } }">{{i.name}}</router-link></td>
2018-04-02 00:10:06 -05:00
<td><app-icon v-if="i.usda" icon="check"></app-icon></td>
<td>{{i.kcal}}</td>
<td>{{i.density}}</td>
<td>
2018-09-11 22:56:26 -05:00
<router-link v-if="isLoggedIn" class="button" :to="{name: 'edit_food', params: { id: i.id } }">
2018-04-03 18:31:20 -05:00
<app-icon icon="pencil"></app-icon>
</router-link>
2018-09-11 22:56:26 -05:00
<button v-if="isLoggedIn" type="button" class="button is-danger" @click="deleteFood(i)">
2018-04-03 18:31:20 -05:00
<app-icon icon="x"></app-icon>
</button>
2018-04-02 00:10:06 -05:00
</td>
</tr>
</tbody>
</table>
2018-09-11 22:56:26 -05:00
<app-pager :current-page="currentPage" :total-pages="totalPages" paged-item-name="food" @changePage="changePage"></app-pager>
2018-04-03 18:31:20 -05:00
2018-04-07 10:54:56 -05:00
<div class="buttons">
2018-09-11 22:56:26 -05:00
<router-link v-if="isLoggedIn" :to="{name: 'new_food'}" class="button is-primary">Create Ingredient</router-link>
2018-04-07 10:54:56 -05:00
</div>
2018-09-11 22:56:26 -05:00
<app-confirm :open="showConfirmFoodDelete" :message="confirmFoodDeleteMessage" :cancel="foodDeleteCancel" :confirm="foodDeleteConfirm"></app-confirm>
2018-05-01 10:55:57 -05:00
2018-04-02 00:10:06 -05:00
</div>
2018-03-30 14:31:09 -05:00
</template>
<script>
2018-04-02 00:10:06 -05:00
import api from "../lib/Api";
import debounce from "lodash/debounce";
2018-03-30 14:31:09 -05:00
export default {
2018-04-02 00:10:06 -05:00
data() {
return {
2018-09-11 22:56:26 -05:00
foodData: null,
foodForDeletion: null,
2018-04-02 00:10:06 -05:00
search: {
page: 1,
per: 25,
name: null
}
};
},
2018-03-30 14:31:09 -05:00
2018-04-02 00:10:06 -05:00
computed: {
2018-09-11 22:56:26 -05:00
foods() {
if (this.foodData) {
return this.foodData.foods;
2018-04-02 00:10:06 -05:00
} else {
return [];
}
2018-04-03 18:31:20 -05:00
},
totalPages() {
2018-09-11 22:56:26 -05:00
if (this.foodData) {
return this.foodData.total_pages
2018-04-03 18:31:20 -05:00
}
return 0;
},
currentPage() {
2018-09-11 22:56:26 -05:00
if (this.foodData) {
return this.foodData.current_page
2018-04-03 18:31:20 -05:00
}
return 0;
2018-05-01 10:55:57 -05:00
},
2018-09-11 22:56:26 -05:00
showConfirmFoodDelete() {
return this.foodForDeletion !== null;
2018-05-01 10:55:57 -05:00
},
2018-09-11 22:56:26 -05:00
confirmFoodDeleteMessage() {
if (this.foodForDeletion !== null) {
return `Are you sure you want to delete ${this.foodForDeletion.name}?`;
2018-05-01 10:55:57 -05:00
} else {
return "??";
}
2018-04-02 00:10:06 -05:00
}
},
2018-03-30 14:31:09 -05:00
2018-04-02 00:10:06 -05:00
methods: {
2018-04-03 18:31:20 -05:00
changePage(idx) {
this.search.page = idx;
},
2018-04-02 00:10:06 -05:00
getList: debounce(function() {
2018-05-01 10:55:57 -05:00
return this.loadResource(
2018-09-11 22:56:26 -05:00
api.getFoodList(this.search.page, this.search.per, this.search.name)
.then(data => this.foodData = data)
2018-04-02 00:10:06 -05:00
);
2018-05-01 10:55:57 -05:00
}, 500, {leading: true, trailing: true}),
2018-09-11 22:56:26 -05:00
deleteFood(food) {
this.foodForDeletion = food;
2018-05-01 10:55:57 -05:00
},
2018-09-11 22:56:26 -05:00
foodDeleteCancel() {
this.foodForDeletion = null;
2018-05-01 10:55:57 -05:00
},
2018-09-11 22:56:26 -05:00
foodDeleteConfirm() {
if (this.foodForDeletion !== null) {
2018-05-01 10:55:57 -05:00
this.loadResource(
2018-09-11 22:56:26 -05:00
api.deleteFood(this.foodForDeletion.id).then(res => {
this.foodForDeletion = null;
2018-05-01 10:55:57 -05:00
return this.getList();
})
);
console.log("This is where the thing happens!!");
2018-09-11 22:56:26 -05:00
this.foodForDeletion = null;
2018-05-01 10:55:57 -05:00
}
}
2018-04-02 00:10:06 -05:00
},
created() {
this.$watch("search",
() => this.getList(),
{
deep: true,
immediate: true
}
);
},
components: {
}
}
2018-03-30 14:31:09 -05:00
2018-04-02 00:10:06 -05:00
</script>