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: GraphQL setting unset children to null shouldn't make them dirty. #115

Merged
merged 1 commit into from
Oct 24, 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
19 changes: 16 additions & 3 deletions src/fields/objectField.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { FragmentFieldConfig, ListFieldConfig, ObjectConfig, ObjectFieldConfig,
import { FragmentField, newFragmentField } from "src/fields/fragmentField";
import { ListFieldState, newListFieldState } from "src/fields/listField";
import { FieldState, FieldStateInternal, InternalSetOpts, SetOpts, newValueFieldState } from "src/fields/valueField";
import { Builtin, deepClone, fail } from "src/utils";
import { areEqual, Builtin, deepClone, fail } from "src/utils";

/**
* Wraps a given input/on-the-wire type `T` for editing in a form.
Expand Down Expand Up @@ -162,6 +162,10 @@ export function newObjectState<T, P = any>(
maybeAutoSave,
);
} else if (config.type === "object") {
// Because our objectField will fundamentally want to do `child.firstName.set(...)` or
// even `child.firstName !== undefined`, etc., we "simplify" things by always setting
// an empty object, for our child valueFields/listFields to use/read from, although
// we then have to "oh right ignore {}" in places like `dirty`.
if (!instance[key]) {
instance[key] = {} as any;
}
Expand Down Expand Up @@ -263,7 +267,12 @@ export function newObjectState<T, P = any>(
},

get dirty(): boolean {
return getFields(this).some((f) => f.dirty) || this.isUnset();
return (
getFields(this).some((f) => f.dirty) ||
// `isUnset` checks if our `parent[key] === undefined`, which can mean "surely we're dirty",
// but as long as we've got some keys actually set
(this.isUnset() && !areEqual(this.value, {}))
);
},

get isNewEntity(): boolean {
Expand Down Expand Up @@ -342,7 +351,11 @@ export function newObjectState<T, P = any>(
// look dirty, but if we're new we should include them anyway.
(this.isNewEntity && (f as any)._kind === "value" && f.value !== undefined) ||
// ...or they're non-empty sub-objects
(this.isNewEntity && (f as any)._kind === "object" && Object.entries(f.changedValue).length > 0) ||
(this.isNewEntity &&
(f as any)._kind === "object" &&
// Child objects that are unset will have `changedValue = null`
f.changedValue &&
Object.entries(f.changedValue).length > 0) ||
// ...or they're non-empty sub-lists
(this.isNewEntity && (f as any)._kind === "list" && f.value?.length > 0)
) {
Expand Down
14 changes: 14 additions & 0 deletions src/formState.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,20 @@ describe("formState", () => {
expect(a1.dirty).toBeFalsy();
});

it("realizes object fields set to null aren't actually dirty", () => {
// Given we started off with a `object: undefined`
const a1 = createAuthorWithAddressInputState({ address: undefined });
expect(a1.address.dirty).toBe(false);
expect(a1.address.value).toEqual({});
// And it's later set to null, i.e. from a mutation returning `{ address: null }`
a1.set({ firstName: "a1", address: null });
// Then we convert it to `{}` to potentially hold data
expect(a1.address.value).toEqual({});
// But we don't consider the address dirty
expect(a1.address.dirty).toBe(false);
expect(a1.changedValue).toEqual({ firstName: "a1" });
});

it("knows an object's field of type object is dirty", () => {
const a1 = createAuthorInputState({
books: [{ title: "b1", classification: dd100 }],
Expand Down
Loading