Skip to content

Commit

Permalink
Fix the condition deciding on listing pagination format so it takes i…
Browse files Browse the repository at this point in the history
…nto account container blocks as well (#4978)
  • Loading branch information
sneridagh authored and ichim-david committed Feb 27, 2024
1 parent 96ac64f commit 511a667
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 7 deletions.
1 change: 1 addition & 0 deletions news/4978.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix the condition deciding on listing pagination format so it takes into account container blocks as well @sneridagh
1 change: 1 addition & 0 deletions src/config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ let config = {
querystringSearchGet: false,
blockSettingsTabFieldsetsInitialStateOpen: true,
excludeLinksAndReferencesMenuItem: true,
containerBlockTypes: [],
},
experimental: {
addBlockButton: {
Expand Down
22 changes: 22 additions & 0 deletions src/helpers/Blocks/Blocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -533,3 +533,25 @@ export const getPreviousNextBlock = ({ content, block }) => {

return [previousBlock, nextBlock];
};

/**
* Given a `block` object and a list of block types, return a list of block ids matching the types
*
* @function findBlocks
* @param {Object} types A list with the list of types to be matched
* @return {Array} An array of block ids
*/
export function findBlocks(blocks, types, result = []) {
const containerBlockTypes = config.settings.containerBlockTypes;

Object.keys(blocks).forEach((blockId) => {
const block = blocks[blockId];
if (types.includes(block['@type'])) {
result.push(blockId);
} else if (containerBlockTypes.includes(block['@type']) || block.blocks) {
findBlocks(block.blocks, types, result);
}
});

return result;
}
38 changes: 38 additions & 0 deletions src/helpers/Blocks/Blocks.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import {
buildStyleClassNamesFromData,
buildStyleClassNamesExtenders,
getPreviousNextBlock,
blocksFormGenerator,

Check warning on line 22 in src/helpers/Blocks/Blocks.test.js

View workflow job for this annotation

GitHub Actions / ESlint (16.x)

'blocksFormGenerator' is defined but never used
findBlocks,
} from './Blocks';

import config from '@plone/volto/registry';
Expand Down Expand Up @@ -1226,3 +1228,39 @@ describe('Blocks', () => {
});
});
});

describe('findBlocks', () => {
it('Get all blocks in the first level (main block container)', () => {
const blocks = {
'1': { title: 'title', '@type': 'title' },
'2': { title: 'an image', '@type': 'image' },
'3': { title: 'description', '@type': 'description' },
'4': { title: 'a text', '@type': 'slate' },
};
const types = ['description'];
expect(findBlocks(blocks, types)).toStrictEqual(['3']);
});

it('Get all blocks in the first level (main block container) given a list', () => {
const blocks = {
'1': { title: 'title', '@type': 'title' },
'2': { title: 'an image', '@type': 'image' },
'3': { title: 'description', '@type': 'description' },
'4': { title: 'a text', '@type': 'slate' },
};
const types = ['description', 'slate'];
expect(findBlocks(blocks, types)).toStrictEqual(['3', '4']);
});

it('Get all blocks in the first level (main block container) given a list', () => {
const blocks = {
'1': { title: 'title', '@type': 'title' },
'2': { title: 'an image', '@type': 'image' },
'3': { title: 'description', '@type': 'description' },
'4': { title: 'a text', '@type': 'slate' },
'5': { title: 'a text', '@type': 'slate' },
};
const types = ['description', 'slate'];
expect(findBlocks(blocks, types)).toStrictEqual(['3', '4', '5']);
});
});
9 changes: 2 additions & 7 deletions src/helpers/Utils/usePagination.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React, { useRef, useEffect } from 'react';
import { useHistory, useLocation } from 'react-router-dom';
import qs from 'query-string';
import { useSelector } from 'react-redux';
import { slugify } from '@plone/volto/helpers/Utils/Utils';
import { findBlocks, slugify } from '@plone/volto/helpers';

/**
* @function useCreatePageQueryStringKey
Expand All @@ -12,13 +12,8 @@ import { slugify } from '@plone/volto/helpers/Utils/Utils';
const useCreatePageQueryStringKey = (id) => {
const blockTypesWithPagination = ['search', 'listing'];
const blocks = useSelector((state) => state?.content?.data?.blocks) || [];
const blocksLayout =
useSelector((state) => state?.content?.data?.blocks_layout?.items) || [];
const displayedBlocks = blocksLayout?.map((item) => blocks[item]);
const hasMultiplePaginations =
displayedBlocks.filter((item) =>
blockTypesWithPagination.includes(item['@type']),
).length > 1 || false;
findBlocks(blocks, blockTypesWithPagination).length > 1;

return hasMultiplePaginations ? slugify(`page-${id}`) : 'page';
};
Expand Down
3 changes: 3 additions & 0 deletions src/helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export {
buildStyleClassNamesFromData,
buildStyleClassNamesExtenders,
getPreviousNextBlock,
findBlocks,
} from '@plone/volto/helpers/Blocks/Blocks';
export { default as BodyClass } from '@plone/volto/helpers/BodyClass/BodyClass';
export { default as ScrollToTop } from '@plone/volto/helpers/ScrollToTop/ScrollToTop';
Expand Down Expand Up @@ -91,6 +92,8 @@ export {
replaceItemOfArray,
cloneDeepSchema,
reorderArray,
isInteractiveElement,
slugify,
} from '@plone/volto/helpers/Utils/Utils';
export { messages } from './MessageLabels/MessageLabels';
export {
Expand Down
1 change: 1 addition & 0 deletions test-setup-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ config.set('settings', {
viewableInBrowserObjects: [],
styleClassNameConverters,
styleClassNameExtenders,
containerBlockTypes: [],
});
config.set('blocks', {
blocksConfig: {
Expand Down

0 comments on commit 511a667

Please sign in to comment.