import Vue from "vue";
import VueRouter from "vue-router";
import Fruits from "@/views/Fruits.vue";
import Fruit from "@/views/Fruit.vue";
import store from "@/store/index";

Vue.use(VueRouter);

const routes = [
  {
    path: "/",
    name: "FruitsDirectory",
    component: Fruits
  },
  {
    path: "/fruit/:id",
    name: "FruitDetails",
    component: Fruit,
    beforeEnter: async (to, from, next) => {
      await store.dispatch("getFruit", to.params.id);
      next();
    }
  }
];

const router = new VueRouter({
  mode: "history",
  base: process.env.BASE_URL,
  routes
});

export default router;
