Fix field forms (#7595)
@lucasbordeau forms are broken! revert - #7363 used useRelationSettingsFormInitialValues hook from that commit. TODO - figure out a way to change the relation name label from singular to plural and vice versa, until it is edited. related issue - #7355 --------- Co-authored-by: Lucas Bordeau <bordeau.lucas@gmail.com>
This commit is contained in:
@ -3,14 +3,10 @@ import { Controller, useFormContext } from 'react-hook-form';
|
|||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
|
|
||||||
import { FieldMetadataItem } from '@/object-metadata/types/FieldMetadataItem';
|
import { FieldMetadataItem } from '@/object-metadata/types/FieldMetadataItem';
|
||||||
import { ObjectMetadataItem } from '@/object-metadata/types/ObjectMetadataItem';
|
|
||||||
import { fieldMetadataItemSchema } from '@/object-metadata/validation-schemas/fieldMetadataItemSchema';
|
import { fieldMetadataItemSchema } from '@/object-metadata/validation-schemas/fieldMetadataItemSchema';
|
||||||
import { getErrorMessageFromError } from '@/settings/data-model/fields/forms/utils/errorMessages';
|
import { getErrorMessageFromError } from '@/settings/data-model/fields/forms/utils/errorMessages';
|
||||||
import { RelationType } from '@/settings/data-model/types/RelationType';
|
|
||||||
import { IconPicker } from '@/ui/input/components/IconPicker';
|
import { IconPicker } from '@/ui/input/components/IconPicker';
|
||||||
import { TextInput } from '@/ui/input/components/TextInput';
|
import { TextInput } from '@/ui/input/components/TextInput';
|
||||||
import { useEffect, useState } from 'react';
|
|
||||||
import { RelationDefinitionType } from '~/generated-metadata/graphql';
|
|
||||||
|
|
||||||
export const settingsDataModelFieldIconLabelFormSchema = (
|
export const settingsDataModelFieldIconLabelFormSchema = (
|
||||||
existingOtherLabels: string[] = [],
|
existingOtherLabels: string[] = [],
|
||||||
@ -36,47 +32,19 @@ type SettingsDataModelFieldIconLabelFormProps = {
|
|||||||
disabled?: boolean;
|
disabled?: boolean;
|
||||||
fieldMetadataItem?: FieldMetadataItem;
|
fieldMetadataItem?: FieldMetadataItem;
|
||||||
maxLength?: number;
|
maxLength?: number;
|
||||||
relationObjectMetadataItem?: ObjectMetadataItem;
|
|
||||||
relationType?: RelationType;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export const SettingsDataModelFieldIconLabelForm = ({
|
export const SettingsDataModelFieldIconLabelForm = ({
|
||||||
disabled,
|
disabled,
|
||||||
fieldMetadataItem,
|
fieldMetadataItem,
|
||||||
maxLength,
|
maxLength,
|
||||||
relationObjectMetadataItem,
|
|
||||||
relationType,
|
|
||||||
}: SettingsDataModelFieldIconLabelFormProps) => {
|
}: SettingsDataModelFieldIconLabelFormProps) => {
|
||||||
const {
|
const {
|
||||||
control,
|
control,
|
||||||
trigger,
|
trigger,
|
||||||
formState: { errors },
|
formState: { errors },
|
||||||
setValue,
|
|
||||||
} = useFormContext<SettingsDataModelFieldIconLabelFormValues>();
|
} = useFormContext<SettingsDataModelFieldIconLabelFormValues>();
|
||||||
|
|
||||||
const [labelEditedManually, setLabelEditedManually] = useState(false);
|
|
||||||
const [iconEditedManually, setIconEditedManually] = useState(false);
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
if (labelEditedManually || !relationType) return;
|
|
||||||
const label = [
|
|
||||||
RelationDefinitionType.ManyToOne,
|
|
||||||
RelationDefinitionType.ManyToMany,
|
|
||||||
].includes(relationType ?? RelationDefinitionType.OneToMany)
|
|
||||||
? relationObjectMetadataItem?.labelPlural
|
|
||||||
: relationObjectMetadataItem?.labelSingular;
|
|
||||||
setValue('label', label ?? '');
|
|
||||||
|
|
||||||
if (iconEditedManually) return;
|
|
||||||
setValue('icon', relationObjectMetadataItem?.icon ?? 'IconUsers');
|
|
||||||
}, [
|
|
||||||
labelEditedManually,
|
|
||||||
iconEditedManually,
|
|
||||||
relationObjectMetadataItem,
|
|
||||||
setValue,
|
|
||||||
relationType,
|
|
||||||
]);
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<StyledInputsContainer>
|
<StyledInputsContainer>
|
||||||
<Controller
|
<Controller
|
||||||
@ -87,10 +55,7 @@ export const SettingsDataModelFieldIconLabelForm = ({
|
|||||||
<IconPicker
|
<IconPicker
|
||||||
disabled={disabled}
|
disabled={disabled}
|
||||||
selectedIconKey={value ?? ''}
|
selectedIconKey={value ?? ''}
|
||||||
onChange={({ iconKey }) => {
|
onChange={({ iconKey }) => onChange(iconKey)}
|
||||||
setIconEditedManually(true);
|
|
||||||
onChange(iconKey);
|
|
||||||
}}
|
|
||||||
variant="primary"
|
variant="primary"
|
||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
@ -104,7 +69,6 @@ export const SettingsDataModelFieldIconLabelForm = ({
|
|||||||
placeholder="Employees"
|
placeholder="Employees"
|
||||||
value={value}
|
value={value}
|
||||||
onChange={(e) => {
|
onChange={(e) => {
|
||||||
setLabelEditedManually(true);
|
|
||||||
onChange(e);
|
onChange(e);
|
||||||
trigger('label');
|
trigger('label');
|
||||||
}}
|
}}
|
||||||
|
|||||||
@ -5,18 +5,17 @@ import { z } from 'zod';
|
|||||||
|
|
||||||
import { useFilteredObjectMetadataItems } from '@/object-metadata/hooks/useFilteredObjectMetadataItems';
|
import { useFilteredObjectMetadataItems } from '@/object-metadata/hooks/useFilteredObjectMetadataItems';
|
||||||
import { FieldMetadataItem } from '@/object-metadata/types/FieldMetadataItem';
|
import { FieldMetadataItem } from '@/object-metadata/types/FieldMetadataItem';
|
||||||
|
import { ObjectMetadataItem } from '@/object-metadata/types/ObjectMetadataItem';
|
||||||
import { isObjectMetadataAvailableForRelation } from '@/object-metadata/utils/isObjectMetadataAvailableForRelation';
|
import { isObjectMetadataAvailableForRelation } from '@/object-metadata/utils/isObjectMetadataAvailableForRelation';
|
||||||
import { fieldMetadataItemSchema } from '@/object-metadata/validation-schemas/fieldMetadataItemSchema';
|
import { fieldMetadataItemSchema } from '@/object-metadata/validation-schemas/fieldMetadataItemSchema';
|
||||||
import { FIELD_NAME_MAXIMUM_LENGTH } from '@/settings/data-model/constants/FieldNameMaximumLength';
|
import { FIELD_NAME_MAXIMUM_LENGTH } from '@/settings/data-model/constants/FieldNameMaximumLength';
|
||||||
import { RELATION_TYPES } from '@/settings/data-model/constants/RelationTypes';
|
import { RELATION_TYPES } from '@/settings/data-model/constants/RelationTypes';
|
||||||
import { useRelationSettingsFormInitialValues } from '@/settings/data-model/fields/forms/relation/hooks/useRelationSettingsFormInitialValues';
|
import { useRelationSettingsFormInitialValues } from '@/settings/data-model/fields/forms/relation/hooks/useRelationSettingsFormInitialValues';
|
||||||
import { SettingsDataModelFieldPreviewCardProps } from '@/settings/data-model/fields/preview/components/SettingsDataModelFieldPreviewCard';
|
|
||||||
import { RelationType } from '@/settings/data-model/types/RelationType';
|
import { RelationType } from '@/settings/data-model/types/RelationType';
|
||||||
import { IconPicker } from '@/ui/input/components/IconPicker';
|
import { IconPicker } from '@/ui/input/components/IconPicker';
|
||||||
import { Select } from '@/ui/input/components/Select';
|
import { Select } from '@/ui/input/components/Select';
|
||||||
import { TextInput } from '@/ui/input/components/TextInput';
|
import { TextInput } from '@/ui/input/components/TextInput';
|
||||||
import { useIsMobile } from '@/ui/utilities/responsive/hooks/useIsMobile';
|
import { useIsMobile } from '@/ui/utilities/responsive/hooks/useIsMobile';
|
||||||
import { useEffect, useState } from 'react';
|
|
||||||
import { RelationDefinitionType } from '~/generated-metadata/graphql';
|
import { RelationDefinitionType } from '~/generated-metadata/graphql';
|
||||||
|
|
||||||
export const settingsDataModelFieldRelationFormSchema = z.object({
|
export const settingsDataModelFieldRelationFormSchema = z.object({
|
||||||
@ -41,7 +40,7 @@ export type SettingsDataModelFieldRelationFormValues = z.infer<
|
|||||||
|
|
||||||
type SettingsDataModelFieldRelationFormProps = {
|
type SettingsDataModelFieldRelationFormProps = {
|
||||||
fieldMetadataItem: Pick<FieldMetadataItem, 'type'>;
|
fieldMetadataItem: Pick<FieldMetadataItem, 'type'>;
|
||||||
objectMetadataItem: SettingsDataModelFieldPreviewCardProps['objectMetadataItem'];
|
objectMetadataItem: ObjectMetadataItem;
|
||||||
};
|
};
|
||||||
|
|
||||||
const StyledContainer = styled.div`
|
const StyledContainer = styled.div`
|
||||||
@ -84,17 +83,12 @@ export const SettingsDataModelFieldRelationForm = ({
|
|||||||
fieldMetadataItem,
|
fieldMetadataItem,
|
||||||
objectMetadataItem,
|
objectMetadataItem,
|
||||||
}: SettingsDataModelFieldRelationFormProps) => {
|
}: SettingsDataModelFieldRelationFormProps) => {
|
||||||
const {
|
const { control, watch: watchFormValue } =
|
||||||
control,
|
useFormContext<SettingsDataModelFieldRelationFormValues>();
|
||||||
watch: watchFormValue,
|
|
||||||
setValue,
|
|
||||||
} = useFormContext<SettingsDataModelFieldRelationFormValues>();
|
|
||||||
const { getIcon } = useIcons();
|
const { getIcon } = useIcons();
|
||||||
const { objectMetadataItems, findObjectMetadataItemById } =
|
const { objectMetadataItems, findObjectMetadataItemById } =
|
||||||
useFilteredObjectMetadataItems();
|
useFilteredObjectMetadataItems();
|
||||||
|
|
||||||
const [labelEditedManually, setLabelEditedManually] = useState(false);
|
|
||||||
|
|
||||||
const {
|
const {
|
||||||
disableFieldEdition,
|
disableFieldEdition,
|
||||||
disableRelationEdition,
|
disableRelationEdition,
|
||||||
@ -111,20 +105,6 @@ export const SettingsDataModelFieldRelationForm = ({
|
|||||||
);
|
);
|
||||||
|
|
||||||
const isMobile = useIsMobile();
|
const isMobile = useIsMobile();
|
||||||
const relationType = watchFormValue('relation.type');
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
if (labelEditedManually) return;
|
|
||||||
setValue(
|
|
||||||
'relation.field.label',
|
|
||||||
[
|
|
||||||
RelationDefinitionType.ManyToMany,
|
|
||||||
RelationDefinitionType.ManyToOne,
|
|
||||||
].includes(relationType)
|
|
||||||
? objectMetadataItem.labelPlural
|
|
||||||
: objectMetadataItem.labelSingular,
|
|
||||||
);
|
|
||||||
}, [labelEditedManually, objectMetadataItem, relationType, setValue]);
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<StyledContainer>
|
<StyledContainer>
|
||||||
@ -195,10 +175,7 @@ export const SettingsDataModelFieldRelationForm = ({
|
|||||||
disabled={disableFieldEdition}
|
disabled={disableFieldEdition}
|
||||||
placeholder="Field name"
|
placeholder="Field name"
|
||||||
value={value}
|
value={value}
|
||||||
onChange={(newValue) => {
|
onChange={onChange}
|
||||||
setLabelEditedManually(true);
|
|
||||||
onChange(newValue);
|
|
||||||
}}
|
|
||||||
fullWidth
|
fullWidth
|
||||||
maxLength={FIELD_NAME_MAXIMUM_LENGTH}
|
maxLength={FIELD_NAME_MAXIMUM_LENGTH}
|
||||||
/>
|
/>
|
||||||
|
|||||||
@ -60,7 +60,10 @@ export const SettingsDataModelFieldRelationSettingsFormCard = ({
|
|||||||
initialRelationObjectMetadataItem,
|
initialRelationObjectMetadataItem,
|
||||||
initialRelationType,
|
initialRelationType,
|
||||||
initialRelationFieldMetadataItem,
|
initialRelationFieldMetadataItem,
|
||||||
} = useRelationSettingsFormInitialValues({ fieldMetadataItem });
|
} = useRelationSettingsFormInitialValues({
|
||||||
|
fieldMetadataItem,
|
||||||
|
objectMetadataItem,
|
||||||
|
});
|
||||||
|
|
||||||
const relationObjectMetadataId = watchFormValue(
|
const relationObjectMetadataId = watchFormValue(
|
||||||
'relation.objectMetadataId',
|
'relation.objectMetadataId',
|
||||||
|
|||||||
Reference in New Issue
Block a user