Skip to content

Commit

Permalink
Merge pull request #32 from VirtusLab/fix/render-to-use-uid
Browse files Browse the repository at this point in the history
fix: render should use uid instead of content type name
  • Loading branch information
cyp3rius authored Feb 23, 2021
2 parents e1d7979 + b50395d commit 888f259
Show file tree
Hide file tree
Showing 9 changed files with 223 additions and 85 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ Complete installation requirements are exact same as for Strapi itself and can b

**Supported Strapi versions**:

- Strapi v3.4.3 (recently tested)
- Strapi v3.5.0 (recently tested)
- Strapi v3.x

(This plugin may work with the older Strapi versions, but these are not tested nor officially supported at this time.)
Expand Down
1 change: 1 addition & 0 deletions __mocks__/helpers/blog-post.settings.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"uid": "application::blog-post.blog-post",
"kind": "collectionType",
"collectionName": "blog_posts",
"info": {
Expand Down
1 change: 1 addition & 0 deletions __mocks__/helpers/page.settings.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"uid": "application::page.page",
"kind": "collectionType",
"collectionName": "pages",
"info": {
Expand Down
2 changes: 1 addition & 1 deletion admin/src/containers/View/utils/parsers.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export const transformItemToRESTPayload = (
{
refId: relatedId,
ref: relatedContentType ? relatedContentType.name : relatedType,
field: relatedContentType ? relatedContentType.relatedField : 'navigation',
field: relatedContentType && relatedContentType.relatedField ? relatedContentType.relatedField : 'navigation',
},
],
items: items.map((iItem) => transformItemToRESTPayload(iItem, id, master, config)),
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
"reactstrap": "8.4.1",
"redux-saga": "^0.16.0",
"request": "^2.83.0",
"strapi-helper-plugin": "3.4.3",
"strapi-utils": "3.4.3",
"strapi-helper-plugin": "3.5.0",
"strapi-utils": "3.5.0",
"uuidv4": "^6.2.6",
"slugify": "^1.4.5",
"pluralize": "^8.0.0"
Expand Down
2 changes: 2 additions & 0 deletions services/__tests__/navigation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ describe('Navigation service', () => {
it('Config Content Types', () => {
const { configContentTypes } = require("../navigation");
const result = [{
uid: "application::page.page",
collectionName: "pages",
isSingle: false,
contentTypeName: "Page",
Expand All @@ -21,6 +22,7 @@ describe('Navigation service', () => {
name: "page",
visible: true,
}, {
uid: "application::blog-post.blog-post",
collectionName: "blog_posts",
isSingle: false,
contentTypeName: "BlogPost",
Expand Down
15 changes: 8 additions & 7 deletions services/navigation.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ module.exports = {
)
.map((key) => {
const item = strapi.contentTypes[key];
const relatedField = (item.associations || []).find(_ => _.collection === 'navigationitem');
const { options, info, collectionName, apiName, plugin, kind } = item;
const relatedField = (item.associations || []).find(_ => _.model === 'navigationitem');
const { uid, options, info, collectionName, apiName, plugin, kind } = item;
const { name, description } = info;
const { isManaged, hidden } = options;
const isSingle = kind === KIND_TYPES.SINGLE;
Expand All @@ -104,6 +104,7 @@ module.exports = {
const contentTypeName = relationNameParts.length > 1 ? relationNameParts.reduce((prev, curr) => `${prev}${upperFirst(curr)}`, '') : upperFirst(relationName);
const labelSingular = upperFirst(relationNameParts.length > 1 ? relationNameParts.join(' ') : relationName);
return {
uid,
name: relationName,
isSingle,
description,
Expand Down Expand Up @@ -235,7 +236,7 @@ module.exports = {
if (!items) {
return [];
}
const getTemplateName = await utilsFunctions.templateNameFactory(items, strapi)
const getTemplateName = await utilsFunctions.templateNameFactory(items, strapi, service.configContentTypes())

switch (type) {
case renderType.TREE:
Expand All @@ -244,7 +245,7 @@ module.exports = {
const isExternal = item.type === itemType.EXTERNAL;
const parentPath = isExternal ? undefined : `${path === '/' ? '' : path}/${item.path === '/' ? '' : item.path}`;
const slug = isString(parentPath) ? slugify((first(parentPath) === '/' ? parentPath.substring(1) : parentPath).replace(/\//g, '-')) : undefined;
const firstRelated = first(item.related);
const lastRelated = last(item.related);
return {
title: item.title,
menuAttached: item.menuAttached,
Expand All @@ -253,9 +254,9 @@ module.exports = {
uiRouterKey: item.uiRouterKey,
slug: !slug && item.uiRouterKey ? slugify(item.uiRouterKey) : slug,
external: isExternal,
related: isExternal || !firstRelated ? undefined : {
...firstRelated,
__templateName: getTemplateName(firstRelated.__contentType, firstRelated.id),
related: isExternal || !lastRelated ? undefined : {
...lastRelated,
__templateName: getTemplateName(lastRelated.__contentType, lastRelated.id),
},
audience: !isEmpty(item.audience) ? item.audience.map(aItem => aItem.key) : undefined,
items: isExternal ? undefined : service.renderTree(
Expand Down
22 changes: 14 additions & 8 deletions services/utils/functions.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
const {
isEmpty,
isObject,
first,
find,
flatten,
get,
kebabCase
kebabCase,
last,
} = require('lodash');

const { NavigationError } = require('../../utils/NavigationError');
Expand Down Expand Up @@ -68,6 +71,7 @@ module.exports = {
.map(entity => {
return ({
...entity,
related: !isEmpty(entity.related) ? [last(entity.related)] : entity.related,
items: this.buildNestedStructure(entities, entity.id, field),
});
});
Expand All @@ -78,7 +82,7 @@ module.exports = {
return componentName ? strapi.components[componentName] : null;
},

async templateNameFactory(items, strapi) {
async templateNameFactory(items, strapi, contentTypes = []) {
const flatRelated = flatten(items.map(i => i.related));
const relatedMap = flatRelated.reduce((acc, curr) => {
if (!acc[curr.__contentType]) {
Expand All @@ -89,12 +93,14 @@ module.exports = {
}, {});
const responses = await Promise.all(
Object.entries(relatedMap)
.map(
([contentType, ids]) => {
return strapi.query(kebabCase(contentType))
.find({ id_in: ids })
.then(res => ({ [contentType]: res }));
}),
.map(
([contentType, ids]) => {
const contentTypeName = kebabCase(contentType);
const contentTypeUid = get(find(contentTypes, cnt => cnt.contentTypeName === contentTypeName), 'uid') || contentTypeName;
return strapi.query(contentTypeUid)
.find({ id_in: ids })
.then(res => ({ [contentType]: res }))
}),
);
const relatedResponseMap = responses.reduce((acc, curr) => ({ ...acc, ...curr }), {});

Expand Down
Loading

0 comments on commit 888f259

Please sign in to comment.