2018-04-02 00:10:06 -05:00
|
|
|
<template>
|
|
|
|
<div>
|
|
|
|
|
2018-09-11 22:56:26 -05:00
|
|
|
<div v-if="food === null">
|
2018-04-02 00:10:06 -05:00
|
|
|
Loading...
|
|
|
|
</div>
|
|
|
|
<div v-else>
|
2018-09-11 22:56:26 -05:00
|
|
|
<food-edit :food="food" :validation-errors="validationErrors"></food-edit>
|
2018-04-02 00:10:06 -05:00
|
|
|
</div>
|
|
|
|
|
|
|
|
<button type="button" class="button is-primary" @click="save">Save</button>
|
2018-09-11 22:56:26 -05:00
|
|
|
<router-link class="button is-secondary" to="/foods">Cancel</router-link>
|
2018-04-02 00:10:06 -05:00
|
|
|
|
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
|
|
|
|
2018-09-11 22:56:26 -05:00
|
|
|
import FoodEdit from "./FoodEdit";
|
2018-04-02 00:10:06 -05:00
|
|
|
import { mapState } from "vuex";
|
|
|
|
import api from "../lib/Api";
|
|
|
|
import * as Errors from '../lib/Errors';
|
|
|
|
|
|
|
|
export default {
|
|
|
|
data: function () {
|
|
|
|
return {
|
2018-09-11 22:56:26 -05:00
|
|
|
food: null,
|
2018-06-09 12:36:46 -05:00
|
|
|
validationErrors: {}
|
2018-04-02 00:10:06 -05:00
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
computed: {
|
|
|
|
...mapState({
|
2018-09-11 22:56:26 -05:00
|
|
|
foodId: state => state.route.params.id,
|
2018-04-02 00:10:06 -05:00
|
|
|
})
|
|
|
|
},
|
|
|
|
|
|
|
|
methods: {
|
|
|
|
save() {
|
2018-06-09 12:36:46 -05:00
|
|
|
this.validationErrors = {};
|
2018-04-02 00:10:06 -05:00
|
|
|
this.loadResource(
|
2018-09-11 22:56:26 -05:00
|
|
|
api.patchFood(this.food)
|
|
|
|
.then(() => this.$router.push({name: 'food', params: {id: this.foodId }}))
|
2018-04-02 00:10:06 -05:00
|
|
|
.catch(Errors.onlyFor(Errors.ApiValidationError, err => this.validationErrors = err.validationErrors()))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
created() {
|
|
|
|
this.loadResource(
|
2018-09-11 22:56:26 -05:00
|
|
|
api.getFood(this.foodId)
|
|
|
|
.then(data => { this.food = data; return data; })
|
2018-04-02 00:10:06 -05:00
|
|
|
);
|
|
|
|
},
|
|
|
|
|
|
|
|
components: {
|
2018-09-11 22:56:26 -05:00
|
|
|
FoodEdit
|
2018-04-02 00:10:06 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<style lang="scss" scoped>
|
|
|
|
</style>
|