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.
pull/7677/head
Oliver Günther 5 years ago
parent e23ee0c669
commit 5083396cc6
No known key found for this signature in database
GPG Key ID: A3A8BDAD7C0C552C
  1. 41
      frontend/src/app/components/wp-edit/work-package-changeset.ts

@ -54,17 +54,20 @@ export class WorkPackageChangeset extends ResourceChangeset<WorkPackageResource>
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<WorkPackageResource>
.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<WorkPackageResource>
}
}
/**
* 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);
}
});
}
}

Loading…
Cancel
Save