@ordergroove/offers 2.44.0 → 2.44.1-alpha-PR-1167-2.36

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.
Files changed (40) hide show
  1. package/dist/bundle-report.html +42 -39
  2. package/dist/offers.js +69 -69
  3. package/dist/offers.js.map +4 -4
  4. package/package.json +2 -2
  5. package/src/components/FrequencyStatus.js +14 -10
  6. package/src/components/Offer.js +33 -14
  7. package/src/components/OptinButton.js +2 -2
  8. package/src/components/OptinSelect.js +6 -5
  9. package/src/components/OptinStatus.js +16 -9
  10. package/src/components/Price.js +3 -3
  11. package/src/components/SelectFrequency.js +11 -6
  12. package/src/components/TestWizard.js +45 -41
  13. package/src/components/UpsellModal.js +9 -3
  14. package/src/components/__tests__/Offer.spec.js +0 -19
  15. package/src/components/__tests__/OptinStatus.spec.js +17 -4
  16. package/src/core/__tests__/actions.spec.js +47 -1
  17. package/src/core/__tests__/base.spec.js +0 -77
  18. package/src/core/__tests__/experiments.spec.js +0 -3
  19. package/src/core/__tests__/offerRequest.spec.js +2 -1
  20. package/src/core/__tests__/selectors.spec.js +7 -7
  21. package/src/core/actions-preview.js +6 -3
  22. package/src/core/actions.js +22 -13
  23. package/src/core/base.js +0 -23
  24. package/src/core/offerRequest.js +1 -1
  25. package/src/core/{reducer.js → reducer.ts} +30 -10
  26. package/src/core/selectors.ts +215 -0
  27. package/src/core/types/api.ts +71 -0
  28. package/src/core/types/reducer.ts +94 -0
  29. package/src/core/types/utility.ts +1 -0
  30. package/src/core/utils.ts +32 -15
  31. package/src/make-api.js +1 -1
  32. package/src/shopify/__tests__/reducers/config.spec.js +603 -0
  33. package/src/shopify/__tests__/shopifyReducer.spec.js +69 -744
  34. package/src/shopify/__tests__/utils.spec.js +24 -1
  35. package/src/shopify/reducers/config.ts +185 -0
  36. package/src/shopify/shopifyMiddleware.ts +2 -9
  37. package/src/shopify/{shopifyReducer.js → shopifyReducer.ts} +50 -195
  38. package/src/shopify/utils.ts +25 -0
  39. package/src/core/selectors.js +0 -192
  40. package/src/types.ts +0 -16
