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 bug on column type options form #4046

Merged
merged 1 commit into from
Dec 6, 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 @@ -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.

Loading