From 7f7ea59b51be9a8ce3389cdfcdaef991e5137607 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tha=C3=AFs?= Date: Fri, 24 May 2024 12:15:17 +0200 Subject: [PATCH] refactor: reset field default value on type change in Settings (#5534) Related issue: #5412 See https://github.com/twentyhq/twenty/pull/5436#discussion_r1609470484 for context. --- .../validation-schemas/selectOptionsSchema.ts | 2 +- .../SettingsDataModelFieldBooleanForm.tsx | 20 +++---- ...sDataModelFieldBooleanSettingsFormCard.tsx | 57 ++++++++++++++++++ .../useBooleanSettingsFormInitialValues.ts | 26 ++++++++ ...SettingsDataModelFieldSettingsFormCard.tsx | 51 ++++++++-------- .../SettingsDataModelFieldTypeSelect.tsx | 41 ++++++++++++- .../SettingsDataModelFieldCurrencyForm.tsx | 29 +++------ ...DataModelFieldCurrencySettingsFormCard.tsx | 60 +++++++++++++++++++ .../useCurrencySettingsFormInitialValues.ts | 37 ++++++++++++ .../useSelectSettingsFormInitialValues.ts | 38 ------------ .../SettingsDataModelFieldRelationForm.tsx | 4 +- ...DataModelFieldRelationSettingsFormCard.tsx | 4 +- .../useRelationSettingsFormInitialValues.ts | 0 .../SettingsDataModelFieldSelectForm.tsx | 11 +--- ...tingsDataModelFieldSelectFormOptionRow.tsx | 2 +- ...gsDataModelFieldSelectSettingsFormCard.tsx | 8 +-- .../useSelectSettingsFormInitialValues.ts | 49 +++++++++++++++ .../generateNewSelectOptionLabel.test.ts | 2 +- .../__tests__/getOptionValueFromLabel.test.ts | 2 +- .../utils/generateNewSelectOption.ts | 4 +- .../utils/generateNewSelectOptionLabel.ts | 0 .../utils/getOptionValueFromLabel.ts | 0 .../SettingsObjectNewFieldStep2.tsx | 10 ---- 23 files changed, 326 insertions(+), 131 deletions(-) rename packages/twenty-front/src/modules/settings/data-model/fields/forms/{components/boolean => boolean/components}/SettingsDataModelFieldBooleanForm.tsx (79%) create mode 100644 packages/twenty-front/src/modules/settings/data-model/fields/forms/boolean/components/SettingsDataModelFieldBooleanSettingsFormCard.tsx create mode 100644 packages/twenty-front/src/modules/settings/data-model/fields/forms/boolean/hooks/useBooleanSettingsFormInitialValues.ts rename packages/twenty-front/src/modules/settings/data-model/fields/forms/{components/currency => currency/components}/SettingsDataModelFieldCurrencyForm.tsx (72%) create mode 100644 packages/twenty-front/src/modules/settings/data-model/fields/forms/currency/components/SettingsDataModelFieldCurrencySettingsFormCard.tsx create mode 100644 packages/twenty-front/src/modules/settings/data-model/fields/forms/currency/hooks/useCurrencySettingsFormInitialValues.ts delete mode 100644 packages/twenty-front/src/modules/settings/data-model/fields/forms/hooks/useSelectSettingsFormInitialValues.ts rename packages/twenty-front/src/modules/settings/data-model/fields/forms/{components/relation => relation/components}/SettingsDataModelFieldRelationForm.tsx (98%) rename packages/twenty-front/src/modules/settings/data-model/fields/forms/{components/relation => relation/components}/SettingsDataModelFieldRelationSettingsFormCard.tsx (96%) rename packages/twenty-front/src/modules/settings/data-model/fields/forms/{ => relation}/hooks/useRelationSettingsFormInitialValues.ts (100%) rename packages/twenty-front/src/modules/settings/data-model/fields/forms/{components/select => select/components}/SettingsDataModelFieldSelectForm.tsx (95%) rename packages/twenty-front/src/modules/settings/data-model/fields/forms/{components/select => select/components}/SettingsDataModelFieldSelectFormOptionRow.tsx (99%) rename packages/twenty-front/src/modules/settings/data-model/fields/forms/{components/select => select/components}/SettingsDataModelFieldSelectSettingsFormCard.tsx (88%) create mode 100644 packages/twenty-front/src/modules/settings/data-model/fields/forms/select/hooks/useSelectSettingsFormInitialValues.ts rename packages/twenty-front/src/modules/settings/data-model/fields/forms/{ => select}/utils/__tests__/generateNewSelectOptionLabel.test.ts (85%) rename packages/twenty-front/src/modules/settings/data-model/fields/forms/{ => select}/utils/__tests__/getOptionValueFromLabel.test.ts (90%) rename packages/twenty-front/src/modules/settings/data-model/fields/forms/{ => select}/utils/generateNewSelectOption.ts (85%) rename packages/twenty-front/src/modules/settings/data-model/fields/forms/{ => select}/utils/generateNewSelectOptionLabel.ts (100%) rename packages/twenty-front/src/modules/settings/data-model/fields/forms/{ => select}/utils/getOptionValueFromLabel.ts (100%) diff --git a/packages/twenty-front/src/modules/object-metadata/validation-schemas/selectOptionsSchema.ts b/packages/twenty-front/src/modules/object-metadata/validation-schemas/selectOptionsSchema.ts index 4b634a3ac4..814892da9b 100644 --- a/packages/twenty-front/src/modules/object-metadata/validation-schemas/selectOptionsSchema.ts +++ b/packages/twenty-front/src/modules/object-metadata/validation-schemas/selectOptionsSchema.ts @@ -1,7 +1,7 @@ import { z } from 'zod'; import { FieldMetadataItemOption } from '@/object-metadata/types/FieldMetadataItem'; -import { getOptionValueFromLabel } from '@/settings/data-model/fields/forms/utils/getOptionValueFromLabel'; +import { getOptionValueFromLabel } from '@/settings/data-model/fields/forms/select/utils/getOptionValueFromLabel'; import { themeColorSchema } from '@/ui/theme/utils/themeColorSchema'; import { computeOptionValueFromLabelOrThrow } from '~/pages/settings/data-model/utils/compute-option-value-from-label.utils'; diff --git a/packages/twenty-front/src/modules/settings/data-model/fields/forms/components/boolean/SettingsDataModelFieldBooleanForm.tsx b/packages/twenty-front/src/modules/settings/data-model/fields/forms/boolean/components/SettingsDataModelFieldBooleanForm.tsx similarity index 79% rename from packages/twenty-front/src/modules/settings/data-model/fields/forms/components/boolean/SettingsDataModelFieldBooleanForm.tsx rename to packages/twenty-front/src/modules/settings/data-model/fields/forms/boolean/components/SettingsDataModelFieldBooleanForm.tsx index 806e3d16fb..c737fab7db 100644 --- a/packages/twenty-front/src/modules/settings/data-model/fields/forms/components/boolean/SettingsDataModelFieldBooleanForm.tsx +++ b/packages/twenty-front/src/modules/settings/data-model/fields/forms/boolean/components/SettingsDataModelFieldBooleanForm.tsx @@ -1,10 +1,10 @@ -import { useEffect } from 'react'; import { Controller, useFormContext } from 'react-hook-form'; import styled from '@emotion/styled'; import { IconCheck, IconX } from 'twenty-ui'; import { z } from 'zod'; import { FieldMetadataItem } from '@/object-metadata/types/FieldMetadataItem'; +import { useBooleanSettingsFormInitialValues } from '@/settings/data-model/fields/forms/boolean/hooks/useBooleanSettingsFormInitialValues'; import { Select } from '@/ui/input/components/Select'; import { CardContent } from '@/ui/layout/card/components/CardContent'; import { isDefined } from '~/utils/isDefined'; @@ -13,13 +13,13 @@ export const settingsDataModelFieldBooleanFormSchema = z.object({ defaultValue: z.boolean(), }); -type SettingsDataModelFieldBooleanFormValues = z.infer< +export type SettingsDataModelFieldBooleanFormValues = z.infer< typeof settingsDataModelFieldBooleanFormSchema >; type SettingsDataModelFieldBooleanFormProps = { className?: string; - fieldMetadataItem?: Pick; + fieldMetadataItem: Pick; }; const StyledContainer = styled(CardContent)` @@ -38,16 +38,12 @@ export const SettingsDataModelFieldBooleanForm = ({ className, fieldMetadataItem, }: SettingsDataModelFieldBooleanFormProps) => { - const { control, resetField } = - useFormContext(); + const { control } = useFormContext(); const isEditMode = isDefined(fieldMetadataItem?.defaultValue); - const initialValue = fieldMetadataItem?.defaultValue ?? true; - - // Reset defaultValue on mount, so it doesn't conflict with other field types. - useEffect(() => { - resetField('defaultValue', { defaultValue: initialValue }); - }, [initialValue, resetField]); + const { initialDefaultValue } = useBooleanSettingsFormInitialValues({ + fieldMetadataItem, + }); return ( @@ -55,7 +51,7 @@ export const SettingsDataModelFieldBooleanForm = ({ (