OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
Prevent double 409 conflict form errors after saving the work package
Oliver Günther740d1e966f5d
diff --git a/frontend/src/app/components/work-packages/wp-single-view/wp-single-view.component.ts b/frontend/src/app/components/work-packages/wp-single-view/wp-single-view.component.tsindex 04126d5256e..1ccd66e1a96 100644--- a/frontend/src/app/components/work-packages/wp-single-view/wp-single-view.component.ts+++ b/frontend/src/app/components/work-packages/wp-single-view/wp-single-view.component.ts@@ -156,7 +156,7 @@ export class WorkPackageSingleViewComponent extends UntilDestroyedMixin implementhis.$element = jQuery(this.elementRef.nativeElement);const change = this.halEditing.changeFor<WorkPackageResource, WorkPackageChangeset>(this.workPackage);- this.resourceContextChange.next(this.contextFrom(change));+ this.resourceContextChange.next(this.contextFrom(change.projectedResource));this.refresh(change);// Whenever the resource context changes in any way,@@ -172,13 +172,13 @@ export class WorkPackageSingleViewComponent extends UntilDestroyedMixin implemen// Update the resource context on every update to the temporary resource.// This allows detecting a changed type value in a new work package.this.halEditing- .typedState<WorkPackageResource, WorkPackageChangeset>(this.workPackage)+ .temporaryEditResource(this.workPackage).values$().pipe(this.untilDestroyed())- .subscribe((change:WorkPackageChangeset) => {- this.resourceContextChange.next(this.contextFrom(change));+ .subscribe(resource => {+ this.resourceContextChange.next(this.contextFrom(resource));});}@@ -368,12 +368,11 @@ export class WorkPackageSingleViewComponent extends UntilDestroyedMixin implemen* Used to identify changes in the schema or project that may result in visual changes* to the single view.*- * @param {WorkPackageChangeset} change+ * @param {WorkPackage} workPackage* @returns {SchemaContext}*/- private contextFrom(change:WorkPackageChangeset):ResourceContextChange {- let schema = change.schema;- let workPackage = change.projectedResource;+ private contextFrom(workPackage:WorkPackageResource):ResourceContextChange {+ let schema = workPackage.schema;let schemaHref:string|null = null;let projectHref:string|null = workPackage.project && workPackage.project.href;diff --git a/frontend/src/app/components/wp-buttons/wp-status-button/wp-status-button.component.ts b/frontend/src/app/components/wp-buttons/wp-status-button/wp-status-button.component.tsindex ac2d64b9766..1eebd796b74 100644--- a/frontend/src/app/components/wp-buttons/wp-status-button/wp-status-button.component.ts+++ b/frontend/src/app/components/wp-buttons/wp-status-button/wp-status-button.component.ts@@ -35,6 +35,8 @@ import {Highlighting} from "core-components/wp-fast-table/builders/highlighting/import {HalResource} from "core-app/modules/hal/resources/hal-resource";import {WorkPackageCacheService} from "core-components/work-packages/work-package-cache.service";import {UntilDestroyedMixin} from "core-app/helpers/angular/until-destroyed.mixin";+import {concatAll} from "rxjs/operators";+import {combineLatest} from "rxjs";@Component({selector: 'wp-status-button',@@ -67,18 +69,19 @@ export class WorkPackageStatusButtonComponent extends UntilDestroyedMixin implem).subscribe((wp) => {this.workPackage = wp;- this.cdRef.detectChanges();if (this.workPackage.status) {this.workPackage.status.$load();}++ this.cdRef.detectChanges();});}public get buttonTitle() {if (this.workPackage.isReadonly) {return this.text.workPackageReadOnly;- } else if (this.workPackage.isEditable && this.isDisabled) {+ } else if (this.workPackage.isEditable && !this.allowed) {return this.text.workPackageStatusBlocked;} else {return '';@@ -93,25 +96,11 @@ export class WorkPackageStatusButtonComponent extends UntilDestroyedMixin implemreturn Highlighting.backgroundClass('status', status.id!);}- public get status():HalResource|undefined {- if (!this.halEditing) {- return;- }-- return this.changeset.projectedResource.status;+ public get status():HalResource {+ return this.workPackage.status;}public get allowed() {return this.workPackage.isAttributeEditable('status');}-- public get isDisabled() {- let writable = this.changeset.isWritable('status');-- return !this.allowed || !writable || this.changeset.inFlight;- }-- private get changeset() {- return this.halEditing.changeFor(this.workPackage);- }}diff --git a/frontend/src/app/components/wp-buttons/wp-status-button/wp-status-button.html b/frontend/src/app/components/wp-buttons/wp-status-button/wp-status-button.htmlindex 53d4d8de08a..c4f720560a9 100644--- a/frontend/src/app/components/wp-buttons/wp-status-button/wp-status-button.html+++ b/frontend/src/app/components/wp-buttons/wp-status-button/wp-status-button.html@@ -4,7 +4,7 @@*ngIf="status"><button class="button"[ngClass]="statusHighlightClass"- [disabled]="isDisabled"+ [disabled]="!allowed"[attr.aria-label]="text.explanation"wpStatusDropdown[wpStatusDropdown-workPackage]="workPackage">diff --git a/frontend/src/app/components/wp-edit-form/table-edit-form.ts b/frontend/src/app/components/wp-edit-form/table-edit-form.tsindex e47660c8473..092db0663c0 100644--- a/frontend/src/app/components/wp-edit-form/table-edit-form.ts+++ b/frontend/src/app/components/wp-edit-form/table-edit-form.ts@@ -70,7 +70,6 @@ export class TableEditForm extends EditForm<WorkPackageResource> {destroy() {this.resourceSubscription.unsubscribe();- super.destroy();}public findContainer(fieldName:string):JQuery {diff --git a/frontend/src/app/components/wp-new/wp-create.component.ts b/frontend/src/app/components/wp-new/wp-create.component.tsindex 91316028efc..13fbfb3c0fc 100644--- a/frontend/src/app/components/wp-new/wp-create.component.ts+++ b/frontend/src/app/components/wp-new/wp-create.component.ts@@ -92,7 +92,6 @@ export class WorkPackageCreateComponent extends UntilDestroyedMixin implements Opublic ngOnDestroy() {super.ngOnDestroy();- this.editForm.destroy();}public switchToFullscreen() {@@ -102,7 +101,7 @@ export class WorkPackageCreateComponent extends UntilDestroyedMixin implements Opublic onSaved(params:{ savedResource:WorkPackageResource, isInitial:boolean }) {let { savedResource, isInitial } = params;- this.editForm?.stop();+ this.editForm?.cancel(false);if (this.successState) {this.$state.go(this.successState, { workPackageId: savedResource.id })diff --git a/frontend/src/app/modules/common/edit-actions-bar/wp-edit-actions-bar.component.ts b/frontend/src/app/modules/common/edit-actions-bar/wp-edit-actions-bar.component.tsindex 34e3a3eafb3..c47b39bc676 100644--- a/frontend/src/app/modules/common/edit-actions-bar/wp-edit-actions-bar.component.ts+++ b/frontend/src/app/modules/common/edit-actions-bar/wp-edit-actions-bar.component.ts@@ -66,7 +66,7 @@ export class WorkPackageEditActionsBarComponent {this.saving = true;this.editForm- .save()+ .submit().then(() => {this.saving = false;this.onSave.emit();@@ -77,7 +77,7 @@ export class WorkPackageEditActionsBarComponent {}public cancel():void {- this.editForm.stop();+ this.editForm.cancel();this.onCancel.emit();}}diff --git a/frontend/src/app/modules/fields/edit/edit-form/edit-form.component.ts b/frontend/src/app/modules/fields/edit/edit-form/edit-form.component.tsindex cc9fe99995a..dad1571bf1b 100644--- a/frontend/src/app/modules/fields/edit/edit-form/edit-form.component.ts+++ b/frontend/src/app/modules/fields/edit/edit-form/edit-form.component.ts@@ -43,6 +43,7 @@ import {IFieldSchema} from "core-app/modules/fields/field.base";import {EditFieldHandler} from "core-app/modules/fields/edit/editing-portal/edit-field-handler";import {EditingPortalService} from "core-app/modules/fields/edit/editing-portal/editing-portal-service";import {EditFormRoutingService} from "core-app/modules/fields/edit/edit-form/edit-form-routing.service";+import {ResourceChangesetCommit} from "core-app/modules/fields/edit/services/hal-resource-editing.service";@Component({selector: 'edit-form,[edit-form]',@@ -84,7 +85,7 @@ export class EditFormComponent extends EditForm<HalResource> implements OnInit,return false;}- this.stop();+ this.cancel(false);}return true;@@ -92,7 +93,7 @@ export class EditFormComponent extends EditForm<HalResource> implements OnInit,}ngOnDestroy() {- this.destroy();+ this.unregisterListener();}ngOnInit() {@@ -103,11 +104,6 @@ export class EditFormComponent extends EditForm<HalResource> implements OnInit,}}- destroy() {- this.unregisterListener();- super.destroy();- }-public async activateField(form:EditForm, schema:IFieldSchema, fieldName:string, errors:string[]):Promise<EditFieldHandler> {return this.waitForField(fieldName).then((ctrl) => {ctrl.setActive(true);@@ -129,9 +125,18 @@ export class EditFormComponent extends EditForm<HalResource> implements OnInit,ctrl.deactivate(focus);}- public onSaved(isInitial:boolean, saved:HalResource) {- super.onSaved(isInitial, saved);- this.stopEditingAndLeave(saved, isInitial);+ public onSaved(commit:ResourceChangesetCommit) {+ this.cancel(false);+ this.onSavedEmitter.emit({savedResource: commit.resource, isInitial: commit.wasNew });+ }++ public cancel(reset:boolean = false) {+ this.editMode = false;+ this.closeEditFields('all', reset);++ if (reset) {+ this.halEditing.reset(this.change)+ }}public requireVisible(fieldName:string):Promise<void> {@@ -178,27 +183,6 @@ export class EditFormComponent extends EditForm<HalResource> implements OnInit,_.each(this.fields, ctrl => this.activate(ctrl.fieldName));}- public stop() {- this.editMode = false;- this.closeEditFields();- this.halEditing.stopEditing(this.resource);- this.destroy();- }-- public save() {- const isInitial = this.resource.isNew;- return this- .submit()- .then((savedResource:HalResource) => {- this.stopEditingAndLeave(savedResource, isInitial);- });- }-- public stopEditingAndLeave(savedResource:HalResource, isInitial:boolean) {- this.stop();- this.onSavedEmitter.emit({savedResource, isInitial});- }-protected focusOnFirstError():void {// Focus the first field that is erroneousjQuery(this.elementRef.nativeElement)diff --git a/frontend/src/app/modules/fields/edit/edit-form/edit-form.ts b/frontend/src/app/modules/fields/edit/edit-form/edit-form.tsindex 17b040d7e46..7273352cb06 100644--- a/frontend/src/app/modules/fields/edit/edit-form/edit-form.ts+++ b/frontend/src/app/modules/fields/edit/edit-form/edit-form.ts@@ -32,7 +32,10 @@ import {Subscription} from 'rxjs';import {States} from 'core-components/states.service';import {IFieldSchema} from "core-app/modules/fields/field.base";-import {HalResourceEditingService} from "core-app/modules/fields/edit/services/hal-resource-editing.service";+import {+ HalResourceEditingService,+ ResourceChangesetCommit+} from "core-app/modules/fields/edit/services/hal-resource-editing.service";import {HalEventsService} from "core-app/modules/hal/services/hal-events.service";import {EditFieldHandler} from "core-app/modules/fields/edit/editing-portal/edit-field-handler";import {HalResource} from "core-app/modules/hal/resources/hal-resource";@@ -63,9 +66,6 @@ export abstract class EditForm<T extends HalResource = HalResource> {// Whether this form exists in edit modepublic editMode:boolean = false;- // Subscribe to changes to the temporary edit form- protected subscription:Subscription;-protected constructor(public injector:Injector) {}@@ -87,9 +87,8 @@ export abstract class EditForm<T extends HalResource = HalResource> {/*** Optional callback when the form is being saved*/- protected onSaved(isInitial:boolean, saved:HalResource):void {- const eventType = isInitial ? 'created' : 'updated';- this.halEvents.push(saved, { eventType });+ protected onSaved(commit:ResourceChangesetCommit):void {+ // Does nothing by default}protected abstract focusOnFirstError():void;@@ -190,7 +189,7 @@ export abstract class EditForm<T extends HalResource = HalResource> {this.halNotification.showSave(result.resource, result.wasNew);this.editMode = false;- this.onSaved(result.wasNew, result.resource);+ this.onSaved(result);this.change.inFlight = false;}).catch((error:ErrorResource|Object) => {@@ -206,36 +205,24 @@ export abstract class EditForm<T extends HalResource = HalResource> {});}- /**- * Close all fields and unsubscribe the observers on this form.- */- public destroy() {- if (this.subscription) {- // Unsubscribe changes- this.subscription.unsubscribe();- }-- // Kill all active fields- // Without resetting the changeset, if, e.g., we're moving an active edit- _.each(this.activeFields, (handler) => {- handler && handler.deactivate(false);- });- }-/*** Close the given or all open fields.** @param {string[]} fields+ * @param resetChange whether to undo any changes made*/- public closeEditFields(fields?:string[]) {- if (!fields) {+ public closeEditFields(fields:string[]|'all' = 'all', resetChange:boolean = true) {+ if (fields === 'all') {fields = _.keys(this.activeFields);}fields.forEach((name:string) => {const handler = this.activeFields[name];handler && handler.deactivate(false);- this.change.reset(name);++ if (resetChange) {+ this.change.reset(name);+ }});}diff --git a/frontend/src/app/modules/fields/edit/services/hal-resource-editing.service.ts b/frontend/src/app/modules/fields/edit/services/hal-resource-editing.service.tsindex dd28444ca93..c22d4684c58 100644--- a/frontend/src/app/modules/fields/edit/services/hal-resource-editing.service.ts+++ b/frontend/src/app/modules/fields/edit/services/hal-resource-editing.service.ts@@ -27,7 +27,7 @@// ++import {combine, deriveRaw, InputState, multiInput, MultiInputState, State, StatesGroup} from 'reactivestates';-import {map} from 'rxjs/operators';+import {filter, map} from 'rxjs/operators';import {Injectable, Injector} from '@angular/core';import {Subject} from "rxjs";import {FormResource} from "core-app/modules/hal/resources/form-resource";@@ -36,6 +36,7 @@ import {ResourceChangeset} from "core-app/modules/fields/changeset/resource-chanimport {HalResource} from "core-app/modules/hal/resources/hal-resource";import {StateCacheService} from "core-components/states/state-cache.service";import {HookService} from "core-app/modules/plugins/hook-service";+import {HalEventsService} from "core-app/modules/hal/services/hal-events.service";class ChangesetStates extends StatesGroup {name = 'Changesets';@@ -98,6 +99,7 @@ export class HalResourceEditingService extends StateCacheService<ResourceChangesprivate stateGroup = new ChangesetStates();constructor(protected readonly injector:Injector,+ protected readonly halEvents:HalEventsService,protected readonly hook:HookService) {super();}@@ -110,7 +112,7 @@ export class HalResourceEditingService extends StateCacheService<ResourceChanges// Initialize any potentially new HAL valuessavedResource.retainFrom(change.pristineResource);- this.onSaved(savedResource);+ await this.onSaved(savedResource);// Complete the changereturn this.complete(change, savedResource);@@ -125,6 +127,9 @@ export class HalResourceEditingService extends StateCacheService<ResourceChangesthis.comittedChanges.next(commit);this.reset(change);+ const eventType = commit.wasNew ? 'created' : 'updated';+ this.halEvents.push(commit.resource, { eventType }) ;+return commit;}@@ -190,8 +195,12 @@ export class HalResourceEditingService extends StateCacheService<ResourceChangesif (changeset && !changeset.isEmpty()) {return changeset;}- if (!changeset ||- resource.hasOwnProperty('lockVersion') && changeset.pristineResource.lockVersion < resource.lockVersion) {++ if (!changeset) {+ return this.edit<V, T>(resource);+ }++ if (resource.hasOwnProperty('lockVersion') && changeset.pristineResource.lockVersion < resource.lockVersion) {return this.edit<V, T>(resource);}@@ -217,12 +226,14 @@ export class HalResourceEditingService extends StateCacheService<ResourceChangesreturn deriveRaw(combined,($) => $.pipe(+ filter(([resource, _]) => !!resource),map(([resource, change]) => {- if (resource && change && !change.isEmpty()) {- return change.projectedResource as V;- } else {- return resource;+ if (change) {+ change.pristineResource = resource as V;+ return change.projectedResource;}++ return resource;})));@@ -236,10 +247,12 @@ export class HalResourceEditingService extends StateCacheService<ResourceChangesreturn Promise.reject('Loading not applicable for changesets.') as any;}- protected onSaved(saved:HalResource) {+ protected onSaved(saved:HalResource):Promise<unknown> {if (saved.state) {- saved.push(saved);+ return saved.push(saved);}++ return Promise.resolve();}protected loadAll(hrefs:string[]) {diff --git a/frontend/src/app/modules/hal/resources/hal-resource.ts b/frontend/src/app/modules/hal/resources/hal-resource.tsindex 5ed4fb07727..b2ed2ea5d02 100644--- a/frontend/src/app/modules/hal/resources/hal-resource.ts+++ b/frontend/src/app/modules/hal/resources/hal-resource.ts@@ -235,10 +235,12 @@ export class HalResource {/*** Update the state*/- public push(newValue:this):void {+ public push(newValue:this):Promise<unknown> {if (this.state) {this.state.putValue(newValue);}++ return Promise.resolve();}public previewPath():string|undefined {diff --git a/frontend/src/app/modules/hal/resources/work-package-resource.ts b/frontend/src/app/modules/hal/resources/work-package-resource.tsindex e8b2d48210a..955e7211a51 100644--- a/frontend/src/app/modules/hal/resources/work-package-resource.ts+++ b/frontend/src/app/modules/hal/resources/work-package-resource.ts@@ -338,7 +338,7 @@ export class WorkPackageBaseResource extends HalResource {/*** Update the state*/- public push(newValue:this):void {+ public push(newValue:this):Promise<unknown> {this.wpActivity.clear(newValue.id!);// If there is a parent, its view has to be updated as well@@ -346,7 +346,7 @@ export class WorkPackageBaseResource extends HalResource {this.wpCacheService.require(newValue.parent.id!, true);}- this.wpCacheService.updateWorkPackage(newValue as any);+ return this.wpCacheService.updateWorkPackage(newValue as any);}public get hasOverriddenSchema():boolean {diff --git a/frontend/src/app/modules/time_entries/form/form.component.ts b/frontend/src/app/modules/time_entries/form/form.component.tsindex 269aafcddcb..370e8dbee98 100644--- a/frontend/src/app/modules/time_entries/form/form.component.ts+++ b/frontend/src/app/modules/time_entries/form/form.component.ts@@ -75,7 +75,7 @@ export class TimeEntryFormComponent extends UntilDestroyedMixin implements OnIni}public save() {- return this.editForm.save();+ return this.editForm.submit();}public get inEditMode() {diff --git a/spec/features/work_packages/table/switch_types_spec.rb b/spec/features/work_packages/table/switch_types_spec.rbindex 2107378a7ee..7fa61c306ce 100644--- a/spec/features/work_packages/table/switch_types_spec.rb+++ b/spec/features/work_packages/table/switch_types_spec.rb@@ -147,6 +147,7 @@ describe 'Switching types in work package table', js: true do# Now switch back to a type without the required CFtype_field.activate!+ type_field.openSelectFieldtype_field.set_value type_task.namewp_table.expect_notification(