2021-07-25 20:04:59 +07:00
|
|
|
import { Flow } from "@vaadin/flow-frontend";
|
2021-07-25 17:44:30 +07:00
|
|
|
import { Route } from '@vaadin/router';
|
2021-07-25 18:16:45 +07:00
|
|
|
import Role from './generated/com/cubetiqs/fusion/data/Role';
|
2021-07-25 17:44:30 +07:00
|
|
|
import { appStore } from './stores/app-store';
|
|
|
|
import './views/home/home-view';
|
|
|
|
import './views/main-layout';
|
|
|
|
|
2021-07-25 20:04:59 +07:00
|
|
|
const { serverSideRoutes } = new Flow({
|
|
|
|
imports: () => import('../target/frontend/generated-flow-imports'),
|
|
|
|
});
|
|
|
|
|
2021-07-27 09:53:06 +07:00
|
|
|
|
|
|
|
console.log("serverSideRoutes", serverSideRoutes)
|
|
|
|
|
2021-07-25 17:44:30 +07:00
|
|
|
export type ViewRoute = Route & {
|
|
|
|
title?: string;
|
|
|
|
icon?: string;
|
|
|
|
requiresLogin?: boolean;
|
|
|
|
rolesAllowed?: Role[];
|
|
|
|
children?: ViewRoute[];
|
|
|
|
};
|
|
|
|
|
|
|
|
export const hasAccess = (route: Route) => {
|
|
|
|
const viewRoute = route as ViewRoute;
|
|
|
|
if (viewRoute.requiresLogin && !appStore.loggedIn) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (viewRoute.rolesAllowed) {
|
|
|
|
return viewRoute.rolesAllowed.some((role) => appStore.isUserInRole(role));
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const views: ViewRoute[] = [
|
|
|
|
// place routes below (more info https://vaadin.com/docs/latest/fusion/routing/overview)
|
|
|
|
{
|
|
|
|
path: '',
|
|
|
|
component: 'home-view',
|
|
|
|
icon: 'la la-home',
|
|
|
|
title: 'Home',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
path: 'profile',
|
|
|
|
component: 'profile-view',
|
|
|
|
requiresLogin: true,
|
|
|
|
icon: 'la la-user',
|
|
|
|
title: 'Profile',
|
|
|
|
action: async (_context, _command) => {
|
|
|
|
if (!hasAccess(_context.route)) {
|
|
|
|
return _command.redirect('login');
|
|
|
|
}
|
|
|
|
await import('./views/profile/profile-view');
|
|
|
|
return;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
path: 'admin',
|
|
|
|
component: 'admin-view',
|
|
|
|
rolesAllowed: [Role.ADMIN],
|
|
|
|
icon: 'la la-user-lock',
|
|
|
|
title: 'Admin',
|
|
|
|
action: async (_context, _command) => {
|
|
|
|
if (!hasAccess(_context.route)) {
|
|
|
|
return _command.redirect('login');
|
|
|
|
}
|
|
|
|
await import('./views/admin/admin-view');
|
|
|
|
return;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
];
|
|
|
|
export const routes: ViewRoute[] = [
|
|
|
|
{
|
|
|
|
path: 'login',
|
|
|
|
component: 'login-view',
|
|
|
|
icon: '',
|
|
|
|
title: 'Login',
|
|
|
|
action: async (_context, _command) => {
|
|
|
|
await import('./views/login/login-view');
|
|
|
|
return;
|
|
|
|
},
|
|
|
|
},
|
2021-07-27 09:53:06 +07:00
|
|
|
{
|
|
|
|
path: '',
|
|
|
|
component: 'main-layout',
|
|
|
|
children: [
|
|
|
|
...views,
|
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
path: '',
|
|
|
|
component: 'server-layout',
|
|
|
|
children: [
|
|
|
|
...serverSideRoutes,
|
|
|
|
]
|
|
|
|
}
|
2021-07-25 17:44:30 +07:00
|
|
|
];
|