@ordergroove/offers 2.27.19 → 2.27.21
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 +21 -0
- package/dist/bundle-report.html +9 -9
- package/dist/offers.js +28 -28
- package/dist/offers.js.map +2 -2
- package/package.json +2 -2
- package/src/shopify/__tests__/shopifyMiddleware.spec.js +15 -3
- package/src/shopify/shopifyMiddleware.ts +20 -11
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ordergroove/offers",
|
|
3
|
-
"version": "2.27.
|
|
3
|
+
"version": "2.27.21",
|
|
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": "
|
|
48
|
+
"gitHead": "e2b5d12965378f8ce62ea66be35de68ac7338e9c"
|
|
49
49
|
}
|
|
@@ -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-
|
|
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":["
|
|
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
|
|
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:
|
|
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
|
|
207
|
-
|
|
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
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
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();
|