@redotech/redo-hydrogen 1.1.3 → 1.2.0
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 +4 -0
- package/dist/cjs/index.js +1 -1
- package/dist/cjs/index.js.map +1 -1
- package/dist/esm/index.js +1 -1
- package/dist/esm/index.js.map +1 -1
- package/package.json +1 -1
- package/src/providers/redo-coverage-client.tsx +2 -0
- package/src/utils/cart.ts +67 -33
package/package.json
CHANGED
|
@@ -164,6 +164,7 @@ const useRedoCoverageClient = (): RedoCoverageClient => {
|
|
|
164
164
|
cartInfoToEnable: redoContext.cartInfoToEnable,
|
|
165
165
|
});
|
|
166
166
|
await setCartRedoEnabledAttribute({
|
|
167
|
+
cart: redoContext.cart,
|
|
167
168
|
fetcher,
|
|
168
169
|
cartInfoToEnable: redoContext.cartInfoToEnable,
|
|
169
170
|
enabled: true
|
|
@@ -180,6 +181,7 @@ const useRedoCoverageClient = (): RedoCoverageClient => {
|
|
|
180
181
|
cartInfoToEnable: redoContext.cartInfoToEnable
|
|
181
182
|
});
|
|
182
183
|
await setCartRedoEnabledAttribute({
|
|
184
|
+
cart: redoContext.cart,
|
|
183
185
|
fetcher,
|
|
184
186
|
cartInfoToEnable: redoContext.cartInfoToEnable,
|
|
185
187
|
enabled: false
|
package/src/utils/cart.ts
CHANGED
|
@@ -20,6 +20,17 @@ const getCartLines = (cart: CartReturn | CartWithActionsDocs): Array<CartLine |
|
|
|
20
20
|
}
|
|
21
21
|
}
|
|
22
22
|
|
|
23
|
+
const waitUntilCartIdle = (cart: CartWithActionsDocs): Promise<void> => {
|
|
24
|
+
return new Promise((resolve, reject) => {
|
|
25
|
+
let interval = setInterval(() => {
|
|
26
|
+
if(cart.status === 'idle') {
|
|
27
|
+
clearInterval(interval);
|
|
28
|
+
return resolve();
|
|
29
|
+
}
|
|
30
|
+
}, 100);
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
|
|
23
34
|
const addProductToCartIfNeeded = async ({
|
|
24
35
|
cart,
|
|
25
36
|
fetcher,
|
|
@@ -30,7 +41,7 @@ const addProductToCartIfNeeded = async ({
|
|
|
30
41
|
cartInfoToEnable: CartInfoToEnable
|
|
31
42
|
}) => {
|
|
32
43
|
if(!cart) {
|
|
33
|
-
return await addProductToCart({ fetcher, cartInfoToEnable });
|
|
44
|
+
return await addProductToCart({ cart, fetcher, cartInfoToEnable });
|
|
34
45
|
}
|
|
35
46
|
|
|
36
47
|
const redoProductsInCart = getCartLines(cart).filter((cartLine) => {
|
|
@@ -40,26 +51,28 @@ const addProductToCartIfNeeded = async ({
|
|
|
40
51
|
return cartLine.merchandise.id === `gid://shopify/ProductVariant/${cartInfoToEnable.variantId}`;
|
|
41
52
|
});
|
|
42
53
|
if(redoProductsInCart.length === 0) {
|
|
43
|
-
return await addProductToCart({ fetcher, cartInfoToEnable });
|
|
54
|
+
return await addProductToCart({ cart, fetcher, cartInfoToEnable });
|
|
44
55
|
} else if (redoProductsInCart.length === 1 && correctRedoProductInCart.length === 1 && correctRedoProductInCart[0].quantity === 1) {
|
|
45
56
|
// No action needed
|
|
46
57
|
return;
|
|
47
58
|
} else {
|
|
48
59
|
let isSuccess = true;
|
|
49
60
|
|
|
50
|
-
await removeLinesFromCart({ fetcher, lineIds: redoProductsInCart.map((cartLine) => cartLine.id) });
|
|
51
|
-
await addProductToCart({ fetcher, cartInfoToEnable });
|
|
61
|
+
await removeLinesFromCart({ cart, fetcher, lineIds: redoProductsInCart.map((cartLine) => cartLine.id) });
|
|
62
|
+
await addProductToCart({ cart, fetcher, cartInfoToEnable });
|
|
52
63
|
|
|
53
64
|
return;
|
|
54
65
|
}
|
|
55
66
|
};
|
|
56
67
|
|
|
57
68
|
const removeLinesFromCart = async ({
|
|
69
|
+
cart,
|
|
58
70
|
fetcher,
|
|
59
71
|
lineIds
|
|
60
72
|
}: {
|
|
61
|
-
|
|
62
|
-
|
|
73
|
+
cart: CartReturn | CartWithActionsDocs | undefined;
|
|
74
|
+
fetcher: FetcherWithComponents<unknown>;
|
|
75
|
+
lineIds: string[];
|
|
63
76
|
}) => {
|
|
64
77
|
const formInput = {
|
|
65
78
|
action: CartForm.ACTIONS.LinesRemove,
|
|
@@ -68,12 +81,17 @@ const removeLinesFromCart = async ({
|
|
|
68
81
|
}
|
|
69
82
|
}
|
|
70
83
|
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
84
|
+
if(cart && isCartWithActionsDocs(cart)) {
|
|
85
|
+
cart.linesRemove(lineIds);
|
|
86
|
+
await waitUntilCartIdle(cart);
|
|
87
|
+
} else {
|
|
88
|
+
await fetcher.submit(
|
|
89
|
+
{
|
|
90
|
+
[CartForm.INPUT_NAME]: JSON.stringify(formInput),
|
|
91
|
+
},
|
|
92
|
+
{method: 'POST', action: '/cart'},
|
|
93
|
+
);
|
|
94
|
+
}
|
|
77
95
|
};
|
|
78
96
|
|
|
79
97
|
const removeProductFromCartIfNeeded = async ({
|
|
@@ -95,15 +113,17 @@ const removeProductFromCartIfNeeded = async ({
|
|
|
95
113
|
});
|
|
96
114
|
|
|
97
115
|
if(redoProductsInCart.length !== 0) {
|
|
98
|
-
await removeLinesFromCart({ fetcher, lineIds: redoProductsInCart.map((cartLine) => cartLine.id) });
|
|
116
|
+
await removeLinesFromCart({ cart, fetcher, lineIds: redoProductsInCart.map((cartLine) => cartLine.id) });
|
|
99
117
|
} else {
|
|
100
118
|
}
|
|
101
119
|
};
|
|
102
120
|
|
|
103
121
|
const addProductToCart = async ({
|
|
122
|
+
cart,
|
|
104
123
|
fetcher,
|
|
105
|
-
cartInfoToEnable
|
|
124
|
+
cartInfoToEnable,
|
|
106
125
|
}: {
|
|
126
|
+
cart: CartReturn | CartWithActionsDocs | undefined,
|
|
107
127
|
fetcher: FetcherWithComponents<unknown>,
|
|
108
128
|
cartInfoToEnable: CartInfoToEnable
|
|
109
129
|
}) => {
|
|
@@ -122,41 +142,55 @@ const addProductToCart = async ({
|
|
|
122
142
|
}
|
|
123
143
|
}
|
|
124
144
|
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
145
|
+
if(cart && isCartWithActionsDocs(cart)) {
|
|
146
|
+
cart.linesAdd([redoProductLine]);
|
|
147
|
+
await waitUntilCartIdle(cart);
|
|
148
|
+
} else {
|
|
149
|
+
await fetcher.submit(
|
|
150
|
+
{
|
|
151
|
+
[CartForm.INPUT_NAME]: JSON.stringify(formInput),
|
|
152
|
+
},
|
|
153
|
+
{method: 'POST', action: '/cart'},
|
|
154
|
+
);
|
|
155
|
+
}
|
|
131
156
|
};
|
|
132
157
|
|
|
133
158
|
const setCartRedoEnabledAttribute = async ({
|
|
159
|
+
cart,
|
|
134
160
|
fetcher,
|
|
135
161
|
cartInfoToEnable,
|
|
136
162
|
enabled
|
|
137
163
|
}: {
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
164
|
+
cart: CartReturn | CartWithActionsDocs | undefined;
|
|
165
|
+
fetcher: FetcherWithComponents<unknown>;
|
|
166
|
+
cartInfoToEnable: CartInfoToEnable | null;
|
|
167
|
+
enabled: boolean;
|
|
141
168
|
}) => {
|
|
169
|
+
const redoCartAttribute = {
|
|
170
|
+
key: cartInfoToEnable?.cartAttribute || DEFAULT_REDO_ENABLED_CART_ATTRIBUTE,
|
|
171
|
+
value: enabled.toString()
|
|
172
|
+
};
|
|
173
|
+
|
|
142
174
|
const formInput = {
|
|
143
175
|
action: CartForm.ACTIONS.AttributesUpdateInput,
|
|
144
176
|
inputs: {
|
|
145
177
|
attributes: [
|
|
146
|
-
|
|
147
|
-
key: cartInfoToEnable?.cartAttribute || DEFAULT_REDO_ENABLED_CART_ATTRIBUTE,
|
|
148
|
-
value: enabled.toString()
|
|
149
|
-
}
|
|
178
|
+
redoCartAttribute
|
|
150
179
|
]
|
|
151
180
|
}
|
|
152
181
|
}
|
|
153
182
|
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
183
|
+
if(cart && isCartWithActionsDocs(cart)) {
|
|
184
|
+
cart.cartAttributesUpdate([redoCartAttribute]);
|
|
185
|
+
await waitUntilCartIdle(cart);
|
|
186
|
+
} else {
|
|
187
|
+
await fetcher.submit(
|
|
188
|
+
{
|
|
189
|
+
[CartForm.INPUT_NAME]: JSON.stringify(formInput),
|
|
190
|
+
},
|
|
191
|
+
{method: 'POST', action: '/cart'},
|
|
192
|
+
);
|
|
193
|
+
}
|
|
160
194
|
};
|
|
161
195
|
|
|
162
196
|
type FetcherData<T> = NonNullable<T | unknown> // FIXME: used to use SerializeFrom which is deprecated. Can this be better typed?
|