forked from findify/react-bundle
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
120 lines (93 loc) · 3.24 KB
/
index.js
File metadata and controls
120 lines (93 loc) · 3.24 KB
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
import { useRef, useEffect, useState } from 'react';
const eventBindings = {
autocomplete: 'change:suggestions',
recommendation: 'change:items',
search: 'change:items',
'smart-collection': 'change:items',
}
const randomKey = () => Math.random().toString(36).substring(7)
export const waitForFindify = () => new Promise(resolve =>
(window.findifyCallbacks = window.findifyCallbacks || []).push(findify => resolve(findify))
);
const getWidgetConfig = (type, node, config, customs) => {
if (type !== 'recommendation') {
return customs;
}
let cfg = config.getIn(['features', 'recommendations', customs.slot || node.getAttribute('id')]);
if (!cfg) {
cfg = config.getIn(['features', 'recommendations', `#${customs.slot || node.getAttribute('id')}`]);
}
return cfg.mergeDeep(customs);
};
const cleanCollectionSlot = (slot) => slot.replace(/^\/|\/$/g, "").toLowerCase();
export default ({ type, config = {}, options = {}, history, widgetKey = randomKey() }) => {
const container = useRef(null);
const [ready, setReady] = useState(false);
const [hasError, setError] = useState(false);
useEffect(() => {
if (!container.current) return;
let findify = void 0;
let shouldRender = true;
const init = async () => {
findify = await waitForFindify();
if (!shouldRender) return;
if (history) {
findify.utils.setHistory
? findify.utils.setHistory(history)
: findify.utils.history = history
};
const widgetConfig = getWidgetConfig(
type,
container.current,
findify.config,
{
widgetKey,
disableAutoRequest: true,
...config,
}
);
findify.widgets.attach(
container.current,
type === 'smart-collection' ? 'search' : type,
widgetConfig
)
const widget = findify.widgets.get(widgetKey)
const meta = widget.config.get('meta') && widget.config.get('meta').toJS() || {};
const defaultRequestParams = (widget.config.get('defaultRequestParams') && widget.config.get('defaultRequestParams').toJS()) || {};
const defaults = {
...meta,
...defaultRequestParams,
...options
}
if (type === 'recommendation') {
defaults.slot = config.slot;
defaults.type = config.type || widgetConfig.get('type');
}
if (type === 'smart-collection') {
defaults.slot = config.slot && config.slot !== '' ? cleanCollectionSlot(config.slot) : findify.utils.collectionPath();
}
const callback = (items) => window.requestAnimationFrame(() => {
widget.agent.off(callback)
if (!items.size) setError(true)
setReady(true);
})
widget.agent
.defaults(defaults)
.on('error', () => setError(true))
.on(eventBindings[type], callback)
if (['search', 'smart-collection'].includes(type)) {
widget.agent.applyState(findify.utils.getQuery())
}
}
init();
return () => {
console.log('detach', widgetKey)
if (findify && findify.widgets.get(widgetKey)) {
findify.widgets.detach(widgetKey)
} else {
shouldRender = false
}
}
}, [container]);
return [container, ready, hasError];
}