Skip to content

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

Avoid requesting the form more than once per changeset instance

Oliver Günther318917a3f13e
diff --git a/frontend/src/app/components/wp-edit/work-package-changeset.ts b/frontend/src/app/components/wp-edit/work-package-changeset.ts
index e758b237a40..9060a9f210c 100644
--- a/frontend/src/app/components/wp-edit/work-package-changeset.ts
+++ b/frontend/src/app/components/wp-edit/work-package-changeset.ts
@@ -10,7 +10,7 @@ export class WorkPackageChangeset extends ResourceChangeset<WorkPackageResource>
// when the work package is new. Otherwise, the save request afterwards
// will update the form automatically.
if (this.pristineResource.isNew && (key === 'project' || key === 'type')) {
- this.updateForm();
+ this.form$.clear(`${key} changed in a new work package`);
}
}
diff --git a/frontend/src/app/modules/fields/changeset/resource-changeset.ts b/frontend/src/app/modules/fields/changeset/resource-changeset.ts
index c885a4aa2c6..c953ffa2b80 100644
--- a/frontend/src/app/modules/fields/changeset/resource-changeset.ts
+++ b/frontend/src/app/modules/fields/changeset/resource-changeset.ts
@@ -2,9 +2,10 @@ import {SchemaResource} from "core-app/modules/hal/resources/schema-resource";
import {FormResource} from "core-app/modules/hal/resources/form-resource";
import {HalResource} from "core-app/modules/hal/resources/hal-resource";
import {ChangeMap, Changeset} from "core-app/modules/fields/changeset/changeset";
-import {InputState} from "reactivestates";
+import {input, InputState} from "reactivestates";
import {IFieldSchema} from "core-app/modules/fields/field.base";
import {debugLog} from "core-app/helpers/debug_output";
+import {take} from "rxjs/operators";
/**
* Temporary class living while a resource is being edited
@@ -21,7 +22,7 @@ export class ResourceChangeset<T extends HalResource|{ [key:string]:unknown; } =
protected changeset = new Changeset();
/** Reference and load promise for the current form */
- private formPromise:Promise<FormResource>|null;
+ protected form$ = input<FormResource>();
/** Flag whether this is currently being saved */
public inFlight = false;
@@ -40,7 +41,11 @@ export class ResourceChangeset<T extends HalResource|{ [key:string]:unknown; } =
constructor(public pristineResource:T,
public readonly state?:InputState<ResourceChangeset<T>>,
- public form:FormResource|null = null) {
+ loadedForm:FormResource|null = null) {
+
+ if (loadedForm) {
+ this.form$.putValue(loadedForm);
+ }
}
/**
@@ -56,24 +61,25 @@ export class ResourceChangeset<T extends HalResource|{ [key:string]:unknown; } =
/**
* Build the request attributes against the fresh form
*/
- public buildRequestPayload():Promise<[FormResource, Object]> {
+ public buildRequestPayload():Promise<Object> {
return this
- .updateForm()
- .then(form => [form, this.buildPayloadFromChanges()]) as Promise<[FormResource, Object]>;
+ .getForm()
+ .then(() => this.buildPayloadFromChanges());
}
-
/**
* Returns the current work package form.
* This may be different from the base form when project or type is changed.
*/
public getForm():Promise<FormResource> {
- if (!this.form) {
- return this.updateForm();
- } else {
- return Promise.resolve(this.form);
- }
+ this.form$.putFromPromiseIfPristine(() => this.updateForm());
+
+ return this
+ .form$
+ .values$()
+ .pipe(take(1))
+ .toPromise();
}
public getSchemaName(attribute:string):string {
@@ -90,24 +96,13 @@ export class ResourceChangeset<T extends HalResource|{ [key:string]:unknown; } =
protected updateForm():Promise<FormResource> {
let payload = this.buildPayloadFromChanges();
- if (!this.formPromise) {
- this.formPromise = this.pristineResource.$links
- .update(payload)
- .then((form:FormResource) => {
- this.formPromise = null;
- this.form = form;
- this.setNewDefaults(form);
- this.push();
- return form;
- })
- .catch((error:any) => {
- this.formPromise = null;
- this.form = null;
- throw error;
- }) as Promise<FormResource>;
- }
-
- return this.formPromise;
+ return this.pristineResource.$links
+ .update(payload)
+ .then((form:FormResource) => {
+ this.setNewDefaults(form);
+ this.push();
+ return form;
+ });
}
/**
@@ -215,9 +210,9 @@ export class ResourceChangeset<T extends HalResource|{ [key:string]:unknown; } =
}
public clear() {
- this.state && this.state.clear();
- this.changeset.clear();
- this.form = null;
+ this.state && this.state.clear();
+ this.changeset.clear();
+ this.form$.clear();
}
/**
@@ -243,7 +238,7 @@ export class ResourceChangeset<T extends HalResource|{ [key:string]:unknown; } =
* and contains available values.
*/
public get schema():SchemaResource {
- return (this.form || this.pristineResource).schema;
+ return this.form$.getValueOr(this.pristineResource).schema;
}
protected get minimalPayload() {
@@ -259,8 +254,8 @@ export class ResourceChangeset<T extends HalResource|{ [key:string]:unknown; } =
protected applyChanges(plainPayload:any) {
// Fall back to the last known state of the HalResource should the form not be loaded.
let reference = this.pristineResource.$source;
- if (this.form) {
- reference = this.form.payload.$source;
+ if (this.form$.value) {
+ reference = this.form$.value.payload.$source;
}
_.each(this.changeset.all, (val:unknown, key:string) => {
@@ -291,8 +286,8 @@ export class ResourceChangeset<T extends HalResource|{ [key:string]:unknown; } =
if (this.pristineResource.isNew) {
// If the resource is new, we need to pass the entire form payload
// to let all default values be transmitted (type, status, etc.)
- if (this.form) {
- payload = this.form.payload.$source;
+ if (this.form$.value) {
+ payload = this.form$.value.payload.$source;
} else {
payload = this.pristineResource.$source;
}
@@ -305,7 +300,7 @@ export class ResourceChangeset<T extends HalResource|{ [key:string]:unknown; } =
.attachments
.elements
.map((a:HalResource) => {
- return {href: a.href};
+ return { href: a.href };
});
}
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.ts
index 99b2bc41616..1b8c76c9b7f 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
@@ -89,7 +89,7 @@ export abstract class EditForm<T extends HalResource = HalResource> {
*/
protected onSaved(isInitial:boolean, saved:HalResource):void {
const eventType = isInitial ? 'created' : 'updated';
- this.halEvents.push(saved, {eventType});
+ this.halEvents.push(saved, { eventType });
}
protected abstract focusOnFirstError():void;
@@ -168,6 +168,9 @@ export abstract class EditForm<T extends HalResource = HalResource> {
return Promise.resolve(this.resource);
}
+ // Mark changeset as in flight
+ this.change.inFlight = true;
+
// Reset old error notifcations
this.errorsPerAttribute = {};
@@ -188,6 +191,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.change.inFlight = false;
})
.catch((error:ErrorResource|Object) => {
this.halNotification.handleRawError(error, this.resource);
@@ -196,6 +200,8 @@ export abstract class EditForm<T extends HalResource = HalResource> {
this.handleSubmissionErrors(error);
reject();
}
+
+ this.change.inFlight = false;
});
});
}
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.ts
index cf1514690b8..362716b454b 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
@@ -103,17 +103,8 @@ export class HalResourceEditingService extends StateCacheService<ResourceChanges
}
public async save<V extends HalResource, T extends ResourceChangeset<V>>(change:T):Promise<ResourceChangesetCommit<V>> {
- change.inFlight = true;
-
// Form the payload we're going to save
- const [form, payload] = await change.buildRequestPayload();
- // Reject errors when occurring in form validation
- const errors = form.getErrors();
- if (errors !== null) {
- change.inFlight = false;
- throw(errors);
- }
-
+ const payload = await change.buildRequestPayload();
const savedResource = await change.pristineResource.$links.updateImmediately(payload);
// Initialize any potentially new HAL values
@@ -121,8 +112,6 @@ export class HalResourceEditingService extends StateCacheService<ResourceChanges
this.onSaved(savedResource);
- change.inFlight = false;
-
// Complete the change
return this.complete(change, savedResource);
}