Skip to content

Commit

Permalink
Merge pull request #4046 from mathesar-foundation/3878_type_options_f…
Browse files Browse the repository at this point in the history
…orm_bug

Fix bug on column type options form
  • Loading branch information
pavish authored Dec 6, 2024
2 parents 3b769bf + 7c1328a commit 5552df8
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import type { RequestStatus } from '@mathesar/api/rest/utils/requestUtils';
import { toast } from '@mathesar/stores/toast';
import { objectsAreDeeplyEqual } from '@mathesar/utils/objectUtils';
import { columnTypeOptionsAreEqual } from '@mathesar/utils/columnUtils';
import {
CancelOrProceedButtonPair,
createValidationContext,
Expand Down Expand Up @@ -38,7 +38,7 @@
$: actionButtonsVisible =
selectedAbstractType !== column.abstractType ||
selectedDbType !== column.type ||
!objectsAreDeeplyEqual(savedTypeOptions, typeOptions);
!columnTypeOptionsAreEqual(savedTypeOptions, typeOptions ?? {});
let typeChangeState: RequestStatus;
Expand Down
31 changes: 30 additions & 1 deletion mathesar_ui/src/utils/columnUtils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Column } from '@mathesar/api/rpc/columns';
import type { Column, ColumnTypeOptions } from '@mathesar/api/rpc/columns';
import type { ConstraintType } from '@mathesar/api/rpc/constraints';
import { type ValidationFn, uniqueWith } from '@mathesar/components/form';
import { iconConstraint, iconTableLink } from '@mathesar/icons';
Expand Down Expand Up @@ -66,3 +66,32 @@ export function getColumnConstraintTypeByColumnId(
);
return constraintsType;
}

export function columnTypeOptionsAreEqual(
a: ColumnTypeOptions,
b: ColumnTypeOptions,
): boolean {
type TypeOption = keyof ColumnTypeOptions;
// This weird object exists for type safety purposes. This way, if a new field
// is added to ColumnTypeOptions, we'll get a type error here if we don't
// update this object.
const fieldsObj: Record<TypeOption, unknown> = {
precision: null,
scale: null,
length: null,
fields: null,
item_type: null,
};
const fields = Object.keys(fieldsObj) as TypeOption[];

for (const field of fields) {
// The nullish coalescing here is important and kind of the main reason this
// function exists. We need to make sure that if a field is missing from one
// object while present but `null` in the other object, then the two objects
// are still considered equal as far as comparing column type options goes.
if ((a[field] ?? null) !== (b[field] ?? null)) {
return false;
}
}
return true;
}
14 changes: 0 additions & 14 deletions mathesar_ui/src/utils/objectUtils.ts

This file was deleted.

0 comments on commit 5552df8

Please sign in to comment.