Split components into object-metadata and object-record (#2425)
* Split components into object-metadata and object-record * Fix seed
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
import { IconComponent } from '@/ui/display/icon/types/IconComponent';
|
||||
import { FieldMetadata } from '@/ui/object/field/types/FieldMetadata';
|
||||
import { ColumnDefinition } from '@/ui/object/record-table/types/ColumnDefinition';
|
||||
import { Field } from '~/generated-metadata/graphql';
|
||||
|
||||
import { ObjectMetadataItem } from '../types/ObjectMetadataItem';
|
||||
|
||||
import { parseFieldType } from './parseFieldType';
|
||||
|
||||
export const formatFieldMetadataItemAsColumnDefinition = ({
|
||||
position,
|
||||
field,
|
||||
objectMetadataItem,
|
||||
icons,
|
||||
}: {
|
||||
position: number;
|
||||
field: Field;
|
||||
objectMetadataItem: Omit<ObjectMetadataItem, 'fields'>;
|
||||
icons: Record<string, IconComponent>;
|
||||
}): ColumnDefinition<FieldMetadata> => ({
|
||||
position,
|
||||
fieldMetadataId: field.id,
|
||||
label: field.label,
|
||||
size: 100,
|
||||
type: parseFieldType(field.type),
|
||||
metadata: {
|
||||
fieldName: field.name,
|
||||
placeHolder: field.label,
|
||||
},
|
||||
Icon: icons[field.icon ?? 'Icon123'],
|
||||
isVisible: true,
|
||||
basePathToShowPage: `/object/${objectMetadataItem.nameSingular}/`,
|
||||
});
|
||||
@@ -0,0 +1,16 @@
|
||||
import { FilterDefinition } from '@/ui/object/object-filter-dropdown/types/FilterDefinition';
|
||||
|
||||
import { ObjectMetadataItem } from '../types/ObjectMetadataItem';
|
||||
|
||||
export const formatFieldMetadataItemAsFilterDefinition = ({
|
||||
field,
|
||||
icons,
|
||||
}: {
|
||||
field: ObjectMetadataItem['fields'][0];
|
||||
icons: Record<string, any>;
|
||||
}): FilterDefinition => ({
|
||||
fieldMetadataId: field.id,
|
||||
label: field.label,
|
||||
Icon: icons[field.icon ?? 'Icon123'],
|
||||
type: 'TEXT',
|
||||
});
|
||||
@@ -0,0 +1,15 @@
|
||||
import { SortDefinition } from '@/ui/object/object-sort-dropdown/types/SortDefinition';
|
||||
|
||||
import { ObjectMetadataItem } from '../types/ObjectMetadataItem';
|
||||
|
||||
export const formatFieldMetadataItemAsSortDefinition = ({
|
||||
field,
|
||||
icons,
|
||||
}: {
|
||||
field: ObjectMetadataItem['fields'][0];
|
||||
icons: Record<string, any>;
|
||||
}): SortDefinition => ({
|
||||
fieldMetadataId: field.id,
|
||||
label: field.label,
|
||||
Icon: icons[field.icon ?? 'Icon123'],
|
||||
});
|
||||
@@ -0,0 +1,12 @@
|
||||
import toCamelCase from 'lodash.camelcase';
|
||||
|
||||
import { Field } from '~/generated-metadata/graphql';
|
||||
|
||||
export const formatFieldMetadataItemInput = (
|
||||
input: Pick<Field, 'label' | 'icon' | 'description'>,
|
||||
) => ({
|
||||
description: input.description?.trim() ?? null,
|
||||
icon: input.icon,
|
||||
label: input.label.trim(),
|
||||
name: toCamelCase(input.label.trim()),
|
||||
});
|
||||
@@ -0,0 +1,17 @@
|
||||
import toCamelCase from 'lodash.camelcase';
|
||||
|
||||
import { ObjectMetadataItem } from '../types/ObjectMetadataItem';
|
||||
|
||||
export const formatObjectMetadataItemInput = (
|
||||
input: Pick<
|
||||
ObjectMetadataItem,
|
||||
'labelPlural' | 'labelSingular' | 'icon' | 'description'
|
||||
>,
|
||||
) => ({
|
||||
description: input.description?.trim() ?? null,
|
||||
icon: input.icon,
|
||||
labelPlural: input.labelPlural.trim(),
|
||||
labelSingular: input.labelSingular.trim(),
|
||||
namePlural: toCamelCase(input.labelPlural.trim()),
|
||||
nameSingular: toCamelCase(input.labelSingular.trim()),
|
||||
});
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
import { ObjectMetadataItemsQuery } from '~/generated-metadata/graphql';
|
||||
|
||||
import { ObjectMetadataItem } from '../types/ObjectMetadataItem';
|
||||
|
||||
export const formatPagedObjectMetadataItemsToObjectMetadataItems = ({
|
||||
pagedObjectMetadataItems: pagedObjectMetadataItems,
|
||||
}: {
|
||||
pagedObjectMetadataItems: ObjectMetadataItemsQuery | undefined;
|
||||
}) => {
|
||||
const formattedObjects: ObjectMetadataItem[] =
|
||||
pagedObjectMetadataItems?.objects.edges.map((object) => ({
|
||||
...object.node,
|
||||
fields: object.node.fields.edges.map((field) => field.node),
|
||||
})) ?? [];
|
||||
|
||||
return formattedObjects;
|
||||
};
|
||||
@@ -0,0 +1,6 @@
|
||||
import toKebabCase from 'lodash.kebabcase';
|
||||
|
||||
import { Field } from '~/generated-metadata/graphql';
|
||||
|
||||
export const getFieldSlug = (metadataField: Pick<Field, 'label'>) =>
|
||||
toKebabCase(metadataField.label);
|
||||
@@ -0,0 +1,7 @@
|
||||
import toKebabCase from 'lodash.kebabcase';
|
||||
|
||||
import { ObjectMetadataItem } from '../types/ObjectMetadataItem';
|
||||
|
||||
export const getObjectSlug = (
|
||||
objectMetadataItem: Pick<ObjectMetadataItem, 'labelPlural'>,
|
||||
) => toKebabCase(objectMetadataItem.labelPlural);
|
||||
@@ -0,0 +1,44 @@
|
||||
import { FieldType } from '@/ui/object/field/types/FieldType';
|
||||
import { Field } from '~/generated/graphql';
|
||||
|
||||
export const mapFieldMetadataToGraphQLQuery = (field: Field) => {
|
||||
// TODO: parse
|
||||
const fieldType = field.type as FieldType;
|
||||
|
||||
const fieldIsSimpleValue = (
|
||||
[
|
||||
'TEXT',
|
||||
'PHONE',
|
||||
'DATE',
|
||||
'EMAIL',
|
||||
'NUMBER',
|
||||
'BOOLEAN',
|
||||
'DATE',
|
||||
] as FieldType[]
|
||||
).includes(fieldType);
|
||||
|
||||
const fieldIsURL = fieldType === 'URL' || fieldType === 'URL_V2';
|
||||
|
||||
const fieldIsMoneyAmount =
|
||||
fieldType === 'MONEY' || fieldType === 'MONEY_AMOUNT_V2';
|
||||
|
||||
if (fieldIsSimpleValue) {
|
||||
return field.name;
|
||||
} else if (fieldIsURL) {
|
||||
return `
|
||||
${field.name}
|
||||
{
|
||||
text
|
||||
link
|
||||
}
|
||||
`;
|
||||
} else if (fieldIsMoneyAmount) {
|
||||
return `
|
||||
${field.name}
|
||||
{
|
||||
amount
|
||||
currency
|
||||
}
|
||||
`;
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,14 @@
|
||||
import { FieldType } from '@/ui/object/field/types/FieldType';
|
||||
import { FieldMetadataType } from '~/generated-metadata/graphql';
|
||||
|
||||
export const parseFieldType = (fieldType: FieldMetadataType): FieldType => {
|
||||
if (fieldType === FieldMetadataType.Url) {
|
||||
return 'URL_V2';
|
||||
}
|
||||
|
||||
if (fieldType === FieldMetadataType.Money) {
|
||||
return 'MONEY_AMOUNT_V2';
|
||||
}
|
||||
|
||||
return fieldType as FieldType;
|
||||
};
|
||||
@@ -0,0 +1,4 @@
|
||||
const metadataLabelValidationPattern = /^[a-zA-Z][a-zA-Z0-9 ]*$/;
|
||||
|
||||
export const validateMetadataLabel = (value: string) =>
|
||||
!!value.match(metadataLabelValidationPattern);
|
||||
Reference in New Issue
Block a user