Skip to content

Commit

Permalink
feat(core): Implement localeText custom field type
Browse files Browse the repository at this point in the history
Closes #2000
  • Loading branch information
michaelbromley committed Feb 23, 2023
1 parent efe23d1 commit 6a3c61f
Show file tree
Hide file tree
Showing 13 changed files with 711 additions and 651 deletions.
513 changes: 288 additions & 225 deletions docs/content/developer-guide/customizing-models.md

Large diffs are not rendered by default.

258 changes: 147 additions & 111 deletions packages/admin-ui/src/lib/core/src/common/generated-types.ts

Large diffs are not rendered by default.

540 changes: 231 additions & 309 deletions packages/admin-ui/src/lib/core/src/common/introspection-result.ts

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export function createUpdatedTranslatable<T extends { translations: any[] } & Ma
if (customFieldConfig && updatedFields.hasOwnProperty('customFields')) {
for (const field of customFieldConfig) {
const value = updatedFields.customFields[field.name];
if (field.type === 'localeString') {
if (field.type === 'localeString' || field.type === 'localeText') {
newTranslatedCustomFields[field.name] = value;
} else {
newCustomFields[field.name] =
Expand Down Expand Up @@ -64,6 +64,7 @@ function getDefaultValue(type: CustomFieldType): any {
case 'localeString':
case 'string':
case 'text':
case 'localeText':
return '';
case 'boolean':
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,12 @@ export const TEXT_CUSTOM_FIELD_FRAGMENT = gql`
}
${CUSTOM_FIELD_CONFIG_FRAGMENT}
`;
export const LOCALE_TEXT_CUSTOM_FIELD_FRAGMENT = gql`
fragment LocaleTextCustomField on LocaleTextCustomFieldConfig {
...CustomFieldConfig
}
${CUSTOM_FIELD_CONFIG_FRAGMENT}
`;
export const BOOLEAN_CUSTOM_FIELD_FRAGMENT = gql`
fragment BooleanCustomField on BooleanCustomFieldConfig {
...CustomFieldConfig
Expand Down Expand Up @@ -677,6 +683,9 @@ export const ALL_CUSTOM_FIELDS_FRAGMENT = gql`
... on TextCustomFieldConfig {
...TextCustomField
}
... on LocaleTextCustomFieldConfig {
...LocaleTextCustomField
}
... on BooleanCustomFieldConfig {
...BooleanCustomField
}
Expand All @@ -701,6 +710,7 @@ export const ALL_CUSTOM_FIELDS_FRAGMENT = gql`
${FLOAT_CUSTOM_FIELD_FRAGMENT}
${DATE_TIME_CUSTOM_FIELD_FRAGMENT}
${RELATION_CUSTOM_FIELD_FRAGMENT}
${LOCALE_TEXT_CUSTOM_FIELD_FRAGMENT}
`;

export const GET_SERVER_CONFIG = gql`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,8 @@ export class DynamicFormInputComponent
return { component: 'text-form-input' };
}
}
case 'text': {
case 'text':
case 'localeText': {
return { component: 'textarea-form-input' };
}
case 'int':
Expand Down
4 changes: 3 additions & 1 deletion packages/common/src/shared-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ export type ID = string | number;
* string | varchar | String
* localeString | varchar | String
* text | longtext(m), text(p,s) | String
* localText | longtext(m), text(p,s) | String
* int | int | Int
* float | double precision | Float
* boolean | tinyint (m), bool (p), boolean (s) | Boolean
Expand All @@ -106,7 +107,8 @@ export type CustomFieldType =
| 'boolean'
| 'datetime'
| 'relation'
| 'text';
| 'text'
| 'localeText';

/**
* @description
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ function validateSingleValue(config: CustomFieldConfig, value: any) {
case 'boolean':
case 'relation':
case 'text':
case 'localeText':
break;
default:
assertNever(config);
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/api/config/generate-resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ export function generateResolvers(
return 'LocaleStringCustomFieldConfig';
case 'text':
return 'TextCustomFieldConfig';
case 'localeText':
return 'LocaleTextCustomFieldConfig';
case 'int':
return 'IntCustomFieldConfig';
case 'float':
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/api/config/graphql-custom-fields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,7 @@ function getFilterOperator(config: CustomFieldConfig): string | undefined {
case 'string':
case 'localeString':
case 'text':
case 'localeText':
return config.list ? 'StringListOperators' : 'StringOperators';
case 'boolean':
return config.list ? 'BooleanListOperators' : 'BooleanOperators';
Expand Down Expand Up @@ -524,6 +525,7 @@ function getGraphQlType(config: CustomFieldConfig): string {
case 'string':
case 'localeString':
case 'text':
case 'localeText':
return 'String';
case 'datetime':
return 'DateTime';
Expand Down
13 changes: 13 additions & 0 deletions packages/core/src/api/schema/common/custom-field-types.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,18 @@ type TextCustomFieldConfig implements CustomField {
ui: JSON
}

type LocaleTextCustomFieldConfig implements CustomField {
name: String!
type: String!
list: Boolean!
label: [LocalizedString!]
description: [LocalizedString!]
readonly: Boolean
internal: Boolean
nullable: Boolean
ui: JSON
}

type LocalizedString {
languageCode: LanguageCode!
value: String!
Expand All @@ -141,3 +153,4 @@ union CustomFieldConfig =
| DateTimeCustomFieldConfig
| RelationCustomFieldConfig
| TextCustomFieldConfig
| LocaleTextCustomFieldConfig
8 changes: 7 additions & 1 deletion packages/core/src/config/custom-field/custom-field-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
FloatCustomFieldConfig as GraphQLFloatCustomFieldConfig,
IntCustomFieldConfig as GraphQLIntCustomFieldConfig,
LocaleStringCustomFieldConfig as GraphQLLocaleStringCustomFieldConfig,
LocaleTextCustomFieldConfig as GraphQLLocaleTextCustomFieldConfig,
LocalizedString,
RelationCustomFieldConfig as GraphQLRelationCustomFieldConfig,
StringCustomFieldConfig as GraphQLStringCustomFieldConfig,
Expand All @@ -23,7 +24,7 @@ import { VendureEntity } from '../../entity/base/base.entity';

// prettier-ignore
export type DefaultValueType<T extends CustomFieldType> =
T extends 'string' | 'localeString' ? string :
T extends 'string' | 'localeString' | 'text' | 'localeText' ? string :
T extends 'int' | 'float' ? number :
T extends 'boolean' ? boolean :
T extends 'datetime' ? Date :
Expand Down Expand Up @@ -84,6 +85,10 @@ export type LocaleStringCustomFieldConfig = TypedCustomFieldConfig<
GraphQLLocaleStringCustomFieldConfig
>;
export type TextCustomFieldConfig = TypedCustomFieldConfig<'text', GraphQLTextCustomFieldConfig>;
export type LocaleTextCustomFieldConfig = TypedCustomFieldConfig<
'localeText',
GraphQLLocaleTextCustomFieldConfig
>;
export type IntCustomFieldConfig = TypedCustomFieldConfig<'int', GraphQLIntCustomFieldConfig>;
export type FloatCustomFieldConfig = TypedCustomFieldConfig<'float', GraphQLFloatCustomFieldConfig>;
export type BooleanCustomFieldConfig = TypedCustomFieldConfig<'boolean', GraphQLBooleanCustomFieldConfig>;
Expand All @@ -103,6 +108,7 @@ export type CustomFieldConfig =
| StringCustomFieldConfig
| LocaleStringCustomFieldConfig
| TextCustomFieldConfig
| LocaleTextCustomFieldConfig
| IntCustomFieldConfig
| FloatCustomFieldConfig
| BooleanCustomFieldConfig
Expand Down
5 changes: 3 additions & 2 deletions packages/core/src/entity/register-custom-entity-fields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,11 @@ function registerCustomFieldsForEntity(
};

if (translation) {
if (customField.type === 'localeString') {
if (customField.type === 'localeString' || customField.type === 'localeText') {
registerColumn();
}
} else {
if (customField.type !== 'localeString') {
if (customField.type !== 'localeString' && customField.type !== 'localeText') {
registerColumn();
}
}
Expand Down Expand Up @@ -193,6 +193,7 @@ function getColumnType(
case 'localeString':
return 'varchar';
case 'text':
case 'localeText':
switch (dbEngine) {
case 'mysql':
case 'mariadb':
Expand Down

0 comments on commit 6a3c61f

Please sign in to comment.