Skip to content

Commit

Permalink
feat(admin-ui): Support custom fields on custom entities
Browse files Browse the repository at this point in the history
Relates to #1848
  • Loading branch information
michaelbromley committed Jan 25, 2024
1 parent 2bef6ca commit 74aeb86
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 95 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ export class TypedBaseListComponent<
this.dataService.client.setContentLanguage(code).subscribe();
}

getCustomFieldConfig(key: Exclude<keyof CustomFields, '__typename'>): CustomFieldConfig[] {
getCustomFieldConfig(key: Exclude<keyof CustomFields, '__typename'> | string): CustomFieldConfig[] {
return this.serverConfigService.getCustomFieldsFor(key);
}
}
4 changes: 2 additions & 2 deletions packages/admin-ui/src/lib/core/src/common/generated-types.ts

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/admin-ui/src/lib/core/src/core.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export class CoreModule {

const lastLanguage = this.localStorageService.get('uiLanguageCode');
const availableLanguages = getAppConfig().availableLanguages;
const availableLocales = getAppConfig().availableLocales;
const availableLocales = getAppConfig().availableLocales ?? [defaultLocale];

if (!!defaultLanguage && !availableLanguages.includes(defaultLanguage)) {
throw new Error(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -696,86 +696,9 @@ export const GET_SERVER_CONFIG = gql`
description
assignable
}
customFieldConfig {
Address {
...CustomFields
}
Administrator {
...CustomFields
}
Asset {
...CustomFields
}
Channel {
...CustomFields
}
Collection {
...CustomFields
}
Customer {
...CustomFields
}
CustomerGroup {
...CustomFields
}
Facet {
...CustomFields
}
FacetValue {
...CustomFields
}
Fulfillment {
...CustomFields
}
GlobalSettings {
...CustomFields
}
Order {
...CustomFields
}
OrderLine {
...CustomFields
}
PaymentMethod {
...CustomFields
}
Product {
...CustomFields
}
ProductOption {
...CustomFields
}
ProductOptionGroup {
...CustomFields
}
ProductVariant {
...CustomFields
}
Promotion {
...CustomFields
}
Region {
...CustomFields
}
Seller {
...CustomFields
}
ShippingMethod {
...CustomFields
}
StockLocation {
...CustomFields
}
TaxCategory {
...CustomFields
}
TaxRate {
...CustomFields
}
User {
...CustomFields
}
Zone {
entityCustomFields {
entityName
customFields {
...CustomFields
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { DocumentNode } from 'graphql/language/ast';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';

import { CustomFields } from '../../common/generated-types';
import { CustomFieldConfig } from '../../common/generated-types';
import { QueryResult } from '../query-result';
import { ServerConfigService } from '../server-config';
import { addCustomFields } from '../utils/add-custom-fields';
Expand All @@ -21,8 +21,8 @@ import { transformRelationCustomFieldInputs } from '../utils/transform-relation-
export class BaseDataService {
constructor(private apollo: Apollo, private serverConfigService: ServerConfigService) {}

private get customFields(): CustomFields {
return this.serverConfigService.serverConfig.customFieldConfig || {};
private get customFields(): Map<string, CustomFieldConfig[]> {
return this.serverConfigService.customFieldsMap;
}

/**
Expand Down
13 changes: 8 additions & 5 deletions packages/admin-ui/src/lib/core/src/data/server-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ export function initializeServerConfigService(serverConfigService: ServerConfigS
*/
@Injectable()
export class ServerConfigService {
private _serverConfig: ServerConfig = {} as any;

private _serverConfig: GetServerConfigQuery['globalSettings']['serverConfig'] = {} as any;
customFieldsMap: Map<string, CustomFieldConfig[]> = new Map();
private get baseDataService() {
return this.injector.get<BaseDataService>(BaseDataService);
}
Expand All @@ -49,6 +49,9 @@ export class ServerConfigService {
).then(
result => {
this._serverConfig = result.globalSettings.serverConfig;
for (const entityCustomFields of this._serverConfig.entityCustomFields) {
this.customFieldsMap.set(entityCustomFields.entityName, entityCustomFields.customFields);
}
},
err => {
// Let the error fall through to be caught by the http interceptor.
Expand All @@ -73,8 +76,8 @@ export class ServerConfigService {
/**
* Retrieves the custom field configs for the given entity type.
*/
getCustomFieldsFor(type: Exclude<keyof CustomFields, '__typename'>): CustomFieldConfig[] {
return this.serverConfig.customFieldConfig[type] || [];
getCustomFieldsFor(type: Exclude<keyof CustomFields, '__typename'> | string): CustomFieldConfig[] {
return this.customFieldsMap.get(type) || [];
}

getOrderProcessStates(): OrderProcessState[] {
Expand All @@ -89,7 +92,7 @@ export class ServerConfigService {
return this.serverConfig.permissions;
}

get serverConfig(): ServerConfig {
get serverConfig(): GetServerConfigQuery['globalSettings']['serverConfig'] {
return this._serverConfig;
}
}
14 changes: 11 additions & 3 deletions packages/admin-ui/src/lib/core/src/data/utils/add-custom-fields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,21 @@ import {
SelectionNode,
} from 'graphql';

import { CustomFields, RelationCustomFieldFragment } from '../../common/generated-types';
import {
CustomFieldConfig,
CustomFields,
EntityCustomFields,
RelationCustomFieldFragment,
} from '../../common/generated-types';

/**
* Given a GraphQL AST (DocumentNode), this function looks for fragment definitions and adds and configured
* custom fields to those fragments.
*/
export function addCustomFields(documentNode: DocumentNode, customFields: CustomFields): DocumentNode {
export function addCustomFields(
documentNode: DocumentNode,
customFields: Map<string, CustomFieldConfig[]>,
): DocumentNode {
const fragmentDefs = documentNode.definitions.filter(isFragmentDefinition);

for (const fragmentDef of fragmentDefs) {
Expand All @@ -33,7 +41,7 @@ export function addCustomFields(documentNode: DocumentNode, customFields: Custom
entityType = 'Region';
}

const customFieldsForType = customFields[entityType];
const customFieldsForType = customFields.get(entityType);
if (customFieldsForType && customFieldsForType.length) {
(fragmentDef.selectionSet.selections as SelectionNode[]).push({
name: {
Expand Down

0 comments on commit 74aeb86

Please sign in to comment.