<template>
  <div>
    <div v-if="ingredient === null">
      Loading...
    </div>
    <div v-else>
      <ingredient-show :ingredient="ingredient"></ingredient-show>
    </div>

    <router-link v-if="isLoggedIn" class="button" :to="{name: 'edit_ingredient', params: { id: ingredientId }}">Edit</router-link>
    <router-link class="button" to="/ingredients">Back</router-link>
  </div>
</template>

<script>

  import IngredientShow from "./IngredientShow";
  import { mapState } from "vuex";
  import api from "../lib/Api";

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

    computed: {
      ...mapState({
        ingredientId: state => state.route.params.id,
      })
    },

    created() {
      this.loadResource(
        api.getIngredient(this.ingredientId)
          .then(data => { this.ingredient = data; return data; })
      );
    },

    components: {
      IngredientShow
    }
  }

</script>

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