From 251257ac80c4f33887ea7b2f7aa68827cb15773e Mon Sep 17 00:00:00 2001 From: Martin Rojas Miguel Angel Date: Fri, 20 Apr 2018 09:17:50 +0200 Subject: [PATCH] TSK-454 Move environment url to startup application instead of being a guard --- .../classification-details.component.ts | 4 +-- .../workbasket-list-toolbar.component.ts | 14 +++++----- web/src/app/app-routing.module.ts | 5 ++-- web/src/app/app.module.ts | 18 ++++++++++--- web/src/app/services/domain/domain.service.ts | 27 ++++++++++++++----- .../startup-service/startup.service.ts} | 13 +++++---- 6 files changed, 50 insertions(+), 31 deletions(-) rename web/src/app/{guards/environment-url-guard.ts => services/startup-service/startup.service.ts} (86%) diff --git a/web/src/app/administration/classification/details/classification-details.component.ts b/web/src/app/administration/classification/details/classification-details.component.ts index 2908d01ba..b781aba20 100644 --- a/web/src/app/administration/classification/details/classification-details.component.ts +++ b/web/src/app/administration/classification/details/classification-details.component.ts @@ -141,8 +141,9 @@ export class ClassificationDetailsComponent implements OnInit, OnDestroy { this.classificationSavingSubscription = this.classificationsService.postClassification(this.classification) .subscribe((classification: ClassificationDefinition) => { this.classification = classification; - this.afterRequest(); + this.classificationsService.selectClassification(classification.classificationId); this.alertService.triggerAlert(new AlertModel(AlertType.SUCCESS, `Classification ${classification.key} was saved successfully`)); + this.afterRequest(); }, error => { this.errorModalService.triggerError(new ErrorModel('There was an error creating a classification', error)) @@ -153,7 +154,6 @@ export class ClassificationDetailsComponent implements OnInit, OnDestroy { .putClassification(this.classification._links.self.href, this.classification) .subscribe((classification: ClassificationDefinition) => { this.classification = classification; - this.classificationsService.selectClassification(classification.classificationId); this.afterRequest(); this.alertService.triggerAlert(new AlertModel(AlertType.SUCCESS, `Classification ${classification.key} was saved successfully`)); }, error => { diff --git a/web/src/app/administration/workbasket/master/list/workbasket-list-toolbar/workbasket-list-toolbar.component.ts b/web/src/app/administration/workbasket/master/list/workbasket-list-toolbar/workbasket-list-toolbar.component.ts index 913560d01..bb6ad6a8b 100644 --- a/web/src/app/administration/workbasket/master/list/workbasket-list-toolbar/workbasket-list-toolbar.component.ts +++ b/web/src/app/administration/workbasket/master/list/workbasket-list-toolbar/workbasket-list-toolbar.component.ts @@ -19,16 +19,14 @@ import { ImportType } from 'app/models/import-type'; selector: 'taskana-workbasket-list-toolbar', animations: [ trigger('toggle', [ - state('*', style({ opacity: '1' })), - state('void', style({ opacity: '0' })), transition('void => *', animate('300ms ease-in', keyframes([ - style({ opacity: 0, height: '0px' }), - style({ opacity: 0.5, height: '50px' }), - style({ opacity: 1, height: '*' })]))), + style({ height: '0px' }), + style({ height: '50px' }), + style({ height: '*' })]))), transition('* => void', animate('300ms ease-out', keyframes([ - style({ opacity: 1, height: '*' }), - style({ opacity: 0.5, height: '50px' }), - style({ opacity: 0, height: '0px' })]))) + style({ height: '*' }), + style({ height: '50px' }), + style({ height: '0px' })]))) ] )], templateUrl: './workbasket-list-toolbar.component.html', diff --git a/web/src/app/app-routing.module.ts b/web/src/app/app-routing.module.ts index 3ca49d81c..a2fa07d93 100644 --- a/web/src/app/app-routing.module.ts +++ b/web/src/app/app-routing.module.ts @@ -8,14 +8,13 @@ import { MasterAndDetailComponent } from './shared/master-and-detail/master-and- import { NoAccessComponent } from './administration/workbasket/details/noAccess/no-access.component'; import { ClassificationListComponent } from './administration/classification/master/list/classification-list.component'; import { ClassificationDetailsComponent } from 'app/administration/classification/details/classification-details.component'; -import { EnvironmentUrlGuard } from 'app/guards/environment-url-guard'; import { DomainGuard } from 'app/guards/domain-guard'; const appRoutes: Routes = [ { path: 'administration/workbaskets', component: MasterAndDetailComponent, - canActivate: [EnvironmentUrlGuard, DomainGuard], + canActivate: [DomainGuard], children: [ { path: '', @@ -42,7 +41,7 @@ const appRoutes: Routes = [ { path: 'administration/classifications', component: MasterAndDetailComponent, - canActivate: [EnvironmentUrlGuard, DomainGuard], + canActivate: [DomainGuard], children: [ { path: '', diff --git a/web/src/app/app.module.ts b/web/src/app/app.module.ts index 4fd2d8a3f..64a6243cc 100644 --- a/web/src/app/app.module.ts +++ b/web/src/app/app.module.ts @@ -3,7 +3,7 @@ * Modules */ import { BrowserModule } from '@angular/platform-browser'; -import { NgModule } from '@angular/core'; +import { NgModule, APP_INITIALIZER } from '@angular/core'; import { FormsModule, ReactiveFormsModule } from '@angular/forms'; import { HTTP_INTERCEPTORS, HttpClientModule } from '@angular/common/http'; import { AppRoutingModule } from './app-routing.module'; @@ -58,6 +58,7 @@ import { ClassificationsService } from './services/classifications/classificatio import { TreeService } from './services/tree/tree.service'; import { ClassificationTypesService } from './services/classification-types/classification-types.service'; import { ClassificationCategoriesService } from 'app/services/classification-categories-service/classification-categories.service'; +import { StartupService } from 'app/services/startup-service/startup.service'; /** @@ -72,7 +73,6 @@ import { DomainService } from './services/domain/domain.service'; /** * Guards */ -import { EnvironmentUrlGuard } from './guards/environment-url-guard'; import { DomainGuard } from './guards/domain-guard'; const MODULES = [ @@ -118,6 +118,10 @@ const DECLARATIONS = [ SpreadNumberPipe ]; +export function startupServiceFactory(startupService: StartupService): Function { + return () => startupService.load(); +} + @NgModule({ declarations: DECLARATIONS, imports: MODULES, @@ -143,8 +147,14 @@ const DECLARATIONS = [ TreeService, ClassificationTypesService, ClassificationCategoriesService, - EnvironmentUrlGuard, - DomainGuard + DomainGuard, + StartupService, + { + provide: APP_INITIALIZER, + useFactory: startupServiceFactory, + deps: [StartupService], + multi: true + } ], bootstrap: [AppComponent] }) diff --git a/web/src/app/services/domain/domain.service.ts b/web/src/app/services/domain/domain.service.ts index 01c8b90a6..0be3fc9b0 100644 --- a/web/src/app/services/domain/domain.service.ts +++ b/web/src/app/services/domain/domain.service.ts @@ -6,6 +6,7 @@ import { Router } from '@angular/router'; import { BehaviorSubject } from 'rxjs/BehaviorSubject'; import { RequestInProgressService } from '../requestInProgress/request-in-progress.service'; import { Subject } from 'rxjs/Subject'; +import { ReplaySubject } from 'rxjs/ReplaySubject'; @Injectable() export class DomainService { @@ -21,6 +22,7 @@ export class DomainService { private domainSelectedValue; private domainSelected = new BehaviorSubject(''); private domainSwitched = new Subject(); + private dataObs$ = new ReplaySubject>(1); constructor( private httpClient: HttpClient, @@ -29,13 +31,24 @@ export class DomainService { } // GET - getDomains(): Observable { - return this.httpClient.get(this.url, this.httpOptions).do(domains => { - if (!this.domainSelectedValue && domains && domains.length > 0) { - this.domainSelectedValue = domains[0]; - this.selectDomain(domains[0]); - } - }); + getDomains(forceRefresh = false): Observable { + if (!this.dataObs$.observers.length || forceRefresh) { + this.httpClient.get(this.url, this.httpOptions).subscribe( + domains => { + this.dataObs$.next(domains); + if (!this.domainSelectedValue && domains && domains.length > 0) { + this.domainSelectedValue = domains[0]; + this.selectDomain(domains[0]); + } + }, + error => { + this.dataObs$.error(error); + this.dataObs$ = new ReplaySubject(1); + } + ); + } + + return this.dataObs$; } getSelectedDomain(): Observable { diff --git a/web/src/app/guards/environment-url-guard.ts b/web/src/app/services/startup-service/startup.service.ts similarity index 86% rename from web/src/app/guards/environment-url-guard.ts rename to web/src/app/services/startup-service/startup.service.ts index 92074c15b..af8fca0a2 100644 --- a/web/src/app/guards/environment-url-guard.ts +++ b/web/src/app/services/startup-service/startup.service.ts @@ -5,10 +5,9 @@ import { environment } from 'app/../environments/environment'; import { Injectable } from '@angular/core'; @Injectable() -export class EnvironmentUrlGuard implements CanActivate { +export class StartupService { constructor(private httpClient: HttpClient) { } - - canActivate() { + load(): Promise { return this.httpClient.get('environments/data-sources/environment-information.json').map(jsonFile => { if (jsonFile) { environment.taskanaWorkplaceUrl = jsonFile.taskanaWorkplaceUrl === '' ? @@ -20,9 +19,9 @@ export class EnvironmentUrlGuard implements CanActivate { environment.taskanaRestUrl = jsonFile.taskanaRestUrl === '' ? environment.taskanaRestUrl : jsonFile.taskanaRestUrl; } - return true; - }).catch(() => { - return Observable.of(true) - }); + }).toPromise() + .catch(() => { + return Observable.of(true) + }) } }