@ordergroove/offers 2.24.3 → 2.25.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +19 -0
- package/dist/bundle-report.html +99 -98
- package/dist/examples.js +9 -9
- package/dist/examples.js.map +2 -2
- package/dist/offers.js +159 -144
- package/dist/offers.js.map +3 -3
- package/examples/index.js +10 -1
- package/package.json +4 -4
- package/src/__tests__/offers.spec.js +100 -5
- package/src/components/Offer.js +10 -1
- package/src/components/Price.js +54 -0
- package/src/components/__tests__/OG.fspec.js +2 -2
- package/src/components/__tests__/Price.fspec.js +43 -0
- package/src/core/actions.js +7 -4
- package/src/core/constants.js +1 -0
- package/src/core/reducer.js +10 -0
- package/src/index.js +187 -150
- package/src/init-func-tests.js +1 -1
- package/src/init-test.js +2 -2
package/src/index.js
CHANGED
|
@@ -1,169 +1,206 @@
|
|
|
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
|
-
|
|
1
|
+
import { offersLiveEditor } from '@ordergroove/offers-live-editor';
|
|
2
|
+
import { setStore } from './core/connect';
|
|
3
|
+
import { getStore } from './core/store';
|
|
4
|
+
import { listenLocalStorageChanges } from './core/localStorage';
|
|
5
|
+
import * as adapters from './core/adapters';
|
|
6
|
+
import * as actions from './core/actions';
|
|
7
|
+
import { ConnectedWhen } from './components/When';
|
|
8
|
+
import { ConnectedOptinButton } from './components/OptinButton';
|
|
9
|
+
import { ConnectedOptoutButton } from './components/OptoutButton';
|
|
10
|
+
import { ConnectedOptinSelect } from './components/OptinSelect';
|
|
11
|
+
import { ConnectedUpsellButton } from './components/UpsellButton';
|
|
12
|
+
import { ConnectedUpsellModal } from './components/UpsellModal';
|
|
13
|
+
import { ConnectedOptinToggle } from './components/OptinToggle';
|
|
14
|
+
import { ConnectedOptinStatus } from './components/OptinStatus';
|
|
15
|
+
import { ConnectedText } from './components/Text';
|
|
16
|
+
import { ConnectedIncentiveText } from './components/IncentiveText';
|
|
17
|
+
import { ConnectedSelectFrequency } from './components/SelectFrequency';
|
|
18
|
+
import { ConnectedNextUpcomingOrder } from './components/NextUpcomingOrder';
|
|
19
|
+
import { ConnectedOffer } from './components/Offer';
|
|
20
|
+
import { Modal } from './components/Modal';
|
|
21
|
+
import { Select } from './components/Select';
|
|
22
|
+
import { Tooltip } from './components/Tooltip';
|
|
23
|
+
import { ConnectedFrequencyStatus } from './components/FrequencyStatus';
|
|
24
|
+
import * as testMode from './test-mode';
|
|
25
|
+
import { api } from './core/api';
|
|
26
|
+
import { environment, offer } from './core/reducer';
|
|
27
|
+
import { RECEIVE_PRODUCT_PLANS } from './core/constants';
|
|
28
|
+
import ConnectedPrice from './components/Price';
|
|
28
29
|
|
|
29
30
|
testMode.enable();
|
|
30
31
|
let store;
|
|
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
|
-
|
|
32
|
+
|
|
33
|
+
export const offers = {};
|
|
34
|
+
|
|
35
|
+
export function setEnvironment(e) {
|
|
36
|
+
store.dispatch(actions.setEnvironment(e));
|
|
37
|
+
return offers;
|
|
38
|
+
}
|
|
39
|
+
export function setMerchantId(m) {
|
|
40
|
+
store.dispatch(actions.setMerchantId(m));
|
|
41
|
+
return offers;
|
|
42
|
+
}
|
|
43
|
+
export function setAuthUrl(authUrl) {
|
|
44
|
+
store.dispatch(actions.setAuthUrl(authUrl));
|
|
45
|
+
return offers;
|
|
46
|
+
}
|
|
47
|
+
export function getProductsForPurchasePost(productIds = []) {
|
|
48
|
+
return adapters.getProductsForPurchasePost(store.getState(), productIds);
|
|
49
|
+
}
|
|
50
|
+
export function getOptins(productIds = []) {
|
|
51
|
+
return adapters.getProductsForPurchasePost(store.getState(), productIds);
|
|
52
|
+
}
|
|
53
|
+
export function clear() {
|
|
54
|
+
store.dispatch(actions.checkout());
|
|
55
|
+
}
|
|
56
|
+
export function addOptinChangedCallback(fn) {
|
|
57
|
+
if (typeof fn === 'function') document.addEventListener('optin-changed', e => fn(e.detail));
|
|
58
|
+
}
|
|
59
|
+
export function disableOptinChangedCallbacks() {
|
|
60
|
+
document.addEventListener('optin-changed', e => e.stopPropagation(), true);
|
|
61
|
+
}
|
|
62
|
+
export function register() {
|
|
63
|
+
try {
|
|
64
|
+
customElements.define('og-when', ConnectedWhen);
|
|
65
|
+
customElements.define('og-text', ConnectedText);
|
|
66
|
+
customElements.define('og-incentive-text', ConnectedIncentiveText);
|
|
67
|
+
customElements.define('og-offer', ConnectedOffer);
|
|
68
|
+
customElements.define('og-select-frequency', ConnectedSelectFrequency);
|
|
69
|
+
customElements.define('og-optout-button', ConnectedOptoutButton);
|
|
70
|
+
customElements.define('og-optin-toggle', ConnectedOptinToggle);
|
|
71
|
+
customElements.define('og-optin-status', ConnectedOptinStatus);
|
|
72
|
+
customElements.define('og-optin-button', ConnectedOptinButton);
|
|
73
|
+
customElements.define('og-optin-select', ConnectedOptinSelect);
|
|
74
|
+
customElements.define('og-upsell-button', ConnectedUpsellButton);
|
|
75
|
+
customElements.define('og-frequency-status', ConnectedFrequencyStatus);
|
|
76
|
+
customElements.define('og-modal', Modal);
|
|
77
|
+
customElements.define('og-select', Select);
|
|
78
|
+
customElements.define('og-tooltip', Tooltip);
|
|
79
|
+
customElements.define('og-upsell-modal', ConnectedUpsellModal);
|
|
80
|
+
customElements.define('og-next-upcoming-order', ConnectedNextUpcomingOrder);
|
|
81
|
+
customElements.define('og-price', ConnectedPrice);
|
|
82
|
+
} catch (err) {
|
|
83
|
+
// eslint-disable-next-line no-console
|
|
84
|
+
console.warn(err);
|
|
85
|
+
}
|
|
86
|
+
offers.register = () => 0;
|
|
87
|
+
}
|
|
88
|
+
export function previewMode(set) {
|
|
89
|
+
window.og = window.og || {};
|
|
90
|
+
if (set === false) {
|
|
91
|
+
delete window.og;
|
|
92
|
+
} else {
|
|
93
|
+
window.og.previewMode = true;
|
|
94
|
+
}
|
|
95
|
+
return this;
|
|
96
|
+
}
|
|
97
|
+
export function config(configuration) {
|
|
98
|
+
store.dispatch(actions.setConfig(configuration));
|
|
99
|
+
return offers;
|
|
100
|
+
}
|
|
101
|
+
export function setLocale(locale) {
|
|
102
|
+
store.dispatch(actions.setLocale(locale));
|
|
103
|
+
return offers;
|
|
104
|
+
}
|
|
105
|
+
export function addTemplate(tagName, content, configOption) {
|
|
106
|
+
store.dispatch(actions.addTemplate(tagName, content, configOption));
|
|
107
|
+
return offers;
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* templates object where keys are selectors and values are content
|
|
111
|
+
*/
|
|
112
|
+
export function setTemplates(templates) {
|
|
113
|
+
store.dispatch(actions.setTemplates(templates));
|
|
114
|
+
return offers;
|
|
115
|
+
}
|
|
116
|
+
export function setPublicPath(publicPath) {
|
|
117
|
+
return offers;
|
|
118
|
+
}
|
|
119
|
+
export function resolveSettings(merchantId, env, settings, storeInstance = store) {
|
|
120
|
+
if (merchantId && env && settings) {
|
|
121
|
+
let products = [];
|
|
122
|
+
if (settings.product) {
|
|
123
|
+
products.push(settings.product);
|
|
124
|
+
} else if (settings.cart && Array.isArray(settings.cart.products)) {
|
|
125
|
+
products = products.concat(settings.cart.products);
|
|
90
126
|
}
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
* @param {*} authUrl
|
|
98
|
-
*/
|
|
99
|
-
initialize(merchantId, env, authUrl) {
|
|
100
|
-
if (offers.isReady) {
|
|
101
|
-
console.warn('og.offers has been initialized already. Skipping.');
|
|
102
|
-
return offers;
|
|
127
|
+
const { apiUrl } = environment({}, actions.setEnvironment(env));
|
|
128
|
+
const { sessionId } = storeInstance.getState();
|
|
129
|
+
if (sessionId) {
|
|
130
|
+
products.forEach(product => {
|
|
131
|
+
api.fetchOffer(apiUrl, merchantId, sessionId, `${product}`, 'pdp');
|
|
132
|
+
});
|
|
103
133
|
}
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
if (merchantId && env && settings) {
|
|
108
|
-
let products = [];
|
|
109
|
-
if (settings.product) {
|
|
110
|
-
products.push(settings.product);
|
|
111
|
-
} else if (settings.cart && Array.isArray(settings.cart.products)) {
|
|
112
|
-
products = products.concat(settings.cart.products);
|
|
113
|
-
}
|
|
114
|
-
const { apiUrl } = environment({}, actions.setEnvironment(env));
|
|
115
|
-
const { sessionId } = store.getState();
|
|
116
|
-
if (sessionId) {
|
|
117
|
-
products.forEach(product => api.fetchOffer(apiUrl, merchantId, sessionId, `${product}`, 'pdp'));
|
|
118
|
-
}
|
|
134
|
+
|
|
135
|
+
if (settings.product_discounts && typeof settings.product_discounts === 'object') {
|
|
136
|
+
storeInstance.dispatch({ type: RECEIVE_PRODUCT_PLANS, payload: settings.product_discounts });
|
|
119
137
|
}
|
|
138
|
+
}
|
|
139
|
+
}
|
|
120
140
|
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
141
|
+
/**
|
|
142
|
+
* Initialize OG object
|
|
143
|
+
* @param {*} merchantId
|
|
144
|
+
* @param {*} env
|
|
145
|
+
* @param {*} authUrl
|
|
146
|
+
*/
|
|
147
|
+
export function initialize(merchantId, env, authUrl) {
|
|
148
|
+
if (offers.isReady) {
|
|
149
|
+
console.warn('og.offers has been initialized already. Skipping.');
|
|
150
|
+
return offers;
|
|
151
|
+
}
|
|
125
152
|
|
|
126
|
-
|
|
153
|
+
store = getStore();
|
|
127
154
|
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
store.dispatch(actions.fetchAuth());
|
|
131
|
-
offers.register();
|
|
132
|
-
}
|
|
155
|
+
offers.store = store;
|
|
156
|
+
offers.resolveSettings(merchantId, env, window.og_settings, store);
|
|
133
157
|
|
|
134
|
-
|
|
158
|
+
setStore(store);
|
|
135
159
|
|
|
136
|
-
|
|
137
|
-
|
|
160
|
+
if (merchantId) offers.setMerchantId(merchantId);
|
|
161
|
+
if (env) offers.setEnvironment(env);
|
|
162
|
+
if (authUrl) offers.setAuthUrl(authUrl);
|
|
138
163
|
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
return offers;
|
|
146
|
-
},
|
|
147
|
-
addTemplate(tagName, content, config) {
|
|
148
|
-
store.dispatch(actions.addTemplate(tagName, content, config));
|
|
149
|
-
return offers;
|
|
150
|
-
},
|
|
151
|
-
/**
|
|
152
|
-
* templates object where keys are selectors and values are content
|
|
153
|
-
*/
|
|
154
|
-
setTemplates(templates) {
|
|
155
|
-
store.dispatch(actions.setTemplates(templates));
|
|
156
|
-
return offers;
|
|
157
|
-
},
|
|
158
|
-
setPublicPath(publicPath) {
|
|
159
|
-
return offers;
|
|
164
|
+
window.addEventListener('storage', listenLocalStorageChanges(store));
|
|
165
|
+
|
|
166
|
+
if (merchantId && env) {
|
|
167
|
+
store.dispatch(actions.requestSessionId());
|
|
168
|
+
store.dispatch(actions.fetchAuth());
|
|
169
|
+
offers.register();
|
|
160
170
|
}
|
|
161
|
-
|
|
171
|
+
|
|
172
|
+
offers.isReady = true;
|
|
173
|
+
|
|
174
|
+
return offers;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
export default initialize;
|
|
178
|
+
|
|
179
|
+
Object.assign(offers, {
|
|
180
|
+
setEnvironment,
|
|
181
|
+
setMerchantId,
|
|
182
|
+
setAuthUrl,
|
|
183
|
+
getProductsForPurchasePost,
|
|
184
|
+
getOptins,
|
|
185
|
+
clear,
|
|
186
|
+
addOptinChangedCallback,
|
|
187
|
+
disableOptinChangedCallbacks,
|
|
188
|
+
register,
|
|
189
|
+
previewMode,
|
|
190
|
+
config,
|
|
191
|
+
setLocale,
|
|
192
|
+
addTemplate,
|
|
193
|
+
setTemplates,
|
|
194
|
+
setPublicPath,
|
|
195
|
+
resolveSettings,
|
|
196
|
+
initialize
|
|
197
|
+
});
|
|
198
|
+
|
|
162
199
|
window.OG = window.OG || {};
|
|
163
200
|
Object.assign(window.OG, offers);
|
|
164
201
|
Object.assign(offers.initialize, offers);
|
|
165
202
|
|
|
166
203
|
offersLiveEditor();
|
|
167
204
|
|
|
168
|
-
// use this syntax to allow es6 module be called as default function og.offers(...)
|
|
169
|
-
module.exports = offers.initialize;
|
|
205
|
+
// // use this syntax to allow es6 module be called as default function og.offers(...)
|
|
206
|
+
// module.exports = offers.initialize;
|
package/src/init-func-tests.js
CHANGED
package/src/init-test.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { initialize } from './index';
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
initialize('some-merchant', 'staging');
|