Skip to content

Commit

Permalink
Allow standard field default value and settings editing (#7104) (#8559)
Browse files Browse the repository at this point in the history
Co-authored-by: Marie Stoppa <[email protected]>
  • Loading branch information
ad-elias and ijreilly authored Nov 26, 2024
1 parent 7bde200 commit 2e75fae
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { z } from 'zod';
import { FieldMetadataItem } from '@/object-metadata/types/FieldMetadataItem';
import { SettingsOptionCardContentSelect } from '@/settings/components/SettingsOptions/SettingsOptionCardContentSelect';
import { useBooleanSettingsFormInitialValues } from '@/settings/data-model/fields/forms/boolean/hooks/useBooleanSettingsFormInitialValues';
import { isDefined } from '~/utils/isDefined';

export const settingsDataModelFieldBooleanFormSchema = z.object({
defaultValue: z.boolean(),
Expand All @@ -26,7 +25,6 @@ export const SettingsDataModelFieldBooleanForm = ({
}: SettingsDataModelFieldBooleanFormProps) => {
const { control } = useFormContext<SettingsDataModelFieldBooleanFormValues>();

const isEditMode = isDefined(fieldMetadataItem?.defaultValue);
const { initialDefaultValue } = useBooleanSettingsFormInitialValues({
fieldMetadataItem,
});
Expand All @@ -44,9 +42,6 @@ export const SettingsDataModelFieldBooleanForm = ({
value={value}
onChange={onChange}
selectClassName={className}
// TODO: temporary fix - disabling edition because after editing the defaultValue,
// newly created records are not taking into account the updated defaultValue properly.
disabled={isEditMode}
dropdownId="object-field-default-value-select-boolean"
options={[
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ export const settingsDataModelFieldSettingsFormSchema = z.discriminatedUnion(
);

type SettingsDataModelFieldSettingsFormCardProps = {
isCreatingField?: boolean;
fieldMetadataItem: Pick<
FieldMetadataItem,
'icon' | 'label' | 'type' | 'isCustom'
Expand Down Expand Up @@ -132,7 +131,6 @@ const previewableTypes = [
];

export const SettingsDataModelFieldSettingsFormCard = ({
isCreatingField,
fieldMetadataItem,
objectMetadataItem,
}: SettingsDataModelFieldSettingsFormCardProps) => {
Expand All @@ -152,7 +150,6 @@ export const SettingsDataModelFieldSettingsFormCard = ({
if (fieldMetadataItem.type === FieldMetadataType.Currency) {
return (
<SettingsDataModelFieldCurrencySettingsFormCard
disabled={!isCreatingField}
fieldMetadataItem={fieldMetadataItem}
objectMetadataItem={objectMetadataItem}
/>
Expand All @@ -165,7 +162,6 @@ export const SettingsDataModelFieldSettingsFormCard = ({
) {
return (
<SettingsDataModelFieldDateSettingsFormCard
disabled={!isCreatingField}
fieldMetadataItem={fieldMetadataItem}
objectMetadataItem={objectMetadataItem}
/>
Expand All @@ -184,7 +180,6 @@ export const SettingsDataModelFieldSettingsFormCard = ({
if (fieldMetadataItem.type === FieldMetadataType.Number) {
return (
<SettingsDataModelFieldNumberSettingsFormCard
disabled={fieldMetadataItem.isCustom === false}
fieldMetadataItem={fieldMetadataItem}
objectMetadataItem={objectMetadataItem}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import { useFieldMetadataItem } from '@/object-metadata/hooks/useFieldMetadataIt
import { useFilteredObjectMetadataItems } from '@/object-metadata/hooks/useFilteredObjectMetadataItems';
import { useGetRelationMetadata } from '@/object-metadata/hooks/useGetRelationMetadata';
import { useUpdateOneFieldMetadataItem } from '@/object-metadata/hooks/useUpdateOneFieldMetadataItem';
import { FieldMetadataItem } from '@/object-metadata/types/FieldMetadataItem';
import { formatFieldMetadataItemInput } from '@/object-metadata/utils/formatFieldMetadataItemInput';
import { getFieldSlug } from '@/object-metadata/utils/getFieldSlug';
import { isLabelIdentifierField } from '@/object-metadata/utils/isLabelIdentifierField';
Expand Down Expand Up @@ -47,16 +46,6 @@ type SettingsDataModelFieldEditFormValues = z.infer<
> &
any;

const canPersistFieldMetadataItemUpdate = (
fieldMetadataItem: FieldMetadataItem,
) => {
return (
fieldMetadataItem.isCustom ||
fieldMetadataItem.type === FieldMetadataType.Select ||
fieldMetadataItem.type === FieldMetadataType.MultiSelect
);
};

export const SettingsObjectFieldEdit = () => {
const navigate = useNavigate();
const { enqueueSnackBar } = useSnackBar();
Expand Down Expand Up @@ -179,9 +168,6 @@ export const SettingsObjectFieldEdit = () => {
navigate(`/settings/objects/${objectSlug}`);
};

const shouldDisplaySaveAndCancel =
canPersistFieldMetadataItemUpdate(fieldMetadataItem);

return (
<RecordFieldValueSelectorContextProvider>
{/* eslint-disable-next-line react/jsx-props-no-spreading */}
Expand All @@ -206,14 +192,12 @@ export const SettingsObjectFieldEdit = () => {
},
]}
actionButton={
shouldDisplaySaveAndCancel && (
<SaveAndCancelButtons
isSaveDisabled={!canSave}
isCancelDisabled={isSubmitting}
onCancel={() => navigate(`/settings/objects/${objectSlug}`)}
onSave={formConfig.handleSubmit(handleSave)}
/>
)
<SaveAndCancelButtons
isSaveDisabled={!canSave}
isCancelDisabled={isSubmitting}
onCancel={() => navigate(`/settings/objects/${objectSlug}`)}
onSave={formConfig.handleSubmit(handleSave)}
/>
}
>
<SettingsPageContainer>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,6 @@ export const SettingsObjectNewFieldConfigure = () => {
<Section>
<H2Title title="Values" description="The values of this field" />
<SettingsDataModelFieldSettingsFormCard
isCreatingField
fieldMetadataItem={{
icon: formConfig.watch('icon'),
label: formConfig.watch('label') || 'New Field',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -632,23 +632,25 @@ export class FieldMetadataService extends TypeOrmQueryService<FieldMetadataEntit
fieldMetadataInput: UpdateFieldInput,
existingFieldMetadata: FieldMetadataEntity,
) {
let fieldMetadataInputOverrided = {};

fieldMetadataInputOverrided = {
const updatableStandardFieldInput: UpdateFieldInput = {
id: fieldMetadataInput.id,
isActive: fieldMetadataInput.isActive,
workspaceId: fieldMetadataInput.workspaceId,
defaultValue: fieldMetadataInput.defaultValue,
settings: fieldMetadataInput.settings,
};

if (existingFieldMetadata.type === FieldMetadataType.SELECT) {
fieldMetadataInputOverrided = {
...fieldMetadataInputOverrided,
if (
existingFieldMetadata.type === FieldMetadataType.SELECT ||
existingFieldMetadata.type === FieldMetadataType.MULTI_SELECT
) {
return {
...updatableStandardFieldInput,
options: fieldMetadataInput.options,
};
}

return fieldMetadataInputOverrided as UpdateFieldInput;
return updatableStandardFieldInput;
}

public async getRelationDefinitionFromRelationMetadata(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@ import {
} from 'src/engine/workspace-manager/workspace-sync-metadata/interfaces/comparator.interface';
import { ComputedPartialFieldMetadata } from 'src/engine/workspace-manager/workspace-sync-metadata/interfaces/partial-field-metadata.interface';

import {
FieldMetadataEntity,
FieldMetadataType,
} from 'src/engine/metadata-modules/field-metadata/field-metadata.entity';
import { FieldMetadataEntity } from 'src/engine/metadata-modules/field-metadata/field-metadata.entity';
import { transformMetadataForComparison } from 'src/engine/workspace-manager/workspace-sync-metadata/comparators/utils/transform-metadata-for-comparison.util';

const commonFieldPropertiesToIgnore = [
Expand All @@ -26,6 +23,7 @@ const commonFieldPropertiesToIgnore = [
'gate',
'asExpression',
'generatedType',
'defaultValue',
];

const fieldPropertiesToStringify = ['defaultValue'] as const;
Expand Down Expand Up @@ -57,19 +55,11 @@ export class WorkspaceFieldComparator {
const originalFieldMetadataMap = transformMetadataForComparison(
filteredOriginalFieldCollection,
{
shouldIgnoreProperty: (property, originalMetadata) => {
shouldIgnoreProperty: (property) => {
if (commonFieldPropertiesToIgnore.includes(property)) {
return true;
}

if (
originalMetadata &&
property === 'defaultValue' &&
originalMetadata.type === FieldMetadataType.SELECT
) {
return true;
}

return false;
},
propertiesToStringify: fieldPropertiesToStringify,
Expand All @@ -82,19 +72,11 @@ export class WorkspaceFieldComparator {
const standardFieldMetadataMap = transformMetadataForComparison(
standardFieldMetadataCollection,
{
shouldIgnoreProperty: (property, originalMetadata) => {
shouldIgnoreProperty: (property) => {
if (commonFieldPropertiesToIgnore.includes(property)) {
return true;
}

if (
originalMetadata &&
property === 'defaultValue' &&
originalMetadata.type === FieldMetadataType.SELECT
) {
return true;
}

return false;
},
propertiesToStringify: fieldPropertiesToStringify,
Expand Down

0 comments on commit 2e75fae

Please sign in to comment.