@@ -1,192 +0,0 @@
1
- import { createSelector } from 'reselect';
2
- import memoize from 'lodash.memoize';
3
- import { stringifyFrequency } from './api';
4
- import platform from '../platform';
5
- import { mapFrequencyToSellingPlan, safeProductId } from '../core/utils';
6
-
7
- memoize.Cache = Map;
8
-
9
- function arraysEqual(a, b) {
10
- if (a === b) return true;
11
- if (a === null || b === null) return false;
12
- if (a.length !== b.length) return false;
13
-
14
- // If you don't care about the order of the elements inside
15
- // the array, you should sort both arrays here.
16
- // Please note that calling sort on an array will modify that array.
17
- // you might want to clone your array first.
18
-
19
- for (let i = 0; i < a.length; ++i) {
20
- if (a[i] !== b[i]) return false;
21
- }
22
- return true;
23
- }
24
-
25
- function resolveFrequency(sellingPlans, frequenciesEveryPeriod, frequency) {
26
- const ogFrequency = stringifyFrequency(frequency);
27
- if (!platform.shopify_selling_plans) return ogFrequency;
28
- return mapFrequencyToSellingPlan(sellingPlans, frequenciesEveryPeriod, ogFrequency);
29
- }
30
-
31
- export const isSameProduct = (a, b) => {
32
- if (a === b) return true;
33
- if (typeof a === 'object' && typeof b === 'object' && a && b) {
34
- if (a.id === b.id) {
35
- if (!(Array.isArray(a.components) && Array.isArray(b.components))) {
36
- return true;
37
- }
38
- if (arraysEqual((a.components || []).sort(), (b.components || []).sort())) {
39
- return true;
40
- }
41
- }
42
- }
43
- return false;
44
- };
45
-
46
- /**
47
- * Returns a list of opted in products id from the state
48
- * @param {object} state
49
- */
50
- export const optedinSelector = state => state.optedin || [];
51
-
52
- export const optedoutSelector = state => state.optedout || [];
53
-
54
- export const autoshipSelector = state => state.autoshipByDefault || {};
55
-
56
- export const defaultFrequenciesSelector = state => state.defaultFrequencies || {};
57
-
58
- export const sellingPlansSelector = state => state?.config?.frequencies || [];
59
-
60
- export const frequenciesEveryPeriodSelector = state => state?.config?.frequenciesEveryPeriod || [];
61
-
62
- export const prepaidSellingPlansSelector = state => state?.config?.prepaidSellingPlans || [];
63
- export const prepaidShipmentsSelectedSelector = state => state?.prepaidShipmentsSelected || {};
64
-
65
- /**
66
- * Creates a function with state arguments that return the true when
67
- * productId is in the optedin array or not in optedout or autoship by default
68
- * @param {String} productId
69
- */
70
- export const makeOptedinSelector = memoize(
71
- product =>
72
- createSelector(optedinSelector, optedoutSelector, autoshipSelector, (optedin, optedout, autoshipByDefault) => {
73
- const entry = optedin.find(b => isSameProduct(product, b));
74
- if (entry) {
75
- return entry;
76
- }
77
- if (optedout.find(b => isSameProduct(product, b))) {
78
- return false;
79
- }
80
- if (product && autoshipByDefault[product.id]) {
81
- return { id: product.id };
82
- }
83
- return false;
84
- }),
85
- product => JSON.stringify(product)
86
- );
87
- /**
88
- * Creates a function with state arguments that return the true when
89
- * productId is in the optedin array
90
- * @param {String} productId
91
- */
92
- export const makeSubscribedSelector = memoize(
93
- product =>
94
- createSelector(optedinSelector, optedin => {
95
- const entry = optedin.find(b => isSameProduct(product, b));
96
- if (entry) {
97
- return entry;
98
- }
99
- return false;
100
- }),
101
- product => JSON.stringify(product)
102
- );
103
-
104
- export const makePrepaidSubscribedSelector = memoize(
105
- product =>
106
- createSelector(optedinSelector, optedin => optedin.some(b => isSameProduct(product, b) && b.prepaidShipments)),
107
- product => JSON.stringify(product)
108
- );
109
-
110
- export const makePrepaidShipmentsSelectedSelector = memoize(
111
- product =>
112
- createSelector(
113
- prepaidShipmentsSelectedSelector,
114
- prepaidShipmentsSelected => prepaidShipmentsSelected[product.id] || null
115
- ),
116
- product => JSON.stringify(product)
117
- );
118
-
119
- /**
120
- * Creates a function with state arguments that return the true when
121
- * productId is in the optedout array
122
- * @param {String} productId
123
- */
124
- export const makeOptedoutSelector = memoize(productId =>
125
- createSelector(optedoutSelector, optedout => optedout.find(b => isSameProduct(productId, b)))
126
- );
127
-
128
- export const frequencySelector = state => state.frequency;
129
-
130
- export const makeProductFrequencySelector = memoize(productId =>
131
- createSelector(makeOptedinSelector(productId), productOptin => (productOptin && productOptin.frequency) || null)
132
- );
133
-
134
- export const makeProductPrepaidShipmentsOptedInSelector = memoize(product =>
135
- createSelector(makeOptedinSelector(product), productOptin => (productOptin && productOptin.prepaidShipments) || null)
136
- );
137
-
138
- export const makeProductPrepaidShipmentOptionsSelector = memoize(productId =>
139
- createSelector(prepaidSellingPlansSelector, prepaidSellingPlans => {
140
- const shipmentsList =
141
- prepaidSellingPlans[safeProductId(productId)]?.map(({ numberShipments }) => numberShipments) || [];
142
- return shipmentsList.sort((a, b) => a - b);
143
- })
144
- );
145
-
146
- /**
147
- * Creates a function with state arguments that returns stringified frequency
148
- * if default frequency exists for the product
149
- * @param {String} productId
150
- */
151
- export const makeProductDefaultFrequencySelector = memoize(productId =>
152
- createSelector(
153
- defaultFrequenciesSelector,
154
- sellingPlansSelector,
155
- frequenciesEveryPeriodSelector,
156
- (defaultFrequencies, sellingPlans, frequenciesEveryPeriod) =>
157
- (defaultFrequencies[safeProductId(productId)] &&
158
- resolveFrequency(sellingPlans, frequenciesEveryPeriod, defaultFrequencies[safeProductId(productId)])) ||
159
- null
160
- )
161
- );
162
-
163
- /**
164
- * Convert a string from camel case to kebab case.
165
- * @param {String} string
166
- * @returns {String}
167
- */
168
- export const kebabCase = string => {
169
- return string.replace(/([a-z0-9]|(?=[A-Z]))([A-Z])/g, '$1-$2').toLowerCase();
170
- };
171
-
172
- /**
173
- * Resolve element configuration by looking into html attribute or state config
174
- *
175
- * @param {Object} state
176
- * @param {HTMLElement} element
177
- * @param {String} key
178
- * @returns {Object}
179
- */
180
- export const configSelector = (state, element, key, defaultValue) => ({
181
- [key]:
182
- (state.config && state.config[key]) ||
183
- (element && element.hasAttribute && element.hasAttribute(kebabCase(key)) && element[key]) ||
184
- (element.offer && typeof (element.offer[key] !== 'undefined') && element.offer[key]) ||
185
- defaultValue
186
- });
187
-
188
- /**
189
- * Returns a list of opted in products id from the state
190
- * @param {object} state
191
- */
192
- export const templatesSelector = state => ({ templates: state.templates || [] });
package/src/types.ts DELETED
@@ -1,16 +0,0 @@
1
- export type ExperimentVariant = {
2
- public_id: string;
3
- parameters: any;
4
- weight: number;
5
- };
6
-
7
- export type ExperimentConfig = {
8
- public_id: string;
9
- variants: ExperimentVariant[];
10
- };
11
-
12
- export interface MerchantSettings {
13
- currency_code: string;
14
- multicurrency_enabled: boolean;
15
- experiments?: ExperimentConfig;
16
- }