<template>
  <div>

    <ingredient-edit :ingredient="ingredient" action="Creating"></ingredient-edit>

    <button type="button" class="button is-primary" @click="save">Save</button>
    <router-link class="button is-secondary" to="/ingredients">Cancel</router-link>

  </div>
</template>

<script>

  import IngredientEdit from "./IngredientEdit";
  import { mapState } from "vuex";
  import api from "../lib/Api";
  import * as Errors from '../lib/Errors';

  export default {
    data() {
      return {
        ingredient: {
          name: null
        },
        validationErrors: null
      }
    },

    methods: {
      save() {
        this.loadResource(
          api.postIngredient(this.ingredient)
            .then(() => this.$router.push('/ingredients'))
            .catch(Errors.onlyFor(Errors.ApiValidationError, err => this.validationErrors = err.validationErrors()))
        );
      }
    },

    components: {
      IngredientEdit
    }
  }

</script>

<style lang="scss" scoped>
</style>