@ordergroove/offers 2.27.21 → 2.27.22-alpha-PR-663-5.4

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ordergroove/offers",
3
- "version": "2.27.21",
3
+ "version": "2.27.22-alpha-PR-663-5.4+a95dd1be",
4
4
  "description": "offer state component",
5
5
  "author": "Eugenio Lattanzio <eugenio63@gmail.com>",
6
6
  "homepage": "https://github.com/ordergroove/plush-toys#readme",
@@ -45,5 +45,5 @@
45
45
  "devDependencies": {
46
46
  "@ordergroove/offers-templates": "^0.4.15"
47
47
  },
48
- "gitHead": "e2b5d12965378f8ce62ea66be35de68ac7338e9c"
48
+ "gitHead": "a95dd1be7e59823c6754a58ad6fe5066630697ab"
49
49
  }
package/src/core/store.js CHANGED
@@ -3,6 +3,7 @@ import thunk from 'redux-thunk';
3
3
 
4
4
  import { loadState } from './localStorage';
5
5
  import { dispatchMiddleware, localStorageMiddleware, offerEvents } from './middleware';
6
+ import { waitUntilOffersReady } from './waitUntilOffersReady';
6
7
 
7
8
  export function makeStore(reducer, ...extraMiddlewares) {
8
9
  if (window.og && window.og.store) return window.og.store;
@@ -14,11 +15,11 @@ export function makeStore(reducer, ...extraMiddlewares) {
14
15
  typeof window === 'object' && window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__
15
16
  ? // eslint-disable-next-line no-underscore-dangle
16
17
  window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__({
17
- // Specify extension’s options like name, actionsBlacklist, actionsCreators, serialize...
18
+ name: 'Ordergroove Offers'
18
19
  })
19
20
  : compose;
20
21
 
21
- const middlewares = [thunk, dispatchMiddleware, offerEvents];
22
+ const middlewares = [waitUntilOffersReady, thunk, dispatchMiddleware, offerEvents];
22
23
 
23
24
  let initial = {};
24
25
 
package/src/core/utils.ts CHANGED
@@ -36,7 +36,7 @@ export function resolveEnvAndMerchant() {
36
36
  }
37
37
 
38
38
  export const safeProductId = product => {
39
- if (!product ) return '';
39
+ if (!product) return '';
40
40
  let productId = `${product.id || product}`;
41
41
  if (platform?.shopify_selling_plans) {
42
42
  // we can't avoid make offer request since we need to know the upsell group and autoship by default
@@ -49,9 +49,9 @@ export const safeProductId = product => {
49
49
  /**
50
50
  * Returns the OG frequency if platform is running on selling plans
51
51
  * @param initialFrequency
52
- * @param config
52
+ * @param config
53
53
  */
54
- export const safeOgFrequency = (initialFrequency, config) => {
54
+ export const safeOgFrequency = (initialFrequency, config) => {
55
55
  if (platform.shopify_selling_plans) {
56
56
  const ix = config.frequencies?.indexOf(initialFrequency);
57
57
  if (ix >= 0 && config.frequenciesEveryPeriod[ix]) {
@@ -70,9 +70,20 @@ export const safeProductId = product => {
70
70
  */
71
71
  export function autoInitializeOffers(offers) {
72
72
  if (offers.isReady()) return;
73
+
74
+ console.info('OG offers are auto initializing');
75
+
73
76
  const [merchantId, env] = resolveEnvAndMerchant();
74
77
  if (!env && !merchantId) return;
75
- offers.initialize(merchantId, env);
78
+ const script = document.createElement('script');
79
+ script.onload = () => console.info('OG pull initialization chunk for merchant', merchantId, env);
80
+ script.onerror = () => offers.initialize(merchantId, env);
81
+ script.src = `${window.location.protocol}//${
82
+ env === ENV_PROD ? STATIC_HOST : STAGING_STATIC_HOST
83
+ }/${merchantId}/main.js?initOnly=true`;
84
+
85
+
86
+ document.head.appendChild(script);
76
87
  }
77
88
 
78
89
  export const clearCookie = cookieId => {
@@ -0,0 +1,66 @@
1
+ import { createSessionId, fetchAuth } from './actions';
2
+ import {
3
+ CREATED_SESSION_ID,
4
+ SET_ENVIRONMENT_DEV,
5
+ SET_ENVIRONMENT_PROD,
6
+ SET_ENVIRONMENT_STAGING,
7
+ SET_MERCHANT_ID
8
+ } from './constants';
9
+ import { listenLocalStorageChanges } from './localStorage';
10
+
11
+ const waitFor = () => {
12
+ let resolve, reject;
13
+ return [
14
+ new Promise((yes, no) => {
15
+ resolve = yes;
16
+ reject = no;
17
+ }),
18
+ resolve,
19
+ reject
20
+ ];
21
+ };
22
+ /**
23
+ * Middleware that awaits offers being initialized and resumes the regular actions after.
24
+ * @param {*} store
25
+ * @returns
26
+ */
27
+ export function waitUntilOffersReady(store) {
28
+ const [waitForSetEnv, resolveEnv] = waitFor();
29
+ const [waitForMerchantId, resolveMerchantId] = waitFor();
30
+ const [waitForSessionId, resolveSessionId] = waitFor();
31
+
32
+ waitForMerchantId.then(merchantId => {
33
+ const { sessionId } = store.getState();
34
+ if (!sessionId || (merchantId && !sessionId.startsWith(merchantId))) {
35
+ store.dispatch(createSessionId(merchantId));
36
+ } else {
37
+ resolveSessionId(sessionId);
38
+ }
39
+ });
40
+
41
+ const waitForReady = Promise.all([waitForMerchantId, waitForSetEnv, waitForSessionId]);
42
+
43
+ waitForReady.then(() => {
44
+ window.addEventListener('storage', listenLocalStorageChanges(store));
45
+ if (!store.getState().auth?.ts) {
46
+ store.dispatch(fetchAuth());
47
+ }
48
+ });
49
+
50
+ return next => async action => {
51
+ if (
52
+ SET_ENVIRONMENT_DEV === action.type ||
53
+ SET_ENVIRONMENT_STAGING === action.type ||
54
+ SET_ENVIRONMENT_PROD === action.type
55
+ ) {
56
+ resolveEnv(action.payload);
57
+ } else if (SET_MERCHANT_ID === action.type) {
58
+ resolveMerchantId(action.payload);
59
+ } else if (CREATED_SESSION_ID === action.type) {
60
+ resolveSessionId(action.payload);
61
+ } else {
62
+ await waitForReady;
63
+ }
64
+ next(action);
65
+ };
66
+ }
package/src/index.js CHANGED
@@ -34,15 +34,19 @@ export const setTemplates = offers.setTemplates;
34
34
  export const setupCart = offers.setupCart;
35
35
  export const setupProduct = offers.setupProduct;
36
36
  export const setupProducts = offers.setupProducts;
37
+ export const autoInit = () => autoInitializeOffers(offers);
38
+
37
39
  export { platform };
38
40
  export default offers.initialize;
39
-
40
41
  /*
41
42
  * Attempts to auto initialize the offer library reading the merchantId and env from
42
43
  * integration script i.e. <script src="http://static.ordergroove...."/>.
43
44
  * Useful when local develop using http redirects
44
45
  */
45
- onReady(() => autoInitializeOffers(offers));
46
+ if (process.env.NODE_ENV === 'development') {
47
+ console.info('%c Ordergroove Offers DEV MODE ', 'background: #222; color: #bada55');
48
+ onReady(autoInit);
49
+ }
46
50
 
47
51
  if (platform?.shopify_selling_plans) {
48
52
  onReady(() => authorizeShopifyCustomer(offers));
package/src/make-api.js CHANGED
@@ -156,7 +156,11 @@ export default function makeApi(store) {
156
156
  */
157
157
  initialize(merchantId, env, authUrl) {
158
158
  // settings resolves once, before anything.
159
- if (!isReady) offers.resolveSettings(merchantId, env, window.og_settings, store);
159
+ if (isReady) {
160
+ console.warn('og.offers has been initialized already. Skipping.');
161
+ } else {
162
+ offers.resolveSettings(merchantId, env, window.og_settings, store);
163
+ }
160
164
 
161
165
  const state = store.getState();
162
166
  // dont re-trigger actions if value is the same. allowing set new authurl only
@@ -165,20 +169,6 @@ export default function makeApi(store) {
165
169
  // allow set new authUrl
166
170
  if (authUrl) offers.setAuthUrl(authUrl);
167
171
 
168
- if (isReady) {
169
- console.warn('og.offers has been initialized already. Skipping.');
170
- } else {
171
- window.addEventListener('storage', listenLocalStorageChanges(store));
172
- store.dispatch(actions.requestSessionId());
173
- }
174
-
175
- if (arguments.length === 3) {
176
- // fetch auth is not there already
177
- if (!store.getState().auth?.ts) {
178
- store.dispatch(actions.fetchAuth());
179
- }
180
- }
181
-
182
172
  isReady = true;
183
173
 
184
174
  return this;