Fixed interface for Backend, allow redirect mainLinks

* Fixed the Backend interface to match the returned data structure
* Allow empty parents in RouteConfig, use flatRoutes
This commit is contained in:
Ferdinand Thiessen 2020-11-24 17:35:11 +01:00
parent c05fc5d877
commit d4795a549f
2 changed files with 63 additions and 45 deletions

View File

@ -1,12 +1,13 @@
import {boot} from 'quasar/wrappers'; import { boot } from 'quasar/wrappers';
import {RouteConfig} from 'vue-router'; import { RouteConfig } from 'vue-router';
import {VueRouter} from 'vue-router/types/router'; import { VueRouter } from 'vue-router/types/router';
import {Store} from 'vuex'; import { Store } from 'vuex';
import {StateInterface} from 'src/store'; import { StateInterface } from 'src/store';
import {FG_Plugin} from 'src/plugins'; import { FG_Plugin } from 'src/plugins';
import routes from 'src/router/routes'; import routes from 'src/router/routes';
import {axios} from 'boot/axios'; import { axios } from 'boot/axios';
import {AxiosResponse} from 'axios'; import { AxiosResponse } from 'axios';
import { flatRoutes } from '@dreamonkey/vue-routes-flattener';
const config = { const config = {
// Do not change required Modules !! // Do not change required Modules !!
@ -25,15 +26,15 @@ interface BackendPlugin {
} }
interface BackendPlugins { interface BackendPlugins {
[key: string]: BackendPlugin [key: string]: BackendPlugin | null;
} }
interface Backend { interface Backend {
plugins: [key: string]; plugins: BackendPlugins[];
version: string; version: string;
} }
export {Backend} export { Backend };
function combineRoutes( function combineRoutes(
target: RouteConfig[], target: RouteConfig[],
@ -200,9 +201,9 @@ function loadPlugin(
}); });
} else { } else {
console.exception(`Could not find required Plugin ${requiredModule}`); console.exception(`Could not find required Plugin ${requiredModule}`);
router.push({name: 'error'}).catch((e) => { router.push({ name: 'error' }).catch(e => {
console.warn(e) console.warn(e);
}) });
} }
}); });
return loadedPlugins; return loadedPlugins;
@ -223,7 +224,7 @@ async function getBackend(): Promise<Backend | null> {
// "async" is optional; // "async" is optional;
// more info on params: https://quasar.dev/quasar-cli/cli-documentation/boot-files#Anatomy-of-a-boot-file // more info on params: https://quasar.dev/quasar-cli/cli-documentation/boot-files#Anatomy-of-a-boot-file
export default boot<Store<StateInterface>>(({Vue, router, store}) => { export default boot<Store<StateInterface>>(({ Vue, router, store }) => {
const plugins: FG_Plugin.Plugin[] = []; const plugins: FG_Plugin.Plugin[] = [];
const backendPromise = getBackend(); const backendPromise = getBackend();
@ -245,33 +246,44 @@ export default boot<Store<StateInterface>>(({Vue, router, store}) => {
}); });
// check dependencies // check dependencies
backendPromise.then((backend) => { backendPromise
console.log(backend) .then(backend => {
if (backend) { console.log(backend);
plugins.forEach((plugin: FG_Plugin.Plugin) => { if (backend) {
plugin.requiredModules.forEach((requiredModule: string) => { plugins.forEach((plugin: FG_Plugin.Plugin) => {
if (!(config.requiredModules.includes(requiredModule) || config.loadModules.includes(requiredModule))) { plugin.requiredModules.forEach((requiredModule: string) => {
console.error(`Plugin ${plugin.name} need Plugin ${requiredModule}`) if (
router.push({name: 'error'}).catch((e) => { !(
console.warn(e) config.requiredModules.includes(requiredModule) ||
}) config.loadModules.includes(requiredModule)
} )
}) ) {
plugin.requiredBackendModules.forEach((requiredBackendModule: string) => { console.error(
if (!(requiredBackendModule in backend.plugins)) { `Plugin ${plugin.name} need Plugin ${requiredModule}`
console.error(`Plugin ${plugin.name} need Plugin ${requiredBackendModule} in backend.`) );
router.push({name: 'error'}).catch(err => { router.push({ name: 'error' }).catch(e => {
console.warn(err) console.warn(e);
}) });
} }
}) });
}) plugin.requiredBackendModules.forEach(
} (requiredBackendModule: string) => {
}) if (!(requiredBackendModule in backend.plugins)) {
.catch(e => { console.error(
console.error(e) `Plugin ${plugin.name} need Plugin ${requiredBackendModule} in backend.`
);
router.push({ name: 'error' }).catch(err => {
console.warn(err);
});
}
}
);
});
}
}) })
.catch(e => {
console.error(e);
});
// load plugins // load plugins
loadedPlugins = loadPlugin( loadedPlugins = loadPlugin(
@ -282,12 +294,19 @@ export default boot<Store<StateInterface>>(({Vue, router, store}) => {
store, store,
router router
); );
loadedPlugins = loadPlugin(loadedPlugins, config.loadModules, backendPromise, plugins, store, router); loadedPlugins = loadPlugin(
loadedPlugins,
config.loadModules,
backendPromise,
plugins,
store,
router
);
loadedPlugins.widgets.sort((a, b) => b.priority - a.priority); loadedPlugins.widgets.sort((a, b) => b.priority - a.priority);
// add new routes for plugins // add new routes for plugins, flatten them to allow empty parent routes
router.addRoutes(loadedPlugins.routes); router.addRoutes(flatRoutes(loadedPlugins.routes));
// save plugins in VM-variable // save plugins in VM-variable
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access

View File

@ -1,6 +1,5 @@
<template> <template>
<q-input <q-input
class="col-xs-12 col-sm-6 q-pa-sm"
filled filled
:readonly="readonly" :readonly="readonly"
:label="label" :label="label"