From 5083396cc6a54a06d4dc95dd109c9208dae65757 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20G=C3=BCnther?= Date: Thu, 19 Sep 2019 15:49:33 +0200 Subject: [PATCH] Add special case for new descriptions again The new changeset is meant to avoid ever writing to work packages. In order to take over default descriptions on type change however, we need to avoid: - overriding a description that already exists in the changeset - writing the description in the changeset because that will result in the description sticking around when switching types again without ever typing in the description The previous solution worked because an actual work package resource was generated from scratch when type or project was changed. We no longer do this with the proxy. There's no longer any resource where we can change the description other than the original work package. For new work packages, this doesn't matter as this is destroyed and recreated anyway. Thus for new work packages, we can solve the description setting from the form's default. --- .../wp-edit/work-package-changeset.ts | 41 +++++++++++++++---- 1 file changed, 34 insertions(+), 7 deletions(-) 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 ff8f13fcc8..10420f27b0 100644 --- a/frontend/src/app/components/wp-edit/work-package-changeset.ts +++ b/frontend/src/app/components/wp-edit/work-package-changeset.ts @@ -54,17 +54,20 @@ export class WorkPackageChangeset extends ResourceChangeset return this.changeset.get(key); } - // TODO we might need values from the form (default values on type change?) - // Default value from the form? - // const payloadValue = _.get(this._form, ['payload', key]); - // if (payloadValue !== undefined) { - // return payloadValue; - // } - // Return whatever is on the base. return this.pristineResource[key]; } + /** + * Return whether the given value exists, + * even if its undefined. + * + * @param key + */ + public valueExists(key:string):boolean { + return this.changeset.contains(key) || this.pristineResource.hasOwnProperty(key); + } + public setValue(key:string, val:any) { this.changeset.set(key, val); @@ -148,6 +151,7 @@ export class WorkPackageChangeset extends ResourceChangeset .then((form:FormResource) => { this.wpFormPromise = null; this.form = form; + this.setNewDefaults(form); this.push(); return form; }) @@ -274,4 +278,27 @@ export class WorkPackageChangeset extends ResourceChangeset } } + /** + * When changing type or project, new custom fields may be present + * that we need to set. + */ + private setNewDefaults(form:FormResource) { + _.each(form.payload, (val:unknown, key:string) => { + const fieldSchema:IFieldSchema|undefined = this.schema[key]; + if (!(typeof (fieldSchema) === 'object' && fieldSchema.writable)) { + return; + } + + // Special handling for taking over the description + // to the pristine resource + if (key === 'description' && this.pristineResource.isNew) { + this.pristineResource.description = val; + } + + if (!this.valueExists(key)) { + debugLog("Taking over default value from form for " + key); + this.setValue(key, val); + } + }); + } }