@ordergroove/offers 2.27.23-alpha-PR-672-4.2 → 2.27.23

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.
@@ -88,9 +88,7 @@ export const autoshipEligible = (state = {}, action) => {
88
88
  return cart.items.reduce(reduceProductCartLine, state);
89
89
  }
90
90
  if (constants.SETUP_PRODUCT === action.type) {
91
- const {
92
- payload: { product }
93
- } = action;
91
+ const { payload: product } = action;
94
92
  return [product, ...(product?.variants || [])]?.reduce(
95
93
  (acc, cur) => ({
96
94
  ...overrideLineKey(acc, cur.id, cur.selling_plan_allocations?.length > 0),
@@ -111,21 +109,6 @@ export function textToFreq(text) {
111
109
  return null;
112
110
  }
113
111
 
114
- export function sellingPlansToEveryPeriod(sellingPlanGroup) {
115
- return sellingPlanGroup?.selling_plans
116
- ?.map(({ options }) => options || [])
117
- .flat()
118
- .map(({ value }) => textToFreq(value));
119
- }
120
-
121
- export function sellingPlansToText(sellingPlanGroup, frequencies) {
122
- return sellingPlanGroup.options?.[0]?.values || frequencies;
123
- }
124
-
125
- export function sellingPlansToFrequencies(sellingPlanGroup) {
126
- return sellingPlanGroup?.selling_plans?.map(({ id }) => `${id}`);
127
- }
128
-
129
112
  export const config = (
130
113
  state = {
131
114
  frequencies: [],
@@ -149,16 +132,18 @@ export const config = (
149
132
  }
150
133
 
151
134
  if (constants.SETUP_PRODUCT === action.type) {
152
- const {
153
- payload: { product }
154
- } = action;
135
+ const product = action.payload;
155
136
  const sellingPlanGroup = getOGSellingPlanGroup(product);
156
- const frequencies = sellingPlansToFrequencies(sellingPlanGroup);
137
+ const frequencies = sellingPlanGroup?.selling_plans?.map(({ id }) => `${id}`);
157
138
  if (frequencies?.length) {
158
- const frequenciesEveryPeriod = sellingPlansToEveryPeriod(sellingPlanGroup);
159
- const frequenciesText = sellingPlansToText(sellingPlanGroup, frequencies);
139
+ const frequenciesEveryPeriod = sellingPlanGroup?.selling_plans
140
+ ?.map(({ options }) => options || [])
141
+ .flat()
142
+ .map(({ value }) => textToFreq(value));
143
+ const frequenciesText = sellingPlanGroup.options?.[0]?.values || frequencies;
160
144
  return {
161
145
  ...state,
146
+ defaultFrequency: frequencies[0],
162
147
  frequenciesEveryPeriod,
163
148
  frequencies,
164
149
  frequenciesText
@@ -181,9 +166,8 @@ export const inStock = (state = {}, action) => {
181
166
  }
182
167
 
183
168
  if (constants.SETUP_PRODUCT === action.type) {
184
- const {
185
- payload: { product }
186
- } = action;
169
+ const product = action.payload;
170
+
187
171
  return [product, ...product?.variants]?.reduce(productOrVariantInStockReducer, state) || state;
188
172
  }
189
173
  // force offer to refresh when requesting a new one
@@ -195,31 +179,6 @@ export const inStock = (state = {}, action) => {
195
179
 
196
180
  export const offer = (state = {}, action) => state;
197
181
 
198
- export const lookupFrequencySellingPlan = (frequencies, sellingPlans, frequency) => {
199
- const index = frequencies.findIndex(it => it === frequency);
200
- if (index >= 0) {
201
- return sellingPlans[index];
202
- }
203
-
204
- return null;
205
- };
206
-
207
- export const resolveSellingPlanFrequency = (sellingPlans, frequencies, frequency, defaultFrequency) => {
208
- if (!frequency.includes('_')) return frequency;
209
-
210
- if (sellingPlans.length !== frequencies.length) {
211
- return frequency;
212
- }
213
-
214
- let sellingPlan = lookupFrequencySellingPlan(frequencies, sellingPlans, frequency);
215
-
216
- if (!sellingPlan) {
217
- sellingPlan = lookupFrequencySellingPlan(frequencies, sellingPlans, defaultFrequency);
218
- }
219
-
220
- return sellingPlan || frequency;
221
- };
222
-
223
182
  export const optedin = (state = [], action) => {
224
183
  if (constants.SETUP_CART === action.type) {
225
184
  const cart = action.payload;
@@ -236,62 +195,32 @@ export const optedin = (state = [], action) => {
236
195
  );
237
196
  }
238
197
  if (constants.RECEIVE_OFFER === action.type) {
239
- const {
240
- autoship = {},
241
- autoship_by_default = {},
242
- default_frequencies = {},
243
- in_stock = {},
244
- offer: offerEl = {}
245
- } = action.payload;
246
- const { frequencies, frequenciesEveryPeriod } = offerEl.config || {};
247
-
248
- return Object.keys(autoship).reduce((acc, id) => {
249
- const existingOptin = state.find(it => it.id === id);
250
- if (existingOptin) return acc;
251
-
252
- const shouldBeOptedIn = autoship[id] && autoship_by_default[id] && in_stock[id];
253
-
254
- if (shouldBeOptedIn) {
255
- const defaultFrequency = default_frequencies[id]
256
- ? `${default_frequencies[id].every}_${default_frequencies[id].every_period}`
257
- : offerEl.productDefaultFrequency || '';
258
-
259
- const frequency = resolveSellingPlanFrequency(
260
- frequencies,
261
- frequenciesEveryPeriod,
262
- defaultFrequency,
263
- offerEl.defaultFrequency
264
- );
265
-
266
- return acc.concat({
267
- id,
268
- frequency
269
- });
270
- }
271
-
272
- return acc;
273
- }, state);
198
+ const { autoship, autoship_by_default, in_stock, offer: offerEl } = action.payload;
199
+
200
+ return Object.keys(autoship).reduce(
201
+ (acc, id) =>
202
+ acc.concat(
203
+ !acc.some(it => it.id === id) && autoship[id] && autoship_by_default[id] && in_stock[id]
204
+ ? { id, frequency: offerEl.defaultFrequency }
205
+ : []
206
+ ),
207
+ state
208
+ );
274
209
  }
275
210
 
276
211
  if (constants.SETUP_PRODUCT === action.type) {
277
- const { product, offer: offerEl } = action.payload;
278
- const sellingPlanGroup = getOGSellingPlanGroup(product);
279
- const frequencies = sellingPlansToFrequencies(sellingPlanGroup);
280
- const frequenciesEveryPeriod = sellingPlansToEveryPeriod(sellingPlanGroup);
281
- const defaultFrequency = offerEl.productDefaultFrequency;
282
- const productIds = product.variants.map(variant => variant.id).map(id => `${id}`);
283
- return state.map(curr => {
284
- if (productIds.some(id => id === curr.id)) {
285
- return {
286
- ...curr,
287
- frequency: resolveSellingPlanFrequency(frequencies, frequenciesEveryPeriod, curr.frequency, defaultFrequency)
288
- };
289
- }
290
-
291
- return curr;
292
- });
212
+ const productIds = action.payload.variants.map(variant => variant.id).map(id => `${id}`);
213
+ const sellingPlanGroup = getOGSellingPlanGroup(action.payload);
214
+ const frequencies = sellingPlanGroup?.selling_plans?.map(({ id }) => `${id}`);
215
+ // 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
216
+ return state.map(cur => ({
217
+ ...cur,
218
+ frequency:
219
+ productIds.some(id => id === cur.id) && !frequencies.some(freq => freq === cur.frequency)
220
+ ? frequencies[0]
221
+ : cur.frequency
222
+ }));
293
223
  }
294
-
295
224
  return coreOptedin(state, action);
296
225
  };
297
226
 
@@ -299,9 +228,7 @@ export const productOffer = (state = {}, action) => state;
299
228
 
300
229
  export const productPlans = (state = {}, action) => {
301
230
  if (constants.SETUP_PRODUCT === action.type) {
302
- const {
303
- payload: { product }
304
- } = action;
231
+ const product = action.payload;
305
232
  return (
306
233
  [product, ...product?.variants]?.reduce(
307
234
  (acc, cur) => ({