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-03 18:31:20 -05:00
|
|
|
<router-link v-if="isLoggedIn" :to="{name: 'new_ingredient'}" class="button is-primary">Create Ingredient</router-link>
|
|
|
|
|
|
|
|
<app-pager :current-page="currentPage" :total-pages="totalPages" paged-item-name="ingredient" @changePage="changePage"></app-pager>
|
2018-04-02 00:10:06 -05:00
|
|
|
|
|
|
|
<table class="table">
|
|
|
|
<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>
|
|
|
|
<tr v-for="i in ingredients" :key="i.id">
|
|
|
|
<td><router-link :to="{name: 'ingredient', params: { id: i.id } }">{{i.name}}</router-link></td>
|
|
|
|
<td><app-icon v-if="i.usda" icon="check"></app-icon></td>
|
|
|
|
<td>{{i.kcal}}</td>
|
|
|
|
<td>{{i.density}}</td>
|
|
|
|
<td>
|
2018-04-03 18:31:20 -05:00
|
|
|
<router-link v-if="isLoggedIn" class="button" :to="{name: 'edit_ingredient', params: { id: i.id } }">
|
|
|
|
<app-icon icon="pencil"></app-icon>
|
|
|
|
</router-link>
|
|
|
|
<button v-if="isLoggedIn" type="button" class="button is-danger">
|
|
|
|
<app-icon icon="x"></app-icon>
|
|
|
|
</button>
|
2018-04-02 00:10:06 -05:00
|
|
|
</td>
|
|
|
|
</tr>
|
|
|
|
</tbody>
|
|
|
|
</table>
|
|
|
|
|
2018-04-03 18:31:20 -05:00
|
|
|
<app-pager :current-page="currentPage" :total-pages="totalPages" paged-item-name="ingredient" @changePage="changePage"></app-pager>
|
|
|
|
|
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 {
|
|
|
|
ingredientData: null,
|
|
|
|
search: {
|
|
|
|
page: 1,
|
|
|
|
per: 25,
|
|
|
|
name: null
|
|
|
|
}
|
|
|
|
};
|
|
|
|
},
|
2018-03-30 14:31:09 -05:00
|
|
|
|
2018-04-02 00:10:06 -05:00
|
|
|
computed: {
|
|
|
|
ingredients() {
|
|
|
|
if (this.ingredientData) {
|
|
|
|
return this.ingredientData.ingredients;
|
|
|
|
} else {
|
|
|
|
return [];
|
|
|
|
}
|
2018-04-03 18:31:20 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
totalPages() {
|
|
|
|
if (this.ingredientData) {
|
|
|
|
return this.ingredientData.total_pages
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
},
|
|
|
|
|
|
|
|
currentPage() {
|
|
|
|
if (this.ingredientData) {
|
|
|
|
return this.ingredientData.current_page
|
|
|
|
}
|
|
|
|
return 0;
|
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() {
|
|
|
|
this.loadResource(
|
|
|
|
api.getIngredientList(this.search.page, this.search.per, this.search.name)
|
|
|
|
.then(data => this.ingredientData = data)
|
|
|
|
);
|
|
|
|
}, 500, {leading: true, trailing: true})
|
|
|
|
},
|
|
|
|
|
|
|
|
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>
|