@ordergroove/offers 2.27.8 → 2.27.11

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.8",
3
+ "version": "2.27.11",
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.14"
47
47
  },
48
- "gitHead": "2b681b995370886dcc2fc613c5e059062a51f152"
48
+ "gitHead": "f50c6493821c8d3fa4e5ab7b2fdc453d7f8ded20"
49
49
  }
@@ -221,7 +221,9 @@ describe('api.fetchOrders', () => {
221
221
 
222
222
  it('should call endpoint default params', async () => {
223
223
  await underTest('https://host.com', { some: 'auth' }, 2, 'foo');
224
- expect(fetchMock.lastUrl(MATCHED)).toEqual('https://host.com/orders/?status=2&ordering=foo');
224
+ expect(fetchMock.lastUrl(MATCHED)).toEqual(
225
+ 'https://host.com/orders/?status=2&ordering=foo&exclude_prepaid_orders=true'
226
+ );
225
227
  });
226
228
  });
227
229
 
package/src/core/api.js CHANGED
@@ -88,7 +88,8 @@ export const fetchOrders = memoize(
88
88
  withAuth((status = 1, ordering = 'place') => [
89
89
  `/orders/?${toQuery([
90
90
  ['status', status],
91
- ['ordering', ordering]
91
+ ['ordering', ordering],
92
+ ['exclude_prepaid_orders', 'true']
92
93
  ])}`
93
94
  ])
94
95
  )
@@ -337,32 +337,29 @@ describe('offerId', () => {
337
337
 
338
338
  describe('optedin', () => {
339
339
  it('should return optins given action SETUP_CART', () => {
340
- const actual = optedin(
341
- {},
342
- {
343
- type: constants.SETUP_CART,
344
- payload: {
345
- items: [
346
- {
347
- key: 'yum item key 1',
348
- selling_plan_allocation: {
349
- selling_plan: {
350
- id: 'yum selling plan id 1'
351
- }
340
+ const actual = optedin([], {
341
+ type: constants.SETUP_CART,
342
+ payload: {
343
+ items: [
344
+ {
345
+ key: 'yum item key 1',
346
+ selling_plan_allocation: {
347
+ selling_plan: {
348
+ id: 'yum selling plan id 1'
352
349
  }
353
- },
354
- {
355
- key: 'yum item key 2',
356
- selling_plan_allocation: {
357
- selling_plan: {
358
- id: 'yum selling plan id 2'
359
- }
350
+ }
351
+ },
352
+ {
353
+ key: 'yum item key 2',
354
+ selling_plan_allocation: {
355
+ selling_plan: {
356
+ id: 'yum selling plan id 2'
360
357
  }
361
358
  }
362
- ]
363
- }
359
+ }
360
+ ]
364
361
  }
365
- );
362
+ });
366
363
 
367
364
  expect(actual).toEqual([
368
365
  {
@@ -34,11 +34,19 @@ async function setupPdp(store, offer) {
34
34
  console.warn('OG: Unable to fetch product details for PDP', err);
35
35
  }
36
36
  }
37
-
38
37
  const form = offer.closest('form');
39
-
40
- // keep product.id form in sync with offer serving.
41
- new MutationObserver(() => syncProductId(form, offer)).observe(form, { subtree: true, childList: true });
38
+ if (form) {
39
+ const mo = new MutationObserver(() => syncProductId(form, offer));
40
+ mo.observe(form, { subtree: true, childList: true });
41
+ } else {
42
+ for (let it of document.querySelectorAll('form[action="/cart/add"] [name=id]')) {
43
+ const input = it as HTMLFormElement;
44
+ if (input.getAttribute('value') === offer.getAttribute('product')) {
45
+ const mo = new MutationObserver(() => syncProductId(input.form, offer));
46
+ mo.observe(input.form, { subtree: true, childList: true });
47
+ }
48
+ }
49
+ }
42
50
  }
43
51
 
44
52
  const getCart = async () => await (await fetch(CART_JS_URL)).json();
@@ -315,6 +323,7 @@ export default function shopifyMiddleware(store) {
315
323
  } else {
316
324
  setupPdp(store, action.payload.offer);
317
325
  }
326
+ break;
318
327
  default:
319
328
  }
320
329
 
@@ -165,13 +165,17 @@ export const offerId = (state = '', action) => 'native-shopify-offer';
165
165
  export const optedin = (state = [], action) => {
166
166
  if (constants.SETUP_CART === action.type) {
167
167
  const cart = action.payload;
168
- return cart.items.reduce(
169
- (acc, cur) =>
170
- cur.selling_plan_allocation
171
- ? [...acc, { id: cur.key, frequency: `${cur.selling_plan_allocation.selling_plan.id}` }]
172
- : acc,
173
- []
174
- );
168
+ return state
169
+ .filter(it => !it.id.includes(':'))
170
+ .concat(
171
+ cart.items.reduce(
172
+ (acc, cur) =>
173
+ cur.selling_plan_allocation
174
+ ? [...acc, { id: cur.key, frequency: `${cur.selling_plan_allocation.selling_plan.id}` }]
175
+ : acc,
176
+ []
177
+ )
178
+ );
175
179
  }
176
180
  if (constants.RECEIVE_OFFER === action.type) {
177
181
  const { autoship, autoship_by_default, in_stock, offer: offerEl } = action.payload;
@@ -186,6 +190,20 @@ export const optedin = (state = [], action) => {
186
190
  state
187
191
  );
188
192
  }
193
+
194
+ if (constants.SETUP_PRODUCT === action.type) {
195
+ const productIds = action.payload.variants.map(variant => variant.id).map(id => `${id}`);
196
+ const sellingPlanGroup = getOGSellingPlanGroup(action.payload);
197
+ const frequencies = sellingPlanGroup?.selling_plans?.map(({ id }) => `${id}`);
198
+ // if the product is in the list of variants and its frequency isn't a valid selling plan, replace with the first valid selling plan
199
+ return state.map(cur => ({
200
+ ...cur,
201
+ frequency:
202
+ productIds.some(id => id === cur.id) && !frequencies.some(freq => freq === cur.frequency)
203
+ ? frequencies[0]
204
+ : cur.frequency
205
+ }));
206
+ }
189
207
  return coreOptedin(state, action);
190
208
  };
191
209