Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: correctly reset special fields like username when cancelling forms #2191

Merged
merged 1 commit into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,36 @@ describe("EntityFormService", () => {
expect(unsavedChanges.pending).toBeFalse();
});

it("should reset form on cancel, including special fields with getter", async () => {
class MockEntity extends Entity {
@DatabaseField() simpleField = "original";

@DatabaseField() get getterField(): string {
return this._getterValue;
}
set getterField(value) {
this._getterValue = value;
}
private _getterValue: string = "original value";

@DatabaseField() emptyField;
}

const formFields = ["simpleField", "getterField", "emptyField"];
const mockEntity = new MockEntity();
const formGroup = service.createFormGroup(formFields, mockEntity);

formGroup.get("simpleField").setValue("new");
formGroup.get("getterField").setValue("new value");
formGroup.get("emptyField").setValue("value");

service.resetForm(formGroup, mockEntity);

expect(formGroup.get("simpleField").value).toBe("original");
expect(formGroup.get("getterField").value).toBe("original value");
expect(formGroup.get("emptyField").value).toBeUndefined();
});

it("should reset state once navigation happens", async () => {
const router = TestBed.inject(Router);
router.resetConfig([{ path: "test", component: NotFoundComponent }]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { EntitySchemaService } from "../../entity/schema/entity-schema.service";
import { DynamicValidatorsService } from "./dynamic-form-validators/dynamic-validators.service";
import { EntityAbility } from "../../permissions/ability/entity-ability";
import { InvalidFormFieldError } from "./invalid-form-field.error";
import { omit } from "lodash-es";
import { UnsavedChangesService } from "../../entity-details/form/unsaved-changes.service";
import { ActivationStart, Router } from "@angular/router";
import { Subscription } from "rxjs";
Expand Down Expand Up @@ -245,11 +244,10 @@ export class EntityFormService {
}

resetForm<E extends Entity>(form: EntityForm<E>, entity: E) {
// Patch form with values from the entity
form.patchValue(entity as any);
// Clear values that are not yet present on the entity
const newKeys = Object.keys(omit(form.controls, Object.keys(entity)));
newKeys.forEach((key) => form.get(key).setValue(null));
for (const key of Object.keys(form.controls)) {
form.get(key).setValue(entity[key]);
}

form.markAsPristine();
this.unsavedChanges.pending = false;
}
Expand Down
2 changes: 1 addition & 1 deletion src/app/core/entity-details/form/form.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,6 @@ describe("FormComponent", () => {
component.form.get("name").setValue("my name");
component.cancelClicked();

expect(component.form.get("name")).toHaveValue(null);
expect(component.form.get("name").value).toBeUndefined();
});
});