<template>
  <div>
    <div v-if="recipe === null">
      Loading...
    </div>
    <div v-else>
      <h1 class="title">{{ recipe.name }}</h1>
      <div class="subtitle tags">
        <span v-for="tag in recipe.tags" :key="tag" class="tag is-medium">{{tag}}</span>
      </div>
      <hr>
      <recipe-show :recipe="recipe"></recipe-show>
    </div>

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

<script>

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

  export default {
    data: function () {
      return {
        recipe: null,
        showNutrition: false
      }
    },

    computed: {
      ...mapState({
        recipeId: state => state.route.params.id,
        scale: state => state.route.query.scale || null,
        system: state => state.route.query.system || null,
        unit: state => state.route.query.unit || null
      })
    },

    created() {
      this.loadResource(
        api.getRecipe(this.recipeId, this.scale, this.system, this.unit)
          .then(data => { this.recipe = data; return data; })
      );
    },

    components: {
      RecipeShow
    }
  }

</script>

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