Skip to content

Commit

Permalink
(Synchronize redundant block id in listing block on pasting) Fix dupl…
Browse files Browse the repository at this point in the history
…icating listing block (#4239)

Obvoiusly no objections anymore.
  • Loading branch information
ksuess authored and ionlizarazu committed Apr 20, 2023
1 parent 86cc3f2 commit f8de286
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 29 deletions.
4 changes: 2 additions & 2 deletions docs/source/blocks/ssr.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ configuration, pointing to a function that returns a list of promises.
For example:

```js
export default ({ dispatch, data, path }) => {
export default ({ dispatch, id, data, path }) => {
return [
dispatch(
getQueryStringResults(path, { ...data, fullobjects: 1 }, data.block),
getQueryStringResults(path, { ...data, fullobjects: 1 }, id),
),
];
};
Expand Down
1 change: 1 addition & 0 deletions news/4234.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix duplicating listing block by removing block uid from blocks data. @ksuess
14 changes: 0 additions & 14 deletions src/components/manage/Blocks/Listing/Edit.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,7 @@ const messages = defineMessages({
const Edit = React.memo(
(props) => {
const { data, onChangeBlock, block, selected, pathname } = props;

const intl = useIntl();

// componentDidMount
React.useEffect(() => {
if (!data.query) {
onChangeBlock(block, {
...data,
query: [],
block,
});
}
/* eslint-disable react-hooks/exhaustive-deps */
}, []);

const placeholder =
data.placeholder ||
(data?.querystring?.query?.length
Expand Down
12 changes: 10 additions & 2 deletions src/components/manage/Blocks/Listing/getAsyncData.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import { getQueryStringResults } from '@plone/volto/actions';
import { resolveBlockExtensions } from '@plone/volto/helpers';

export default ({ dispatch, data, path, blocksConfig }) => {
const getListingBlockAsyncData = ({
dispatch,
id,
data,
path,
blocksConfig,
}) => {
const { resolvedExtensions } = resolveBlockExtensions(data, blocksConfig);

return [
Expand All @@ -14,8 +20,10 @@ export default ({ dispatch, data, path, blocksConfig }) => {
? { fullobjects: 1 }
: { metadata_fields: '_all' }),
},
data.block,
id,
),
),
];
};

export default getListingBlockAsyncData;
25 changes: 15 additions & 10 deletions src/components/manage/Blocks/Listing/withQuerystringResults.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,16 @@ function getDisplayName(WrappedComponent) {

export default function withQuerystringResults(WrappedComponent) {
function WithQuerystringResults(props) {
const { data = {}, properties: content, path, variation } = props;
const {
data = {},
id = data.block,
properties: content,
path,
variation,
} = props;
const { settings } = config;
const querystring = data.querystring || data; // For backwards compat with data saved before Blocks schema. Note, this is also how the Search block passes data to ListingBody

const { block } = data;
const { b_size = settings.defaultPageSize } = querystring; // batchsize

// save the path so it won't trigger dispatch on eager router location change
Expand Down Expand Up @@ -50,29 +55,29 @@ export default function withQuerystringResults(WrappedComponent) {
const hasLoaded = hasQuery ? querystringResults?.[block]?.loaded : true;

const listingItems =
querystring?.query?.length > 0 && querystringResults?.[block]
? querystringResults?.[block]?.items || []
querystring?.query?.length > 0 && querystringResults?.[id]
? querystringResults?.[id]?.items || []
: folderItems;

const showAsFolderListing = !hasQuery && content?.items_total > b_size;
const showAsQueryListing =
hasQuery && querystringResults?.[block]?.total > b_size;
hasQuery && querystringResults?.[id]?.total > b_size;

const totalPages = showAsFolderListing
? Math.ceil(content.items_total / b_size)
: showAsQueryListing
? Math.ceil(querystringResults[block].total / b_size)
? Math.ceil(querystringResults[id].total / b_size)
: 0;

const prevBatch = showAsFolderListing
? content.batching?.prev
: showAsQueryListing
? querystringResults[block].batching?.prev
? querystringResults[id].batching?.prev
: null;
const nextBatch = showAsFolderListing
? content.batching?.next
: showAsQueryListing
? querystringResults[block].batching?.next
? querystringResults[id].batching?.next
: null;

const isImageGallery =
Expand Down Expand Up @@ -122,7 +127,7 @@ export default function withQuerystringResults(WrappedComponent) {
adaptedQueryRef.current = adaptedQuery;
currentPageRef.current = currentPage;
}, [
block,
id,
isImageGallery,
adaptedQuery,
hasQuery,
Expand All @@ -136,7 +141,7 @@ export default function withQuerystringResults(WrappedComponent) {
<WrappedComponent
{...props}
onPaginationChange={(e, { activePage }) => setCurrentPage(activePage)}
total={querystringResults?.[block]?.total}
total={querystringResults?.[id]?.total}
batch_size={b_size}
currentPage={currentPage}
totalPages={totalPages}
Expand Down
3 changes: 2 additions & 1 deletion src/components/manage/Blocks/Search/SearchBlockView.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ const applyDefaults = (data, root) => {
};

const SearchBlockView = (props) => {
const { data, searchData, mode = 'view', variation } = props;
const { id, data, searchData, mode = 'view', variation } = props;

const Layout = variation.view;

Expand Down Expand Up @@ -89,6 +89,7 @@ const SearchBlockView = (props) => {
setSelectedView={setSelectedView}
>
<ListingBody
id={id}
variation={{ ...data, ...listingBodyVariation }}
data={listingBodyData}
path={props.path}
Expand Down

0 comments on commit f8de286

Please sign in to comment.