cubetiq-fusion/frontend/stores/app-store.ts
2021-07-25 10:44:30 +00:00

49 lines
1.2 KiB
TypeScript

import { RouterLocation } from '@vaadin/router';
import User from 'Frontend/generated/com/cubetiqs/fusion/data/entity/User';
import Role from 'Frontend/generated/com/cubetiqs/fusion/data/Role';
import { UserEndpoint } from 'Frontend/generated/UserEndpoint';
import { makeAutoObservable } from 'mobx';
export class AppStore {
applicationName = 'Fusion Management';
// The location, relative to the base path, e.g. "hello" when viewing "/hello"
location = '';
currentViewTitle = '';
user: User | undefined = undefined;
constructor() {
makeAutoObservable(this);
}
setLocation(location: RouterLocation) {
if (location.route) {
this.location = location.route.path;
} else if (location.pathname.startsWith(location.baseUrl)) {
this.location = location.pathname.substr(location.baseUrl.length);
} else {
this.location = location.pathname;
}
this.currentViewTitle = (location?.route as any)?.title || '';
}
async fetchUserInfo() {
this.user = await UserEndpoint.getAuthenticatedUser();
}
clearUserInfo() {
this.user = undefined;
}
get loggedIn() {
return !!this.user;
}
isUserInRole(role: Role) {
return this.user?.roles?.includes(role);
}
}
export const appStore = new AppStore();