Reading model defs only when user is authenticated
Change-Id: If9f41b8fac8671029280d327e70c5d2f410b9964
diff --git a/src/app/core/index.ts b/src/app/core/index.ts
index 53dfade..2df80c8 100644
--- a/src/app/core/index.ts
+++ b/src/app/core/index.ts
@@ -15,6 +15,7 @@
import 'angular-toastr';
import {xosAlert} from './alert/alert';
import {xosValidation} from './validation/validation';
+import {ModelSetup} from './services/helpers/model-setup.helpers';
export const xosCore = 'xosCore';
@@ -30,6 +31,7 @@
.service('PageTitle', PageTitle)
.service('XosFormHelpers', XosFormHelpers)
.service('ConfigHelpers', ConfigHelpers)
+ .service('ModelSetup', ModelSetup)
.directive('xosLinkWrapper', xosLinkWrapper)
.component('xosHeader', xosHeader)
.component('xosFooter', xosFooter)
diff --git a/src/app/core/login/login.ts b/src/app/core/login/login.ts
index 334ad08..c091e43 100644
--- a/src/app/core/login/login.ts
+++ b/src/app/core/login/login.ts
@@ -1,15 +1,17 @@
import {AuthService} from '../../datasources/rest/auth.rest';
import {StyleConfig} from '../../config/style.config';
import './login.scss';
+import {IXosModelSetupService} from '../services/helpers/model-setup.helpers';
class LoginCtrl {
- static $inject = ['AuthService', '$state'];
+ static $inject = ['AuthService', '$state', 'ModelSetup'];
public loginStyle: any;
public img: string;
/** @ngInject */
constructor(
private authService: AuthService,
- private $state: angular.ui.IStateService
+ private $state: angular.ui.IStateService,
+ private ModelSetup: IXosModelSetupService
) {
this.img = this.getImg(StyleConfig.background);
@@ -24,6 +26,10 @@
password: password
})
.then(res => {
+ // after login set up models
+ return this.ModelSetup.setup();
+ })
+ .then(() => {
this.$state.go('xos.dashboard');
})
.catch(e => console.error);
diff --git a/src/app/core/services/helpers/model-setup.helpers.ts b/src/app/core/services/helpers/model-setup.helpers.ts
new file mode 100644
index 0000000..6a276d9
--- /dev/null
+++ b/src/app/core/services/helpers/model-setup.helpers.ts
@@ -0,0 +1,62 @@
+import {ModeldefsService, IModeldef} from '../../../datasources/rest/modeldefs.rest';
+import {IXosConfigHelpersService} from './config.helpers';
+import {IRuntimeStatesService} from '../runtime-states';
+import {NavigationService} from '../navigation';
+import {IXosState} from '../../../../index';
+import * as _ from 'lodash';
+import IPromise = angular.IPromise;
+
+export interface IXosModelSetupService {
+ setup(): IPromise<null>;
+}
+
+export class ModelSetup {
+ static $inject = ['$q', 'ModelDefs', 'ConfigHelpers', 'RuntimeStates', 'NavigationService'];
+
+ constructor(
+ private $q: ng.IQService,
+ private ModelDefs: ModeldefsService,
+ private ConfigHelpers: IXosConfigHelpersService,
+ private RuntimeStates: IRuntimeStatesService,
+ private NavigationService: NavigationService
+ ) {
+
+ }
+
+ public setup(): IPromise<null> {
+ const d = this.$q.defer();
+ this.ModelDefs.get()
+ .then((models: IModeldef[]) => {
+ _.forEach(models, (m: IModeldef) => {
+ const stateUrl = `/${this.ConfigHelpers.pluralize(m.name.toLowerCase())}/:id?`;
+ const stateName = `xos.core.${this.ConfigHelpers.pluralize(m.name.toLowerCase())}`;
+ const state: IXosState = {
+ parent: 'core',
+ url: stateUrl,
+ component: 'xosCrud',
+ params: {
+ id: null
+ },
+ data: {
+ model: m.name,
+ related: m.relations,
+ xosTableCfg: this.ConfigHelpers.modelToTableCfg(m, stateUrl),
+ xosFormCfg: this.ConfigHelpers.modelToFormCfg(m)
+ }
+ };
+
+ this.RuntimeStates.addState(stateName, state);
+ this.NavigationService.add({
+ label: this.ConfigHelpers.pluralize(m.name),
+ state: stateName,
+ parent: 'xos.core'
+ });
+ });
+
+ d.resolve();
+ })
+ .catch(d.reject);
+
+ return d.promise;
+ }
+}
diff --git a/src/index.ts b/src/index.ts
index e925e08..916730e 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -20,16 +20,11 @@
interceptorConfig, userStatusInterceptor, CredentialsInterceptor,
NoHyperlinksInterceptor
} from './interceptors';
-import {IRuntimeStatesService} from './app/core/services/runtime-states';
-import {IModeldefsService, IModeldef} from './app/datasources/rest/modeldefs.rest';
import {IXosCrudData} from './app/views/crud/crud';
-import * as _ from 'lodash';
-import {IXosNavigationService} from './app/core/services/navigation';
import {IXosPageTitleService} from './app/core/services/page-title';
-import {IXosConfigHelpersService} from './app/core/services/helpers/config.helpers';
import {StyleConfig} from './app/config/style.config';
-import {IXosResourceService} from './app/datasources/rest/model.rest';
import {IXosAuthService} from './app/datasources/rest/auth.rest';
+import {IXosModelSetupService} from './app/core/services/helpers/model-setup.helpers';
export interface IXosState extends angular.ui.IState {
data: IXosCrudData;
@@ -62,15 +57,11 @@
});
})
.run((
+ $transitions: any,
$location: ng.ILocationService,
$state: ng.ui.IStateService,
- ModelDefs: IModeldefsService,
- ModelRest: IXosResourceService,
- RuntimeStates: IRuntimeStatesService,
- NavigationService: IXosNavigationService,
- ConfigHelpers: IXosConfigHelpersService,
+ ModelSetup: IXosModelSetupService,
AuthService: IXosAuthService,
- $transitions: any,
toastr: ng.toastr.IToastrService,
PageTitle: IXosPageTitleService
) => {
@@ -85,39 +76,14 @@
// save the last visited state before reload
const lastRoute = window.location.hash.replace('#', '');
- // TODO this must be triggered only when user is authenticated
- // Dinamically add a core states
- ModelDefs.get()
- .then((models: IModeldef[]) => {
- // TODO move in a separate service and test (StateConfig service in Core)
- _.forEach(models, (m: IModeldef) => {
- const stateUrl = `/${ConfigHelpers.pluralize(m.name.toLowerCase())}/:id?`;
- const stateName = `xos.core.${ConfigHelpers.pluralize(m.name.toLowerCase())}`;
- const state: IXosState = {
- parent: 'core',
- url: stateUrl,
- component: 'xosCrud',
- params: {
- id: null
- },
- data: {
- model: m.name,
- related: m.relations,
- xosTableCfg: ConfigHelpers.modelToTableCfg(m, stateUrl),
- xosFormCfg: ConfigHelpers.modelToFormCfg(m)
- }
- };
-
- RuntimeStates.addState(stateName, state);
- NavigationService.add({
- label: ConfigHelpers.pluralize(m.name),
- state: stateName,
- parent: 'xos.core'
- });
+ // if the user is authenticated
+ if (AuthService.getUser()) {
+ ModelSetup.setup()
+ .then(() => {
+ // after setting up dynamic routes, redirect to previous state
+ $location.path(lastRoute);
});
+ }
- // after setting up dynamic routes, redirect to previous state
- $location.path(lastRoute);
- });
});