Skip to content

Commit

Permalink
Merge pull request #1 from mstrag/feat/update-template-name
Browse files Browse the repository at this point in the history
feat: update templateName - to be taken from settings.json options.te…
  • Loading branch information
cyp3rius authored Oct 8, 2020
2 parents 52d7f5e + 54ddbb8 commit c226b6f
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 21 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "strapi-plugin-navigation",
"version": "1.0.0-beta.3",
"version": "1.0.0-beta.4",
"description": "Strapi - Navigation plugin",
"strapi": {
"name": "Navigation",
Expand Down
77 changes: 57 additions & 20 deletions services/navigation.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const {
isNumber,
find,
first,
flatten,
get,
upperFirst,
isString,
Expand Down Expand Up @@ -51,11 +52,42 @@ const excludedContentTypes = get(

const buildNestedStructure = (entities, id = null, field = 'parent') =>
entities
.filter(entity => (entity[field] === id) || (isObject(entity[field]) && (entity[field].id === id)))
.map(entity => ({
...entity,
items: buildNestedStructure(entities, entity.id, field),
}));
.filter(entity => (entity[field] === id) || (isObject(entity[field]) && (entity[field].id === id)))
.map(entity => ({
...entity,
items: buildNestedStructure(entities, entity.id, field),
}));

const getTemplateComponentFromTemplate = (
template,
) => {
if (template && template[0] && template[0].__component) {
const componentName = template[0].__component;
return strapi.components[componentName];
}
throw new Error(400, "Template field is incompatible");
};

const templateNameFactory = async (items, strapi) => {
const flatRelated = flatten(items.map(i => i.related));
const relatedMap = flatRelated.reduce((acc, curr) => {
if(!acc[curr.__contentType]) {
acc[curr.__contentType] = []
}
acc[curr.__contentType].push(curr.id)
return acc;
}, {})
const responses = await Promise.all(
Object.entries(relatedMap)
.map(([contentType, ids]) => strapi.query(contentType).find({ id_in: ids }).then(res => ({[contentType]: res})))
)
const relatedResponseMap = responses.reduce((acc, curr) => ({...acc, ...curr}), {});

return (contentType, id) => {
const template = get(relatedResponseMap[contentType].find(data => data.id === id), 'template');
return getTemplateComponentFromTemplate(template).options.templateName
}
}

module.exports = {
// Get plugin configuration
Expand Down Expand Up @@ -86,7 +118,7 @@ module.exports = {
};
},

configContentTypes: () =>
configContentTypes: () =>
Object.keys(strapi.contentTypes)
.filter(
(key) =>
Expand Down Expand Up @@ -207,6 +239,7 @@ module.exports = {
if (!items) {
return [];
}
const getTemplateName = await templateNameFactory(items, strapi)

switch (type) {
case renderType.TREE:
Expand All @@ -215,15 +248,19 @@ 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);
return {
title: item.title,
menuAttached: item.menuAttached,
path: isExternal ? item.externalPath : parentPath,
type: item.type,
uiRouterKey: item.uiRouterKey,
slug: !slug && item.uiRouterKey ? slugify(item.uiRouterKey) : slug,
slug: !slug && item.uiRouterKey ? slugify(item.uiRouterKey) : slug,
external: isExternal,
related: isExternal ? undefined : first(item.related),
related: isExternal ? undefined : {
...firstRelated,
__templateName: getTemplateName(firstRelated.__contentType, firstRelated.id),
},
audience: !isEmpty(item.audience) ? item.audience.map(aItem => aItem.key) : undefined,
items: isExternal ? undefined : service.renderTree(
items,
Expand Down Expand Up @@ -331,14 +368,14 @@ module.exports = {
renderRFRPage: (item, parent) => {
const { service } = extractMeta(strapi.plugins);
const { uiRouterKey, title, path, slug, related, type, audience, menuAttached } = item;
const { __contentType, id } = related || {};
const { __contentType, id, __templateName } = related || {};
const contentTypes = service.configContentTypes();
const contentType = (__contentType || '').toLowerCase() || undefined;
const { collectionName } = find(contentTypes, ctItem => ctItem.name === contentType) || {};
return {
id: uiRouterKey,
title,
templateName: `${collectionName}:${id}`,
templateName: __templateName,
related: type === navigationItem.type.INTERNAL ? {
contentType,
collectionName,
Expand Down Expand Up @@ -381,10 +418,10 @@ module.exports = {
});
return !isEmpty(item.items)
? service.createBranch(
item.items,
masterEntity,
sanitizeEntity(navigationItem, { model: itemModel }),
)
item.items,
masterEntity,
sanitizeEntity(navigationItem, { model: itemModel }),
)
: null;
}),
);
Expand All @@ -402,8 +439,8 @@ module.exports = {
.delete({ id });
return !isEmpty(item.items)
? service.removeBranch(
item.items,
)
item.items,
)
: null;
}),
);
Expand Down Expand Up @@ -443,10 +480,10 @@ module.exports = {
}
return !isEmpty(items)
? service.analyzeBranch(
items,
masterEntity,
sanitizeEntity(currentItem, { model: itemModel }),
)
items,
masterEntity,
sanitizeEntity(currentItem, { model: itemModel }),
)
: null;
}),
),
Expand Down

0 comments on commit c226b6f

Please sign in to comment.