Skip to content

Commit

Permalink
Merge pull request #5 from RedTurtle/get_items_by_path
Browse files Browse the repository at this point in the history
refactor: get items by path
  • Loading branch information
giuliaghisini authored Mar 25, 2021
2 parents 88066c3 + 4ce2e2b commit 8d2229a
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 9 deletions.
13 changes: 5 additions & 8 deletions src/components/FooterColumns.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { flattenHTMLToAppURL, flattenToAppURL } from '@plone/volto/helpers';
import { ConditionalLink } from '@plone/volto/components';

import { getEditableFooterColumns } from '../actions';
import { getItemsByPath } from '../utils';
import Socials from './Socials';
import NewsletterSubscribe from './NewsletterSubscribe';

Expand All @@ -25,14 +26,10 @@ const FooterColumns = ({ footer }) => {
}, [dispatch]);

//filter rootpaths
const footerColumns =
footerConfiguration
.filter((f) =>
(location?.pathname?.length ? location.pathname : '/').match(
new RegExp(flattenToAppURL(f.rootPath)),
),
)
.pop()?.items ?? [];
const footerColumns = getItemsByPath(
footerConfiguration,
location?.pathname?.length ? location.pathname : '/',
);

const ncolumns =
footerColumns.length < N_COLUMNS ? footerColumns.length : N_COLUMNS;
Expand Down
8 changes: 7 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import { editableFooterColumnsReducer } from './reducers';
import FooterConfigurationWidget from './widget/FooterConfigurationWidget';
import { getEditableFooterColumns } from './actions';
import { getItemsByPath } from './utils';
import FooterColumns from './components/FooterColumns';

export { FooterConfigurationWidget, getEditableFooterColumns, FooterColumns };
export {
FooterConfigurationWidget,
getEditableFooterColumns,
FooterColumns,
getItemsByPath,
};

export default (config) => {
config.widgets.id = {
Expand Down
19 changes: 19 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export function getItemsByPath(items, pathname) {
let rootPathConfig = null;
const itemsByPath = items?.reduce((acc, val) => {
if (val.rootPath === '/') {
rootPathConfig = val;
return acc;
}
return { ...acc, [val.rootPath]: val };
}, {});
const matchingPaths = Object.keys(itemsByPath)
.filter((path) => pathname.startsWith(path))
.sort((a, b) => {
return a.length < b.length;
});

if (matchingPaths.length > 0) return itemsByPath[matchingPaths[0]].items;
else if (rootPathConfig) return rootPathConfig.items;
else return [];
}

0 comments on commit 8d2229a

Please sign in to comment.