Skip to content

Commit

Permalink
Backport changes in withQuerystringResults.jsx to maintain both in …
Browse files Browse the repository at this point in the history
…sync (change not backported initially because the PR containing it was "breaking"
  • Loading branch information
sneridagh committed Jun 23, 2023
1 parent ce92e05 commit afeaac9
Showing 1 changed file with 19 additions and 14 deletions.
33 changes: 19 additions & 14 deletions src/components/manage/Blocks/Listing/withQuerystringResults.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,23 @@ 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
const [initialPath] = React.useState(getBaseUrl(path));

const copyFields = ['limit', 'query', 'sort_on', 'sort_order', 'depth'];
const { currentPage, setCurrentPage } = usePagination(block, 1);
const { currentPage, setCurrentPage } = usePagination(id, 1);
const adaptedQuery = Object.assign(
variation?.fullobjects ? { fullobjects: 1 } : { metadata_fields: '_all' },
{
Expand All @@ -47,32 +52,32 @@ export default function withQuerystringResults(WrappedComponent) {

const folderItems = content?.is_folderish ? content.items : [];
const hasQuery = querystring?.query?.length > 0;
const hasLoaded = hasQuery ? querystringResults?.[block]?.loaded : true;
const hasLoaded = hasQuery ? querystringResults?.[id]?.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 All @@ -82,7 +87,7 @@ export default function withQuerystringResults(WrappedComponent) {
useDeepCompareEffect(() => {
if (hasQuery) {
dispatch(
getQueryStringResults(initialPath, adaptedQuery, block, currentPage),
getQueryStringResults(initialPath, adaptedQuery, id, currentPage),
);
} else if (isImageGallery && !hasQuery) {
// when used as image gallery, it doesn't need a query to list children
Expand All @@ -100,7 +105,7 @@ export default function withQuerystringResults(WrappedComponent) {
},
],
},
block,
id,
),
);
} else {
Expand All @@ -109,7 +114,7 @@ export default function withQuerystringResults(WrappedComponent) {
adaptedQueryRef.current = adaptedQuery;
currentPageRef.current = currentPage;
}, [
block,
id,
isImageGallery,
adaptedQuery,
hasQuery,
Expand All @@ -122,7 +127,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

0 comments on commit afeaac9

Please sign in to comment.