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: fix Settings field form validation for certain field types #5335

Merged
merged 3 commits into from
May 8, 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
@@ -1,5 +1,6 @@
import { useFormContext } from 'react-hook-form';
import styled from '@emotion/styled';
import omit from 'lodash.omit';
import { z } from 'zod';

import { useFilteredObjectMetadataItems } from '@/object-metadata/hooks/useFilteredObjectMetadataItems';
Expand All @@ -22,6 +23,7 @@ import {
settingsDataModelFieldSelectFormSchema,
} from '@/settings/data-model/components/SettingsObjectFieldSelectForm';
import { RELATION_TYPES } from '@/settings/data-model/constants/RelationTypes';
import { SETTINGS_FIELD_TYPE_CONFIGS } from '@/settings/data-model/constants/SettingsFieldTypeConfigs';
import {
SettingsDataModelFieldPreviewCard,
SettingsDataModelFieldPreviewCardProps,
Expand All @@ -46,13 +48,28 @@ const selectFieldFormSchema = z
})
.merge(settingsDataModelFieldSelectFormSchema);

const otherFieldsFormSchema = z.object({
type: z.enum(
Object.keys(
omit(SETTINGS_FIELD_TYPE_CONFIGS, [
FieldMetadataType.Boolean,
FieldMetadataType.Currency,
FieldMetadataType.Relation,
FieldMetadataType.Select,
FieldMetadataType.MultiSelect,
]),
) as [FieldMetadataType, ...FieldMetadataType[]],
),
});

export const settingsDataModelFieldSettingsFormSchema = z.discriminatedUnion(
'type',
[
booleanFieldFormSchema,
currencyFieldFormSchema,
relationFieldFormSchema,
selectFieldFormSchema,
otherFieldsFormSchema,
],
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
} from '@/settings/data-model/constants/SettingsFieldTypeConfigs';
import { SettingsSupportedFieldType } from '@/settings/data-model/types/SettingsSupportedFieldType';
import { Select, SelectOption } from '@/ui/input/components/Select';
import { FieldMetadataType } from '~/generated-metadata/graphql';

export const settingsDataModelFieldTypeFormSchema = z.object({
type: z.enum(
Expand Down Expand Up @@ -57,7 +58,7 @@ export const SettingsDataModelFieldTypeSelect = ({
defaultValue={
fieldMetadataItem && fieldMetadataItem.type in fieldTypeConfigs
? (fieldMetadataItem.type as SettingsSupportedFieldType)
: undefined
: FieldMetadataType.Text
}
render={({ field: { onChange, value } }) => (
<Select
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ export const WithOpenSelect: Story = {
play: async ({ canvasElement }) => {
const canvas = within(canvasElement);

const inputField = await canvas.findByText('Unique ID');
const inputField = await canvas.findByText('Text');

await userEvent.click(inputField);

const input = await canvas.findByText('Text');
const input = await canvas.findByText('Unique ID');
await userEvent.click(input);

await userEvent.click(inputField);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,9 @@ export const SettingsObjectFieldEdit = () => {
try {
if (
formValues.type === FieldMetadataType.Relation &&
isNonEmptyString(relationFieldMetadataItem?.id) &&
'relation' in dirtyFields
'relation' in formValues &&
'relation' in dirtyFields &&
isNonEmptyString(relationFieldMetadataItem?.id)
) {
await updateOneFieldMetadataItem({
fieldMetadataIdToUpdate: relationFieldMetadataItem.id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,8 @@ export const SettingsObjectNewFieldStep2 = () => {
const createdMetadataField = await createMetadataField({
...formValues,
defaultValue:
formValues.type === FieldMetadataType.Currency
formValues.type === FieldMetadataType.Currency &&
'defaultValue' in formValues
? {
...formValues.defaultValue,
amountMicros: null,
Expand Down
Loading