view client/src/router.js @ 3678:8f58851927c0

client: make layer factory only return new layer config for individual maps instead of each time it is invoked. The purpose of the factory was to support multiple maps with individual layers. But returning a new config each time it is invoked leads to bugs that rely on the layer's state. Now this factory reuses the same objects it created before, per map.
author Markus Kottlaender <markus@intevation.de>
date Mon, 17 Jun 2019 17:31:35 +0200
parents 9b02c0b00d7b
children 0300282b9537
line wrap: on
line source

/* This is Free Software under GNU Affero General Public License v >= 3.0
 * without warranty, see README.md and license for details.
 *
 * SPDX-License-Identifier: AGPL-3.0-or-later
 * License-Filename: LICENSES/AGPL-3.0.txt
 *
 * Copyright (C) 2018 by via donau
 *   – Österreichische Wasserstraßen-Gesellschaft mbH
 * Software engineering by Intevation GmbH
 *
 * Author(s):
 * Thomas Junk <thomas.junk@intevation.de>
 * Bernhard Reiter <bernhard@intevation.de>
 * Markus Kottländer <markus@intevation.de>
 */
import Vue from "vue";
import Router from "vue-router";
import store from "./store";
import { sessionStillActive, toMillisFromString } from "./lib/session";

/*  facilitate codesplitting */
const Login = () => import("./components/Login");
const Main = () => import("./components/Main");

Vue.use(Router);

const router = new Router({
  routes: [
    {
      path: "/login",
      name: "login",
      component: Login
    },
    {
      path: "/usermanagement",
      name: "usermanagement",
      component: () => import("./components/usermanagement/Usermanagement"),
      meta: {
        requiresAuth: true
      },
      beforeEnter: (to, from, next) => {
        const isSysadmin = store.getters["user/isSysAdmin"];
        if (!isSysadmin) {
          next("/login");
        } else {
          next();
        }
      }
    },
    {
      path: "/logs",
      name: "logs",
      component: () => import("./components/Logs"),
      meta: {
        requiresAuth: true
      },
      beforeEnter: (to, from, next) => {
        const isSysadmin = store.getters["user/isSysAdmin"];
        if (!isSysadmin) {
          next("/login");
        } else {
          next();
        }
      }
    },
    {
      path: "/systemconfiguration",
      name: "systemconfiguration",
      component: () =>
        import("./components/systemconfiguration/Systemconfiguration"),
      meta: {
        requiresAuth: true
      },
      beforeEnter: (to, from, next) => {
        const isWaterwayAdmin = store.getters["user/isWaterwayAdmin"];
        if (!isWaterwayAdmin) {
          next("/login");
        } else {
          next();
        }
      }
    },
    {
      path: "/",
      name: "mainview",
      component: Main,
      meta: {
        requiresAuth: true
      },
      beforeEnter: (to, from, next) => {
        store.commit("application/searchQuery", "");
        store.commit("application/showContextBox", false);
        store.commit("application/contextBoxContent", "");
        store.commit("application/showSearchbar", false);
        next();
      }
    },
    {
      path: "/bottlenecks",
      name: "bottlenecks",
      component: Main,
      meta: {
        requiresAuth: true
      },
      beforeEnter: (to, from, next) => {
        store.commit("application/searchQuery", "");
        store.commit("application/showContextBox", true);
        store.commit("application/contextBoxContent", "bottlenecks");
        store.commit("application/showSearchbar", true);
        next();
      }
    },
    {
      path: "/imports/configuration",
      name: "importconfiguration",
      component: Main,
      meta: {
        requiresAuth: true
      },
      beforeEnter: (to, from, next) => {
        const isWaterwayAdmin = store.getters["user/isWaterwayAdmin"];
        if (!isWaterwayAdmin) {
          next("/login");
        } else {
          store.commit("application/searchQuery", "");
          store.commit("application/showContextBox", true);
          store.commit("application/contextBoxContent", "importconfiguration");
          store.commit("application/showSearchbar", true);
          next();
        }
      }
    },
    {
      path: "/imports/overview/:id?",
      name: "importoverview",
      component: Main,
      meta: {
        requiresAuth: true
      },
      beforeEnter: (to, from, next) => {
        const isWaterwayAdmin = store.getters["user/isWaterwayAdmin"];
        if (!isWaterwayAdmin) {
          next("/login");
        } else {
          store.commit("application/showContextBox", true);
          store.commit("application/contextBoxContent", "importoverview");
          store.commit("application/showSearchbar", true);
          next();
        }
      }
    },
    {
      path: "/stretches",
      name: "stretches",
      component: Main,
      meta: {
        requiresAuth: true
      },
      beforeEnter: (to, from, next) => {
        const isSysadmin = store.getters["user/isSysAdmin"];
        if (!isSysadmin) {
          next("/login");
        } else {
          store.commit("application/searchQuery", "");
          store.commit("application/showContextBox", true);
          store.commit("application/contextBoxContent", "stretches");
          store.commit("application/showSearchbar", true);
          next();
        }
      }
    },
    {
      path: "/sections",
      name: "sections",
      component: Main,
      meta: {
        requiresAuth: true
      },
      beforeEnter: (to, from, next) => {
        const isWaterwayAdmin = store.getters["user/isWaterwayAdmin"];
        if (!isWaterwayAdmin) {
          next("/login");
        } else {
          store.commit("application/searchQuery", "");
          store.commit("application/showContextBox", true);
          store.commit("application/contextBoxContent", "sections");
          store.commit("application/showSearchbar", true);
          next();
        }
      }
    },
    {
      path: "/fairwaydepth",
      name: "fairwaydepth",
      component: () => import("./components/fairway/AvailableFairwayDepth"),
      meta: {
        requiresAuth: true
      }
    },
    {
      path: "*",
      component: () => import("./components/PageNotFound")
    }
  ]
});

router.beforeEach((to, from, next) => {
  const expiresFromPastSession = toMillisFromString(
    localStorage.getItem("expires")
  );
  if (sessionStillActive(expiresFromPastSession)) {
    store.commit("user/setUser", localStorage.getItem("user"));
    store.commit("user/setExpires", expiresFromPastSession);
    store.commit("user/setRoles", localStorage.getItem("roles"));
    store.commit("user/setIsAuthenticate", true);
    store.dispatch("application/loadConfig");
  } else {
    store.commit("reset");
    store.commit("user/clearAuth");
  }
  const requiresAuth = to.matched.some(record => record.meta.requiresAuth);
  const redirectToLogin = requiresAuth && !store.state.user.isAuthenticated;
  if (redirectToLogin) {
    localStorage.setItem("tempRoute", to.path);
    next("/login");
  } else {
    next();
  }
});

export default router;