Skip to content

[WIP] Display friendly content type name in admin #514

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions admin/src/api/validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,8 @@ export const configContentTypeSchema = z.object({
name: z.string(),
draftAndPublish: z.boolean(),
isSingle: z.boolean(),
description: z.string(),
collectionName: z.string(),
contentTypeName: z.string(),
label: z.string(),
labelSingular: z.string(),
endpoint: z.string(),
available: z.boolean(),
visible: z.boolean(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ export const NavigationItemForm: React.FC<NavigationItemFormProps> = ({
);

if (!selectedEntity) {
return contentTypes.find((_) => _.uid === relatedType)?.contentTypeName;
return contentTypes.find((_) => _.uid === relatedType)?.label;
}
} else {
const entity = contentTypeItemsQuery.data?.find(({ documentId }) => documentId === related);
Expand Down Expand Up @@ -402,7 +402,7 @@ export const NavigationItemForm: React.FC<NavigationItemFormProps> = ({
return {
key: item.uid,
value: item.uid,
label: item.contentTypeName,
label: item.label,
};
}),
(item) => item.key
Expand Down Expand Up @@ -751,7 +751,7 @@ export const NavigationItemForm: React.FC<NavigationItemFormProps> = ({
>
{configQuery.data?.contentTypes.map((contentType) => (
<SingleSelectOption key={contentType.uid} value={contentType.uid}>
{contentType.contentTypeName}
{contentType.label}
</SingleSelectOption>
))}
</SingleSelect>
Expand Down
2 changes: 1 addition & 1 deletion admin/src/pages/HomePage/utils/parsers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ export const extractRelatedItemLabel = (
const contentType = contentTypes.find(({ uid }) => uid === __collectionUid);

if (contentType?.isSingle) {
return contentType.labelSingular;
return contentType.label;
}

const defaultFieldsWithCapitalizedOptions = [
Expand Down
5 changes: 1 addition & 4 deletions server/src/dtos/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,14 @@ export type ConfigContentTypeDTO = {
name: string;
draftAndPublish: boolean | undefined;
isSingle: boolean;
description: string;
collectionName: string;
contentTypeName: string;
label: string;
relatedField: string | undefined;
templateName: string | undefined;
available: boolean | undefined;
labelSingular: string;
endpoint: string;
plugin: string | undefined;
visible: boolean;
gqlTypeName: string | undefined;
};

export type NavigationPluginConfigDTO = Pick<
Expand Down
13 changes: 8 additions & 5 deletions server/src/graphql/types/content-types.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
export default ({ nexus }: any) =>
import type * as nexusType from 'nexus';
import { ConfigContentTypeDTO } from '../../dtos';

export default ({ nexus }: { nexus: typeof nexusType }) =>
nexus.objectType({
name: 'ContentTypes',
definition(t: any) {
definition(t) {
t.nonNull.string('uid');
t.nonNull.string('name');
t.nonNull.boolean('isSingle');
t.nonNull.string('collectionName');
t.nonNull.string('contentTypeName');
t.nonNull.string('label');
t.nonNull.string('contentTypeName', { deprecation: 'use name', resolve: (t: ConfigContentTypeDTO) => t.name });
t.nonNull.string('label', { deprecation: 'use name', resolve: (t: ConfigContentTypeDTO) => t.name });
t.nonNull.string('relatedField');
t.nonNull.string('labelSingular');
t.nonNull.string('labelSingular', { deprecation: 'use name', resolve: (t: ConfigContentTypeDTO) => t.name });
t.nonNull.string('endpoint');
t.nonNull.boolean('available');
t.nonNull.boolean('visible');
Expand Down
31 changes: 10 additions & 21 deletions server/src/services/admin/admin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ import {
getPluginModels,
getPluginService,
isContentTypeEligible,
singularize,
validateAdditionalFields,
} from '../../utils';
import {
Expand Down Expand Up @@ -79,7 +78,7 @@ const adminService = (context: { strapi: Core.Strapi }) => ({
preferCustomContentTypes,
} = config;

const isGQLPluginEnabled = !!strapi.plugin('graphql');
const isGQLPluginEnabled = !!context.strapi.plugin('graphql');

let extendedResult: Record<string, unknown> = {
allowedContentTypes: ALLOWED_CONTENT_TYPES,
Expand All @@ -89,7 +88,7 @@ const adminService = (context: { strapi: Core.Strapi }) => ({
const configContentTypes = await this.configContentTypes({});

const result = {
contentTypes: await this.configContentTypes({ viaSettingsPage }),
contentTypes: configContentTypes,
contentTypesNameFields: {
default: CONTENT_TYPES_NAME_FIELDS_DEFAULTS,
...(isObject(contentTypesNameFields) ? contentTypesNameFields : {}),
Expand All @@ -101,9 +100,7 @@ const adminService = (context: { strapi: Core.Strapi }) => ({
? additionalFields
: additionalFields.filter((field) => typeof field === 'string' || !!field.enabled),
gql: {
navigationItemRelated: configContentTypes.map(({ labelSingular }) =>
labelSingular.replace(/\s+/g, '')
),
navigationItemRelated: isGQLPluginEnabled ? configContentTypes.map((contentType) => contentType.gqlTypeName!) : [],
},
isGQLPluginEnabled: viaSettingsPage ? isGQLPluginEnabled : undefined,
cascadeMenuAttached,
Expand Down Expand Up @@ -136,6 +133,9 @@ const adminService = (context: { strapi: Core.Strapi }) => ({

const config = await pluginStore.get({ key: 'config' }).then(configSchema.parse);

const gqlPlugin = context.strapi.plugin('graphql');
const graphQlNamingPlugin = gqlPlugin?.service('utils').naming;

const eligibleContentTypes = await Promise.all(
config.contentTypes
.filter(
Expand Down Expand Up @@ -211,7 +211,6 @@ const adminService = (context: { strapi: Core.Strapi }) => ({
}

const { visible = true } = pluginOptions['content-manager'] || {};
const { name = '', description = '' } = info;

const findRouteConfig = find(
get(context.strapi.api, `[${modelName}].config.routes`, []),
Expand All @@ -224,32 +223,22 @@ const adminService = (context: { strapi: Core.Strapi }) => ({
findRoutePath && findRoutePath !== apiName ? findRoutePath : apiName || modelName;
const isSingle = kind === KIND_TYPES.SINGLE;
const endpoint = isSingle ? apiPath : pluralize(apiPath!);
const relationName = singularize(modelName);
const relationNameParts = typeof uid === 'string' ? last(uid.split('.'))!.split('-') : [];
const contentTypeName =
relationNameParts.length > 1
? relationNameParts.reduce((prev, curr) => `${prev}${upperFirst(curr)}`, '')
: upperFirst(modelName);
const labelSingular =
name ||
upperFirst(relationNameParts.length > 1 ? relationNameParts.join(' ') : relationName);

const gqlTypeName: string | undefined = graphQlNamingPlugin?.(item);

acc.push({
uid,
name: relationName,
name: info.displayName,
draftAndPublish: options?.draftAndPublish,
isSingle,
description,
collectionName,
contentTypeName,
label: isSingle ? labelSingular : pluralize(name || labelSingular),
relatedField: relatedField ? relatedField.alias : undefined,
labelSingular: singularize(labelSingular),
endpoint: endpoint!,
plugin,
available: isAvailable,
visible,
templateName: options?.templateName,
gqlTypeName,
});

return acc;
Expand Down
4 changes: 0 additions & 4 deletions server/src/utils/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,3 @@ export const isContentTypeEligible = (uid = '') => {

return !!uid && isOneOfAllowedType && isNoneOfRestricted;
};

export const singularize = (value = '') => {
return last(value) === 's' ? value.substr(0, value.length - 1) : value;
};
13 changes: 0 additions & 13 deletions server/tests/utils/functions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
isContentTypeEligible,
resolveGlobalLikeId,
RESTRICTED_CONTENT_TYPES,
singularize,
validateAdditionalFields,
} from '../../src/utils';
import { asProxy } from '../utils';
Expand Down Expand Up @@ -188,18 +187,6 @@ describe('Navigation', () => {
});
});

describe('singularize()', () => {
it('should singularize plural string', () => {
// Then
expect(singularize('blogs')).toEqual('blog');
});

it('should leave singular string as is', () => {
// Then
expect(singularize('blog')).toEqual('blog');
});
});

describe('isContentTypeEligible()', () => {
it.each(RESTRICTED_CONTENT_TYPES)('%s should be disallowed', (contentType) => {
// Then
Expand Down