parsley/app/javascript/components/TheFoodEditor.vue

53 lines
1.4 KiB
Vue
Raw Normal View History

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>
2024-10-02 14:34:50 -05:00
<script setup>
2018-04-02 00:10:06 -05:00
2024-10-02 14:34:50 -05:00
import { computed, onBeforeMount, ref } from "vue";
import { useRoute, useRouter } from "vue-router";
2018-09-11 22:56:26 -05:00
import FoodEdit from "./FoodEdit";
2018-04-02 00:10:06 -05:00
import api from "../lib/Api";
import * as Errors from '../lib/Errors';
2024-10-02 14:34:50 -05:00
import { useLoadResource } from "../lib/useLoadResource";
const { loadResource } = useLoadResource();
const router = useRouter();
const route = useRoute();
const food = ref(null);
const validationErrors = ref({});
const foodId = computed(() => route.params.id);
onBeforeMount(() => {
loadResource(
api.getFood(foodId.value)
.then(data => { food.value = data; return data; })
);
});
function save() {
validationErrors.value = {};
loadResource(
api.patchFood(food.value)
.then(() => router.push({name: 'food', params: {id: foodId.value }}))
.catch(Errors.onlyFor(Errors.ApiValidationError, err => validationErrors.value = err.validationErrors()))
);
2018-04-02 00:10:06 -05:00
}
</script>
<style lang="scss" scoped>
</style>