Skip to content

Commit

Permalink
fix: inherited values also work from initial values of a new entity (#…
Browse files Browse the repository at this point in the history
…2549)

fixes #2431
  • Loading branch information
sleidig authored Aug 29, 2024
1 parent a77bd02 commit 19fb3eb
Show file tree
Hide file tree
Showing 5 changed files with 184 additions and 100 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,17 @@ export type EntityFormGroup<T extends Entity> = TypedFormGroup<Partial<T>>;
export interface EntityForm<T extends Entity> {
formGroup: EntityFormGroup<T>;
entity: T;

/**
* map of field ids to the default value configuration for that field
*/
defaultValueConfigs: Map<string, DefaultValueConfig>;

/**
* map of field ids to the current value to be inherited from the referenced parent entities' field
*/
inheritedParentValues: Map<string, any>;

watcher: Map<string, Subscription>;
}

Expand Down
24 changes: 3 additions & 21 deletions src/app/core/config/config-fix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -397,8 +397,7 @@ export const defaultJsonConfig = {
"language"
],
"filters": [
{ "id": "privateSchool" },
{ "id": "parentSchool" }
{ "id": "privateSchool" }
]
}
},
Expand All @@ -416,7 +415,6 @@ export const defaultJsonConfig = {
"config": {
"fieldGroups": [
{ "fields": ["name", "privateSchool", "parentSchool"] },
{ fields: [ "testSchools", "bool", "string", "enum", "refSingle", "refMulti" ]},
{ "fields": ["address", "phone"] },
{ "fields": ["language", "timing"] },
{ "fields": ["remarks"] }
Expand Down Expand Up @@ -1071,21 +1069,11 @@ export const defaultJsonConfig = {
},
"privateSchool": {
"dataType": "boolean",
"label": $localize`:Label for if a school is a private school:Private School`,
"defaultValue": {
"mode": "inherited",
"localAttribute": "parentSchool",
"field": "privateSchool"
}
"label": $localize`:Label for if a school is a private school:Private School`
},
"language": {
"dataType": "string",
"label": $localize`:Label for the language of a school:Language`,
"defaultValue": {
"mode": "inherited",
"localAttribute": "parentSchool",
"field": "language"
}
"label": $localize`:Label for the language of a school:Language`
},
"address": {
"dataType": "location",
Expand All @@ -1102,12 +1090,6 @@ export const defaultJsonConfig = {
"remarks": {
"dataType": "string",
"label": $localize`:Label for the remarks for a school:Remarks`
},
"parentSchool": {
"dataType": "entity",
"additional": "School",
"label": $localize`:Label for school attribute:Branch of`,
"description": $localize`:Description for school attribute:Select the "parent school" here to build a hierarchy of a school with multiple branch institutions.`
}
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ export abstract class DefaultValueStrategy {
async initEntityForm<T extends Entity>(form: EntityForm<T>): Promise<void> {}
}

/**
* Get the default value configs filtered for the given mode.
* @param defaultValueConfigs
* @param mode
*/
export function getConfigsByMode(
defaultValueConfigs: Map<string, DefaultValueConfig>,
mode: ("inherited" | "static" | "dynamic")[],
Expand Down
31 changes: 29 additions & 2 deletions src/app/core/default-values/inherited-value.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ describe("InheritedValueService", () => {
);

// then
expect(targetFormControl.value).toBe(null);
expect(targetFormControl.value).toBe(undefined);
expect(
form.watcher.has("sourceFormControlValueChanges_field1"),
).toBeFalse();
Expand Down Expand Up @@ -314,7 +314,7 @@ describe("InheritedValueService", () => {
// when/then
form.formGroup.get("reference-1").setValue("non-existing-entity-id");
tick(); // fetching reference is always async
expect(form.formGroup.get("field").value).toBe(null);
expect(form.formGroup.get("field").value).toBe(undefined);
}));

it("should do nothing, if formGroup is disabled", fakeAsync(() => {
Expand Down Expand Up @@ -343,4 +343,31 @@ describe("InheritedValueService", () => {
tick(); // fetching reference is always async
expect(form.formGroup.get("field").value).toBe(null);
}));

it("should set value on FormControl, if source is not in formGroup but set on entity", fakeAsync(() => {
// given
let form = getDefaultInheritedForm({
field: {
isArray: true,
defaultValue: {
mode: "inherited",
field: "foo",
localAttribute: "reference-1",
},
},
});
form.formGroup.removeControl("reference-1");

let entity0 = new Entity();
entity0["foo"] = ["bar"];
mockEntityMapperService.load.and.returnValue(Promise.resolve(entity0));
form.entity["reference-1"] = entity0.getId();

// when
defaultValueService.handleEntityForm(form, form.entity);
tick();

// then
expect(form.formGroup.get("field").value).toEqual(["bar"]);
}));
});
Loading

0 comments on commit 19fb3eb

Please sign in to comment.