@ordergroove/offers 2.27.19-alpha-PR-659-2.1 → 2.27.20

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.19-alpha-PR-659-2.1+ce990eef",
3
+ "version": "2.27.20",
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": "ce990eef6d106e43adf08c563903844bd8d45e13"
48
+ "gitHead": "16bf0121e814d5488ec969ca1af480074be5a198"
49
49
  }
@@ -16,11 +16,7 @@ export class OptinButton extends OptinStatus {
16
16
  }
17
17
 
18
18
  handleClick(ev) {
19
- this.optinProduct(
20
- resolveProduct(this),
21
- this.offer ? this.offer.defaultFrequency : this.defaultFrequency,
22
- this.offer
23
- );
19
+ this.optinProduct(resolveProduct(this), this.defaultFrequency || this.offer.defaultFrequency, this.offer);
24
20
  ev.preventDefault();
25
21
  }
26
22
 
package/src/core/utils.ts CHANGED
@@ -49,15 +49,15 @@ 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 param1
52
+ * @param config
53
53
  */
54
54
  export const safeOgFrequency = (initialFrequency, config) => {
55
- if (platform?.shopify_selling_plans) {
56
- const ix = config?.frequencies?.indexOf(initialFrequency);
57
- if (ix >= 0 && config?.frequenciesEveryPeriod[ix]) {
58
- return config?.frequenciesEveryPeriod[ix];
55
+ if (platform.shopify_selling_plans) {
56
+ const ix = config.frequencies?.indexOf(initialFrequency);
57
+ if (ix >= 0 && config.frequenciesEveryPeriod[ix]) {
58
+ return config.frequenciesEveryPeriod[ix];
59
59
  } else {
60
- throw `OG Can't convert selling_plain_id ${initialFrequency} to OG frequency every period`;
60
+ throw `OG Can't convert selling_plan_id ${initialFrequency} to OG frequency every period`;
61
61
  }
62
62
  }
63
63
  return initialFrequency;
@@ -112,15 +112,27 @@ describe('synchronizeCartOptin', () => {
112
112
  expect(called).toBeTruthy();
113
113
  });
114
114
 
115
- it('should pass closest section to shopify api', async () => {
115
+ it('should pass closest section cart-items to shopify api to ajax refresh', async () => {
116
116
  const sectionDiv = document.createElement('div');
117
- sectionDiv.id = 'shopify-section-123456789';
117
+ sectionDiv.id = 'shopify-section-123456789__cart-items';
118
118
  sectionDiv.classList.add('shopify-section');
119
119
  sectionDiv.appendChild(offer);
120
120
  document.body.appendChild(sectionDiv);
121
121
 
122
122
  await synchronizeCartOptin({ type: OPTIN_PRODUCT, payload: { offer, frequency, product } }, store);
123
- expect(fetchMock.lastOptions().body).toContain('"sections":["123456789"]');
123
+ expect(fetchMock.lastOptions().body).toContain('"sections":["123456789__cart-items"]');
124
+ sectionDiv.remove();
125
+ });
126
+
127
+ it('should pass closest section cart-footer to shopify api to ajax refresh', async () => {
128
+ const sectionDiv = document.createElement('div');
129
+ sectionDiv.id = 'shopify-section-123456789__cart-footer';
130
+ sectionDiv.classList.add('shopify-section');
131
+ sectionDiv.appendChild(offer);
132
+ document.body.appendChild(sectionDiv);
133
+
134
+ await synchronizeCartOptin({ type: OPTIN_PRODUCT, payload: { offer, frequency, product } }, store);
135
+ expect(fetchMock.lastOptions().body).toContain('"sections":["123456789__cart-footer"]');
124
136
  sectionDiv.remove();
125
137
  });
126
138
  });
@@ -20,6 +20,11 @@ const CART_PAGE_URL = '/cart';
20
20
  const CART_JS_URL = `${SHOPIFY_ROOT}cart.js`;
21
21
  const PRODUCTS_URL = `${SHOPIFY_ROOT}products/`;
22
22
 
23
+ /**
24
+ * List of section DOM elements to update via section-rendering api https://shopify.dev/api/section-rendering
25
+ */
26
+ const DEFAULT_SHOPIFY_CART_AJAX_SECTIONS =
27
+ '[id^="shopify-section-"][id$=__cart-items], [id^="shopify-section-"][id$="__cart-footer"],#cart-live-region-text,#cart-icon-bubble';
23
28
  const syncProductId = debounce(100, false, function(form, offer) {
24
29
  const { id } = Object.fromEntries([...new FormData(form).entries()]);
25
30
  if (id) {
@@ -147,8 +152,7 @@ export async function synchronizeCartOptin(action: any, store: any) {
147
152
  offerElement.style.pointerEvents = 'none';
148
153
  offerElement.style.opacity = '.7';
149
154
 
150
- const closestSection = offerElement.closest('.shopify-section');
151
- const closestSectionId = (closestSection?.id.match(/^shopify-section-(.+)/) || [])[1];
155
+ const sectionsToUpdate = Array.from(document.querySelectorAll(DEFAULT_SHOPIFY_CART_AJAX_SECTIONS));
152
156
 
153
157
  const key = action.payload.product.id; // shopify cart.item.key
154
158
  const cart = await getCart();
@@ -167,7 +171,7 @@ export async function synchronizeCartOptin(action: any, store: any) {
167
171
  attributes: Object.fromEntries([trackingEvent]),
168
172
  properties: item.properties,
169
173
  selling_plan: selling_plan || null,
170
- sections: closestSectionId ? [closestSectionId] : undefined
174
+ sections: sectionsToUpdate.map((el: HTMLElement) => el.id.replace(/^shopify-section-/, ''))
171
175
  })
172
176
  });
173
177
 
@@ -203,15 +207,20 @@ export async function synchronizeCartOptin(action: any, store: any) {
203
207
 
204
208
  const sections = newCart.sections;
205
209
 
206
- if (sections && closestSectionId in sections) {
207
- const sectionRawHtml = sections[closestSectionId];
210
+ if (Object.values(sections).length) {
211
+ sectionsToUpdate.forEach((sectionElement: HTMLElement) => {
212
+ const sectionId = sectionElement.id.replace(/^shopify-section-/, '');
213
+ if (!(sectionId in sections)) return;
208
214
 
209
- const el = new DOMParser()
210
- .parseFromString(sectionRawHtml.toString() || '', 'text/html')
211
- .getElementById('shopify-section-' + closestSectionId);
212
- if (el) {
213
- closestSection.innerHTML = el.innerHTML;
214
- }
215
+ const sectionRawHtml = sections[sectionId];
216
+
217
+ const el = new DOMParser()
218
+ .parseFromString(sectionRawHtml.toString() || '', 'text/html')
219
+ .getElementById(sectionElement.id);
220
+ if (el) {
221
+ sectionElement.innerHTML = el.innerHTML;
222
+ }
223
+ });
215
224
  } else if (window.location.pathname.startsWith(CART_PAGE_URL)) {
216
225
  // only do if we are on the cart page
217
226
  window.location.reload();