DigitalConsumer.ISS/src/router/index.js
Kulbhushan Kaushik 073d5dfee1 commit
2022-09-15 08:18:17 -04:00

81 lines
No EOL
2.9 KiB
JavaScript

import { storeActions } from "@/constants/store-actions";
import { createWebHistory, createRouter } from "vue-router";
const routes = [
{
path: '/',
name: 'root',
async beforeEnter(to, from, next) {
try {
if (router.hasRoute(to.query.issPage)) {
return next({ name: to.query.issPage, query: to.query, params: to.params });
}
const routeData = await GetRouteInfoFromPageName(to.query.issPage);
// Add our dynamic route.
router.addRoute({
path: routeData[0].path, // Always the same path, because we control it with query strings.
name: routeData[0].name,
component: routeData[0].component,
});
// Assign current query string parameters, as well as our fmgPage one.
next({
name: routeData[0].name,
query: Object.assign(to.query, { issPage: routeData[0].name }),
params: to.params
});
} catch (error) {
console.log(error);
await GoToStartOn404(next);
}
}
}];
const router = createRouter({
history: createWebHistory("/"),
routes,
});
// Navigate to the next route, depending on the scenario.
async function navigate(scenario, currentRoute, optionalQuery = {}, optionalParams = {}) {
if (!scenario) {
console.error("No scenario provided. Please review the routing table.");
return;
}
// Match our maps up and navigate if we have a destination.
const matchingScenarioMap = getNavigationMap(scenario, currentRoute);
const destinationIssPageValue = matchingScenarioMap.destinationIssPageValue;
if (destinationIssPageValue !== undefined) {
// We're always pushing the same path, just changing query strings. Make sure our optional query strings get combined with our issPage one.
router.push({
name: "root",
query: Object.assign(optionalQuery, {
fmgPage: destinationIssPageValue,
}),
params: optionalParams
});
} else if (matchingScenarioMap.destinationUrl !== undefined) {
navigateToUrl(matchingScenarioMap.destinationUrl, optionalQuery);
}
}
// Get navigation map depending on the scenario and the current 'page' you're on.
function getNavigationMap(scenario, currentRoute) {
const issPageValue = currentRoute.query.issPage;
const matchedQueryValue = routingTable(store)
.filter(
(item) =>
item.issPageValue === issPageValue &&
item.maps.filter((map) => map.scenario === scenario).length > 0
)
.map((m) => m.maps.filter((map) => map.scenario === scenario))[0]
.filter(x => x.filter === true || x.filter === undefined);
return matchedQueryValue[0];
}
export default router;