/
connectHits.js
67 lines (59 loc) · 1.63 KB
/
connectHits.js
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
import {checkRendering} from '../../lib/utils.js';
const usage = `Usage:
var customHits = connectHits(function render(params, isFirstRendering) {
// params = {
// hits,
// results,
// instantSearchInstance,
// }
});
search.addWidget(
customHits({
[ hitsPerPage = 20 ]
});
);
Full documentation available at https://community.algolia.com/instantsearch.js/connectors/connectHits.html
`;
/**
* @typedef {Object} CustomHitsWidgetOptions
* @param {number} [hitsPerPage = 20] The number of hits to display per page
*/
/**
* @typedef HitsRenderingOptions
* @property {Object[]} hits
* @property {Object} results
* @property {InstantSearch} instantSearchInstance
* @property {Object} widgetParams all original options forwarded to rendering
*/
/**
* Connects a rendering function with the hits business logic.
* @param {function(HitsRenderingOptions)} renderFn function that renders the hits widget
* @return {function(CustomHitsWidgetOptions)} a widget factory for hits widget
*/
export default function connectHits(renderFn) {
checkRendering(renderFn, usage);
return (widgetParams = {}) => {
const {hitsPerPage = 20} = widgetParams;
return {
getConfiguration() {
return {hitsPerPage};
},
init({instantSearchInstance}) {
renderFn({
hits: [],
results: undefined,
instantSearchInstance,
widgetParams,
}, true);
},
render({results, instantSearchInstance}) {
renderFn({
hits: results.hits,
results,
instantSearchInstance,
widgetParams,
}, false);
},
};
};
}