Skip to content

Commit

Permalink
Temp
Browse files Browse the repository at this point in the history
  • Loading branch information
charlesBochet committed Dec 19, 2024
1 parent 3fae330 commit 62445c2
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 29 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
import styled from '@emotion/styled';
import { useFormContext } from 'react-hook-form';

import { FieldMetadataItem } from '@/object-metadata/types/FieldMetadataItem';
import { SettingsDataModelPreviewFormCard } from '@/settings/data-model/components/SettingsDataModelPreviewFormCard';
import {
SettingsDataModelFieldNumberForm,
SettingsDataModelFieldNumberFormValues,
} from '@/settings/data-model/fields/forms/number/components/SettingsDataModelFieldNumberForm';
import { SettingsDataModelFieldNumberForm } from '@/settings/data-model/fields/forms/number/components/SettingsDataModelFieldNumberForm';
import {
SettingsDataModelFieldPreviewCard,
SettingsDataModelFieldPreviewCardProps,
Expand All @@ -16,7 +12,7 @@ type SettingsDataModelFieldNumberSettingsFormCardProps = {
disabled?: boolean;
fieldMetadataItem: Pick<
FieldMetadataItem,
'icon' | 'label' | 'type' | 'defaultValue' | 'settings'
'icon' | 'label' | 'type' | 'defaultValue'
>;
} & Pick<SettingsDataModelFieldPreviewCardProps, 'objectMetadataItem'>;

Expand All @@ -30,18 +26,11 @@ export const SettingsDataModelFieldNumberSettingsFormCard = ({
fieldMetadataItem,
objectMetadataItem,
}: SettingsDataModelFieldNumberSettingsFormCardProps) => {
const { watch } = useFormContext<SettingsDataModelFieldNumberFormValues>();

const settings = watch('settings');

return (
<SettingsDataModelPreviewFormCard
preview={
<StyledFieldPreviewCard
fieldMetadataItem={{
...fieldMetadataItem,
settings: settings ?? fieldMetadataItem.settings ?? { decimals: 0 },
}}
fieldMetadataItem={fieldMetadataItem}
objectMetadataItem={objectMetadataItem}
/>
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,35 @@
import { FieldMetadataItem } from '@/object-metadata/types/FieldMetadataItem';
import { SettingsFieldType } from '@/settings/data-model/types/SettingsFieldType';
import { isFieldValueEmpty } from '@/object-record/record-field/utils/isFieldValueEmpty';
import { generateDefaultFieldValue } from '@/object-record/utils/generateDefaultFieldValue';
import { getSettingsFieldTypeConfig } from '@/settings/data-model/utils/getSettingsFieldTypeConfig';
import { FieldMetadataType } from '~/generated-metadata/graphql';

const DEFAULT_NUMBER_PREVIEW_VALUE = 2000;
import { isFieldTypeSupportedInSettings } from '@/settings/data-model/utils/isFieldTypeSupportedInSettings';
import { isDefined } from '~/utils/isDefined';

export const getFieldPreviewValue = ({
fieldMetadataItem,
}: {
fieldMetadataItem: Pick<
FieldMetadataItem,
'type' | 'defaultValue'
>;
fieldMetadataItem: Pick<FieldMetadataItem, 'type' | 'defaultValue'>;
}) => {
if (fieldMetadataItem.defaultValue !== undefined && fieldMetadataItem.defaultValue !== null) {
return fieldMetadataItem.defaultValue;
if (!isFieldTypeSupportedInSettings(fieldMetadataItem.type)) return null;

if (
!isFieldValueEmpty({
fieldDefinition: { type: fieldMetadataItem.type },
fieldValue: fieldMetadataItem.defaultValue,
})
) {
return generateDefaultFieldValue(fieldMetadataItem);
}

const fieldTypeConfig = getSettingsFieldTypeConfig(fieldMetadataItem.type as SettingsFieldType);

if (fieldMetadataItem.type === FieldMetadataType.Number) {
return fieldTypeConfig.exampleValue || DEFAULT_NUMBER_PREVIEW_VALUE;
const fieldTypeConfig = getSettingsFieldTypeConfig(fieldMetadataItem.type);

if (
isDefined(fieldTypeConfig) &&
'exampleValue' in fieldTypeConfig &&
isDefined(fieldTypeConfig.exampleValue)
) {
return fieldTypeConfig.exampleValue;
}

return fieldTypeConfig.exampleValue ?? null;
return null;
};

0 comments on commit 62445c2

Please sign in to comment.