diff --git a/frontend/app/components/wp-create/wp-create.controller.ts b/frontend/app/components/wp-create/wp-create.controller.ts index 40fea8d3d7..bdc7ad939e 100644 --- a/frontend/app/components/wp-create/wp-create.controller.ts +++ b/frontend/app/components/wp-create/wp-create.controller.ts @@ -32,6 +32,7 @@ import {WorkPackageResource} from "../api/api-v3/hal-resources/work-package-reso import {WorkPackageCacheService} from "../work-packages/work-package-cache.service"; import {scopedObservable} from "../../helpers/angular-rx-utils"; import IRootScopeService = angular.IRootScopeService; +import {WorkPackageEditModeStateService} from "../wp-edit/wp-edit-mode-state.service"; export class WorkPackageCreateController { public newWorkPackage:WorkPackageResource|any; @@ -53,11 +54,13 @@ export class WorkPackageCreateController { protected NotificationsService, protected loadingIndicator, protected wpCreate:WorkPackageCreateService, + protected wpEditModeState:WorkPackageEditModeStateService, protected wpCacheService:WorkPackageCacheService) { + scopedObservable($scope, wpCreate.createNewWorkPackage($state.params.projectPath)) .subscribe(wp => { this.newWorkPackage = wp; - wpCacheService.updateWorkPackage(wp); + this.wpEditModeState.start(); if ($state.params.parent_id) { scopedObservable($scope, wpCacheService.loadWorkPackage($state.params.parent_id)) @@ -73,14 +76,24 @@ export class WorkPackageCreateController { this.$state.go('work-packages.list', this.$state.params); } - public saveWorkPackage(successState:string) { - this.wpCreate.saveWorkPackage().then(wp => { - this.loadingIndicator.mainPage = this.$state.go(successState, {workPackageId: wp.id}) - .then(() => { - this.$rootScope.$emit('workPackagesRefreshInBackground'); - this.notifySuccess(); - }); - }); + public saveWorkPackage(successState:string): ng.IPromise { + if (this.wpEditModeState.active) { + return this.wpEditModeState.save().then(wp => { + this.newWorkPackage = null; + this.refreshAfterSave(wp, successState); + return wp; + }); + } + + return this.$q.reject(); + } + + private refreshAfterSave(wp, successState) { + this.loadingIndicator.mainPage = this.$state.go(successState, {workPackageId: wp.id}) + .then(() => { + this.$rootScope.$emit('workPackagesRefreshInBackground'); + this.notifySuccess(); + }); } private notifySuccess() { diff --git a/frontend/app/components/wp-create/wp-create.service.ts b/frontend/app/components/wp-create/wp-create.service.ts index dd6c9299f8..dea2bd2294 100644 --- a/frontend/app/components/wp-create/wp-create.service.ts +++ b/frontend/app/components/wp-create/wp-create.service.ts @@ -34,52 +34,27 @@ import {WorkPackageEditModeStateService} from "../wp-edit/wp-edit-mode-state.ser import {WorkPackageCacheService} from "../work-packages/work-package-cache.service"; export class WorkPackageCreateService { - protected form:HalResource; - - private _newWorkPackage:ng.IPromise; - - constructor(protected $q:ng.IQService, - protected WorkPackageResource:typeof WorkPackageResource, - protected wpEditModeState:WorkPackageEditModeStateService, - protected wpCacheService:WorkPackageCacheService, - protected apiWorkPackages:ApiWorkPackagesService) { + protected form: HalResource; + + constructor(protected $q: ng.IQService, + protected WorkPackageResource: typeof WorkPackageResource, + protected wpCacheService: WorkPackageCacheService, + protected apiWorkPackages: ApiWorkPackagesService) { } public createNewWorkPackage(projectIdentifier) { - if (!this._newWorkPackage) { - this._newWorkPackage = this.getForm(projectIdentifier).then(form => { - var wp = this.WorkPackageResource.fromCreateForm(form); - - this.wpCacheService.updateWorkPackage(wp); - this.wpEditModeState.start(); + var wp = this.getForm(projectIdentifier).then(form => { + var wp = this.WorkPackageResource.fromCreateForm(form); - return wp; - }); - } + this.wpCacheService.updateWorkPackage(wp); - return Rx.Observable.fromPromise(this._newWorkPackage); - } - - public saveWorkPackage() { - const deferred = this.$q.defer(); - - this._newWorkPackage.then(() => { - if (this.wpEditModeState.active) { - this.wpEditModeState.save().then(wp => { - this._newWorkPackage = null; - - deferred.resolve(wp); - }); - } - else { - deferred.reject(); - } + return wp; }); - - return deferred.promise; + + return Rx.Observable.fromPromise(wp); } - private getForm(projectIdentifier):ng.IPromise { + private getForm(projectIdentifier): ng.IPromise { if (!this.form) { this.form = this.apiWorkPackages.emptyCreateForm(projectIdentifier); } diff --git a/frontend/app/components/wp-edit/wp-edit-mode-state.service.ts b/frontend/app/components/wp-edit/wp-edit-mode-state.service.ts index 2bcc4094ad..245b560588 100644 --- a/frontend/app/components/wp-edit/wp-edit-mode-state.service.ts +++ b/frontend/app/components/wp-edit/wp-edit-mode-state.service.ts @@ -34,7 +34,7 @@ export class WorkPackageEditModeStateService { private _active: boolean = false; - constructor(protected $rootScope, protected $window, protected I18n) { + constructor(protected $rootScope, protected $window, protected $q, protected I18n) { $rootScope.$on('$stateChangeStart', function (event, toState, toParams, fromState, fromParams) { if (this.form && fromParams.workPackageId @@ -73,6 +73,8 @@ export class WorkPackageEditModeStateService { return wp; }); } + + return this.$q.reject(); } public register(form: WorkPackageEditFormController) {