-
-
Notifications
You must be signed in to change notification settings - Fork 653
/
withQuerystringResults.jsx
139 lines (121 loc) · 4.47 KB
/
withQuerystringResults.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import React from 'react';
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 { usePagination, getBaseUrl } from '@plone/volto/helpers';
import config from '@plone/volto/registry';
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 { 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 adaptedQuery = Object.assign(
variation?.fullobjects ? { fullobjects: 1 } : { metadata_fields: '_all' },
{
b_size: b_size,
},
...copyFields.map((name) =>
Object.keys(querystring).includes(name)
? { [name]: querystring[name] }
: {},
),
);
const { currentPage, setCurrentPage } = usePagination(querystring, 1);
const querystringResults = useSelector(
(state) => state.querystringsearch.subrequests,
);
const dispatch = useDispatch();
const folderItems = content?.is_folderish ? content.items : [];
const hasQuery = querystring?.query?.length > 0;
const hasLoaded = hasQuery ? !querystringResults?.[block]?.loading : true;
const listingItems =
querystring?.query?.length > 0 && querystringResults?.[block]
? querystringResults?.[block]?.items || []
: folderItems;
const showAsFolderListing = !hasQuery && content?.items_total > b_size;
const showAsQueryListing =
hasQuery && querystringResults?.[block]?.total > b_size;
const totalPages = showAsFolderListing
? Math.ceil(content.items_total / b_size)
: showAsQueryListing
? Math.ceil(querystringResults[block].total / b_size)
: 0;
const prevBatch = showAsFolderListing
? content.batching?.prev
: showAsQueryListing
? querystringResults[block].batching?.prev
: null;
const nextBatch = showAsFolderListing
? content.batching?.next
: showAsQueryListing
? querystringResults[block].batching?.next
: null;
const isImageGallery =
(!data.variation && data.template === 'imageGallery') ||
data.variation === 'imageGallery';
useDeepCompareEffect(() => {
if (hasQuery) {
dispatch(
getQueryStringResults(initialPath, adaptedQuery, block, currentPage),
);
} else if (isImageGallery && !hasQuery) {
// when used as image gallery, it doesn't need a query to list children
dispatch(
getQueryStringResults(
initialPath,
{
...adaptedQuery,
b_size: 10000000000,
query: [
{
i: 'path',
o: 'plone.app.querystring.operation.string.relativePath',
v: '',
},
],
},
block,
),
);
} else {
dispatch(getContent(initialPath, null, null, currentPage));
}
}, [
block,
isImageGallery,
adaptedQuery,
hasQuery,
initialPath,
dispatch,
currentPage,
]);
return (
<WrappedComponent
{...props}
onPaginationChange={(e, { activePage }) => setCurrentPage(activePage)}
total={querystringResults?.[block]?.total}
batch_size={b_size}
currentPage={currentPage}
totalPages={totalPages}
prevBatch={prevBatch}
nextBatch={nextBatch}
listingItems={listingItems}
hasLoaded={hasLoaded}
isFolderContentsListing={showAsFolderListing}
/>
);
}
WithQuerystringResults.displayName = `WithQuerystringResults(${getDisplayName(
WrappedComponent,
)})`;
return hoistNonReactStatics(WithQuerystringResults, WrappedComponent);
}