parsley/app/javascript/components/FoodEdit.vue

242 lines
6.3 KiB
Vue
Raw Permalink Normal View History

2018-04-02 00:10:06 -05:00
<template>
<div>
2018-09-11 22:56:26 -05:00
<h1 class="title">{{action}} {{food.name || "[Unnamed Food]"}}</h1>
2018-04-03 10:29:57 -05:00
2018-06-09 12:36:46 -05:00
<app-validation-errors :errors="validationErrors"></app-validation-errors>
2018-04-03 10:29:57 -05:00
<div class="field">
<label class="label is-small-mobile">Name</label>
<div class="control">
2018-09-11 22:56:26 -05:00
<input type="text" class="input is-small-mobile" v-model="food.name">
2018-04-03 10:29:57 -05:00
</div>
</div>
2018-04-03 18:31:20 -05:00
<label class="label is-small-mobile">Nutrient Databank Number</label>
<div class="field has-addons">
2018-04-03 10:29:57 -05:00
<div class="control">
2018-09-11 22:56:26 -05:00
<button type="button" class="button" :class="{'is-primary': hasNdbn}"><app-icon :icon="hasNdbn ? 'link-intact' : 'link-broken'" size="sm"></app-icon><span>{{food.ndbn}}</span></button>
2018-04-03 18:31:20 -05:00
</div>
<div class="control is-expanded">
<app-autocomplete
:inputClass="'is-small-mobile'"
ref="autocomplete"
2018-09-11 22:56:26 -05:00
v-model="food.usda_food_name"
2018-04-03 18:31:20 -05:00
:minLength="2"
valueAttribute="name"
labelAttribute="description"
2018-09-14 19:32:49 -05:00
key-attribute="ndbn"
2018-04-03 18:31:20 -05:00
placeholder=""
@optionSelected="searchItemSelected"
:onGetOptions="updateSearchItems"
>
</app-autocomplete>
</div>
<div v-if="hasNdbn" class="control">
<button type="button" class="button is-danger" @click="removeNdbn">X</button>
2018-04-03 10:29:57 -05:00
</div>
</div>
<div class="field">
<label class="label is-small-mobile">Density</label>
<div class="control">
2018-09-11 22:56:26 -05:00
<input type="text" class="input is-small-mobile" v-model="food.density">
2018-04-03 10:29:57 -05:00
</div>
</div>
<div class="field">
<label class="label is-small-mobile">Notes</label>
<div class="control">
2018-09-11 22:56:26 -05:00
<textarea type="text" class="textarea is-small-mobile" v-model="food.notes"></textarea>
2018-04-03 10:29:57 -05:00
</div>
</div>
2018-04-03 18:31:20 -05:00
<div class="columns">
<div class="column">
<div class="message">
<div class="message-header">
Custom Units
</div>
2018-04-03 10:29:57 -05:00
2018-04-03 18:31:20 -05:00
<div class="message-body">
<button class="button" type="button" @click="addUnit">Add Unit</button>
<table class="table">
<tr>
<th>Name</th>
<th>Grams</th>
<th></th>
</tr>
2018-09-11 22:56:26 -05:00
<tr v-for="unit in visibleFoodUnits" :key="unit.id">
2018-04-03 18:31:20 -05:00
<td>
<div class="control">
<input type="text" class="input is-small-mobile" v-model="unit.name">
</div>
</td>
<td>
<div class="control">
<input type="text" class="input is-small-mobile" v-model="unit.gram_weight">
</div>
</td>
<td>
<button type="button" class="button is-danger" @click="removeUnit(unit)">X</button>
</td>
</tr>
</table>
2018-04-03 10:29:57 -05:00
</div>
</div>
</div>
2018-04-03 18:31:20 -05:00
<div class="column">
<div class="message">
<div class="message-header">
NDBN Units
</div>
<div class="message-body">
<table class="table">
<tr>
<th>Name</th>
<th>Grams</th>
</tr>
2018-09-11 22:56:26 -05:00
<tr v-for="unit in food.ndbn_units">
2018-04-03 18:31:20 -05:00
<td>{{unit.description}}</td>
<td>{{unit.gram_weight}}</td>
</tr>
</table>
</div>
</div>
</div>
</div>
<div class="message">
<div class="message-header">
Nutrition per 100 grams
</div>
<div class="message-body">
<div class="columns is-mobile is-multiline">
<div v-for="(nutrient, name) in nutrients" :key="name" class="column is-half-mobile is-one-third-tablet">
<label class="label is-small-mobile">{{nutrient.label}}</label>
<div class="field has-addons">
<div class="control is-expanded">
2018-09-11 22:56:26 -05:00
<input type="text" class="input is-small-mobile" :disabled="hasNdbn" v-model="food[name]">
2018-04-03 18:31:20 -05:00
</div>
<div class="control">
<button type="button" tabindex="-1" class="unit-label button is-static is-small-mobile">{{nutrient.unit}}</button>
</div>
</div>
</div>
</div>
</div>
</div>
2018-04-03 10:29:57 -05:00
2018-04-02 00:10:06 -05:00
</div>
</template>
<script>
2018-04-03 18:31:20 -05:00
import api from "../lib/Api";
2018-09-14 19:32:49 -05:00
import { mapState } from "vuex";
2018-04-03 10:29:57 -05:00
2018-04-02 00:10:06 -05:00
export default {
props: {
2018-09-11 22:56:26 -05:00
food: {
2018-04-02 00:10:06 -05:00
required: true,
type: Object
},
2018-06-09 12:36:46 -05:00
validationErrors: {
required: false,
type: Object,
default: {}
},
2018-04-02 00:10:06 -05:00
action: {
required: false,
type: String,
default: "Editing"
}
2018-04-03 10:29:57 -05:00
},
data() {
return {
2018-09-14 19:32:49 -05:00
2018-04-03 10:29:57 -05:00
};
},
2018-04-03 18:31:20 -05:00
computed: {
2018-09-14 19:32:49 -05:00
...mapState({
nutrients: 'nutrientList'
}),
2018-09-11 22:56:26 -05:00
visibleFoodUnits() {
return this.food.food_units.filter(iu => iu._destroy !== true);
2018-04-03 18:31:20 -05:00
},
hasNdbn() {
2018-09-11 22:56:26 -05:00
return this.food.ndbn !== null;
2018-04-03 18:31:20 -05:00
}
},
methods: {
addUnit() {
2018-09-11 22:56:26 -05:00
this.food.food_units.push({
2018-04-03 18:31:20 -05:00
id: null,
name: null,
gram_weight: null
});
},
removeUnit(unit) {
if (unit.id) {
unit._destroy = true;
} else {
2018-09-11 22:56:26 -05:00
const idx = this.food.food_units.findIndex(i => i === unit);
this.food.food_units.splice(idx, 1);
2018-04-03 18:31:20 -05:00
}
},
removeNdbn() {
2018-09-11 22:56:26 -05:00
this.food.ndbn = null;
this.food.usda_food_name = null;
this.food.ndbn_units = [];
2018-04-03 18:31:20 -05:00
},
updateSearchItems(text) {
return api.getUsdaFoodSearch(text)
.then(data => data.map(f => {
return {
name: f.name,
ndbn: f.ndbn,
description: ["#", f.ndbn, ", Cal:", f.kcal, ", Carbs:", f.carbohydrates, ", Fat:", f.lipid, ", Protein:", f.protein].join("")
}
}));
},
searchItemSelected(food) {
2018-09-11 22:56:26 -05:00
this.food.ndbn = food.ndbn;
this.food.usda_food_name = food.name;
this.food.ndbn_units = [];
2018-04-03 18:31:20 -05:00
this.loadResource(
2018-09-11 22:56:26 -05:00
api.postIngredientSelectNdbn(this.food)
.then(i => Object.assign(this.food, i))
2018-04-03 18:31:20 -05:00
);
},
},
2018-04-03 10:29:57 -05:00
components: {
2018-04-02 00:10:06 -05:00
}
}
</script>
<style lang="scss" scoped>
2018-04-03 10:29:57 -05:00
.unit-label {
width: 3em;
}
2018-04-02 00:10:06 -05:00
</style>