Skip to content

Commit

Permalink
Use default query in SSR rendering and client fetch of the listing block
Browse files Browse the repository at this point in the history
when we don't have a search criteria

Now we use getQueryStringResults on withQuerystringResults even when we
have no search criteria.
  • Loading branch information
wesleybl committed Nov 16, 2022
1 parent a621e32 commit 91ac749
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 22 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

### Breaking

- Use default query in SSR rendering and client fetch of the listing block when we don't have a search criteria. Fix https://github.com/plone/volto/issues/3839 @wesleybl

### Feature

### Bugfix
Expand Down
5 changes: 4 additions & 1 deletion src/components/manage/Blocks/Listing/getAsyncData.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { getQueryStringResults } from '@plone/volto/actions';
import { resolveBlockExtensions } from '@plone/volto/helpers';
import { defaultQuery } from './withQuerystringResults';

export default ({ dispatch, data, path, blocksConfig }) => {
const { resolvedExtensions } = resolveBlockExtensions(data, blocksConfig);
Expand All @@ -9,7 +10,9 @@ export default ({ dispatch, data, path, blocksConfig }) => {
getQueryStringResults(
path,
{
...data.querystring,
...(data.querystring
? data.querystring
: { query: defaultQuery, sort_on: 'getObjPositionInParent' }),
...(resolvedExtensions?.variation?.fullobjects
? { fullobjects: 1 }
: { metadata_fields: '_all' }),
Expand Down
50 changes: 29 additions & 21 deletions src/components/manage/Blocks/Listing/withQuerystringResults.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,26 @@ import { useDispatch, useSelector } from 'react-redux';
import hoistNonReactStatics from 'hoist-non-react-statics';
import useDeepCompareEffect from 'use-deep-compare-effect';

import { getContent, getQueryStringResults } from '@plone/volto/actions';
import { getQueryStringResults } from '@plone/volto/actions';
import { usePagination, getBaseUrl } from '@plone/volto/helpers';

import config from '@plone/volto/registry';

export const defaultQuery = [
{
i: 'path',
o: 'plone.app.querystring.operation.string.relativePath',
v: '::1',
},
];

function getDisplayName(WrappedComponent) {
return WrappedComponent.displayName || WrappedComponent.name || 'Component';
}

export default function withQuerystringResults(WrappedComponent) {
function WithQuerystringResults(props) {
const { data = {}, properties: content, path, variation } = props;
const { data = {}, 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

Expand Down Expand Up @@ -43,33 +51,22 @@ export default function withQuerystringResults(WrappedComponent) {
);
const dispatch = useDispatch();

const folderItems = content?.is_folderish ? content.items : [];
const hasQuery = querystring?.query?.length > 0;
const hasLoaded = hasQuery ? !querystringResults?.[block]?.loading : true;
const hasLoaded = !querystringResults?.[block]?.loading;

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

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

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

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

Expand Down Expand Up @@ -102,7 +99,18 @@ export default function withQuerystringResults(WrappedComponent) {
),
);
} else {
dispatch(getContent(initialPath, null, null, currentPage));
dispatch(
getQueryStringResults(
initialPath,
{
...adaptedQuery,
sort_on: 'getObjPositionInParent',
query: defaultQuery,
},
block,
currentPage,
),
);
}
}, [
block,
Expand Down

0 comments on commit 91ac749

Please sign in to comment.