@atomic-solutions/woocommerce-react 0.5.0 → 0.6.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/dist/hooks/index.cjs +162 -63
- package/dist/hooks/index.cjs.map +1 -1
- package/dist/hooks/index.d.cts +2 -2
- package/dist/hooks/index.d.ts +2 -2
- package/dist/hooks/index.js +162 -65
- package/dist/hooks/index.js.map +1 -1
- package/dist/{index-DdY_1X-C.d.cts → index-DEvJWFSF.d.cts} +96 -34
- package/dist/{index-unbfwba0.d.ts → index-Dmlapl9c.d.ts} +96 -34
- package/dist/index.cjs +167 -63
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +4 -19
- package/dist/index.d.ts +4 -19
- package/dist/index.js +165 -65
- package/dist/index.js.map +1 -1
- package/dist/provider/index.d.cts +2 -2
- package/dist/provider/index.d.ts +2 -2
- package/dist/{types-_8YqnkO-.d.cts → types-Dnq60zh-.d.cts} +3 -0
- package/dist/{types-_8YqnkO-.d.ts → types-Dnq60zh-.d.ts} +3 -0
- package/package.json +2 -2
package/dist/hooks/index.d.cts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { A as AppliedCoupon, C as CartTotals,
|
|
2
|
-
export { Q as QueryKeys, p as createQueryKeys } from '../types-
|
|
1
|
+
export { A as AppliedCoupon, C as CartTotals, aj as InfiniteQueryOptions, ak as MutationOptions, a as PaymentMethod, b as ProcessedCartItem, c as ProcessedCategory, d as ProcessedOrderDetails, e as ProcessedOrderLineItem, f as ProcessedProduct, g as ProcessedProductDetails, h as ProductCartState, i as ProductCategoryTreeNode, al as QueryOptions, R as ReorderInputItem, j as ReorderItemView, k as ReorderUnavailableReason, S as ShippingRate, n as UseCartViewOptions, o as UseCartViewReturn, p as UseOrderViewOptions, q as UseOrderViewReturn, r as UseProductViewOptions, s as UseProductViewReturn, t as UseProductsFeedOptions, u as UseProductsFeedReturn, v as UseReorderViewOptions, w as UseReorderViewReturn, E as getOrderStatusLabel, Q as useAddManyToCart, T as useAddToCart, V as useApplyCoupon, W as useCart, X as useCartView, Y as useCheckout, Z as useInfiniteProducts, _ as useOrder, $ as useOrderView, a0 as useProcessCheckout, a1 as useProduct, a2 as useProductCategories, a3 as useProductCategoryTree, a4 as useProductView, a5 as useProducts, a6 as useProductsFeed, a7 as useRemoveCoupon, a8 as useRemoveFromCart, a9 as useReorderView, aa as useSelectShippingRate, ab as useStore, ac as useStoreFeatures, ad as useStoreLocale, ae as useStoreTheme, af as useUpdateCartItem, ag as useUpdateCustomer, ah as useWooCommerceClient, ai as useWooCommerceQueryKeys } from '../index-DEvJWFSF.cjs';
|
|
2
|
+
export { Q as QueryKeys, p as createQueryKeys } from '../types-Dnq60zh-.cjs';
|
|
3
3
|
export { OrderStatus } from '@atomic-solutions/woocommerce-api-client';
|
|
4
4
|
import '@tanstack/react-query';
|
package/dist/hooks/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { A as AppliedCoupon, C as CartTotals,
|
|
2
|
-
export { Q as QueryKeys, p as createQueryKeys } from '../types-
|
|
1
|
+
export { A as AppliedCoupon, C as CartTotals, aj as InfiniteQueryOptions, ak as MutationOptions, a as PaymentMethod, b as ProcessedCartItem, c as ProcessedCategory, d as ProcessedOrderDetails, e as ProcessedOrderLineItem, f as ProcessedProduct, g as ProcessedProductDetails, h as ProductCartState, i as ProductCategoryTreeNode, al as QueryOptions, R as ReorderInputItem, j as ReorderItemView, k as ReorderUnavailableReason, S as ShippingRate, n as UseCartViewOptions, o as UseCartViewReturn, p as UseOrderViewOptions, q as UseOrderViewReturn, r as UseProductViewOptions, s as UseProductViewReturn, t as UseProductsFeedOptions, u as UseProductsFeedReturn, v as UseReorderViewOptions, w as UseReorderViewReturn, E as getOrderStatusLabel, Q as useAddManyToCart, T as useAddToCart, V as useApplyCoupon, W as useCart, X as useCartView, Y as useCheckout, Z as useInfiniteProducts, _ as useOrder, $ as useOrderView, a0 as useProcessCheckout, a1 as useProduct, a2 as useProductCategories, a3 as useProductCategoryTree, a4 as useProductView, a5 as useProducts, a6 as useProductsFeed, a7 as useRemoveCoupon, a8 as useRemoveFromCart, a9 as useReorderView, aa as useSelectShippingRate, ab as useStore, ac as useStoreFeatures, ad as useStoreLocale, ae as useStoreTheme, af as useUpdateCartItem, ag as useUpdateCustomer, ah as useWooCommerceClient, ai as useWooCommerceQueryKeys } from '../index-Dmlapl9c.js';
|
|
2
|
+
export { Q as QueryKeys, p as createQueryKeys } from '../types-Dnq60zh-.js';
|
|
3
3
|
export { OrderStatus } from '@atomic-solutions/woocommerce-api-client';
|
|
4
4
|
import '@tanstack/react-query';
|
package/dist/hooks/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { createContext, useContext, useMemo, useCallback, useState } from 'react';
|
|
2
2
|
import { useQuery, useQueryClient, useMutation, useInfiniteQuery } from '@tanstack/react-query';
|
|
3
|
-
import { WooCommerceApiError } from '@atomic-solutions/woocommerce-api-client';
|
|
3
|
+
import { WooCommerceApiError, mapWooCommerceCode } from '@atomic-solutions/woocommerce-api-client';
|
|
4
4
|
|
|
5
5
|
// src/hooks/useWooCommerceClient.ts
|
|
6
6
|
var WooCommerceContext = createContext(null);
|
|
@@ -193,6 +193,14 @@ var useCart = (options) => {
|
|
|
193
193
|
getQuantity
|
|
194
194
|
};
|
|
195
195
|
};
|
|
196
|
+
var toWooError = (error) => error instanceof WooCommerceApiError ? error : new WooCommerceApiError({
|
|
197
|
+
code: "unknown_error",
|
|
198
|
+
message: error instanceof Error ? error.message : "Unknown error",
|
|
199
|
+
statusCode: 500,
|
|
200
|
+
url: "",
|
|
201
|
+
method: "POST",
|
|
202
|
+
originalCode: "unknown_error"
|
|
203
|
+
});
|
|
196
204
|
var useAddToCart = () => {
|
|
197
205
|
const client = useWooCommerceClient();
|
|
198
206
|
const queryKeys = useWooCommerceQueryKeys();
|
|
@@ -211,15 +219,50 @@ var useAddToCart = () => {
|
|
|
211
219
|
},
|
|
212
220
|
onError: (error) => {
|
|
213
221
|
if (emitEvent) {
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
222
|
+
emitEvent(createErrorEvent("cart:item_added", toWooError(error)));
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
});
|
|
226
|
+
};
|
|
227
|
+
var useAddManyToCart = () => {
|
|
228
|
+
const client = useWooCommerceClient();
|
|
229
|
+
const queryKeys = useWooCommerceQueryKeys();
|
|
230
|
+
const queryClient = useQueryClient();
|
|
231
|
+
const emitEvent = useEventEmitter();
|
|
232
|
+
return useMutation({
|
|
233
|
+
mutationKey: queryKeys.cart.mutations.addItems(),
|
|
234
|
+
mutationFn: (input) => client.cart.addItems(input),
|
|
235
|
+
onSuccess: ({ cart, results }) => {
|
|
236
|
+
queryClient.setQueryData(queryKeys.cart.details(), cart);
|
|
237
|
+
queryClient.invalidateQueries({ queryKey: queryKeys.cart.all });
|
|
238
|
+
if (!emitEvent) return;
|
|
239
|
+
results.forEach((result) => {
|
|
240
|
+
if (result.status >= 200 && result.status < 300) {
|
|
241
|
+
const addedItem = cart.items.find((item) => item.id === result.item.id);
|
|
242
|
+
if (addedItem) {
|
|
243
|
+
emitEvent(createSuccessEvent("cart:item_added", { item: addedItem, cart }));
|
|
244
|
+
}
|
|
245
|
+
return;
|
|
246
|
+
}
|
|
247
|
+
emitEvent(
|
|
248
|
+
createErrorEvent(
|
|
249
|
+
"cart:item_added",
|
|
250
|
+
new WooCommerceApiError({
|
|
251
|
+
code: mapWooCommerceCode(result.code),
|
|
252
|
+
originalCode: result.code ?? "unknown_error",
|
|
253
|
+
message: result.message ?? "Failed to add item to cart",
|
|
254
|
+
statusCode: result.status,
|
|
255
|
+
url: "",
|
|
256
|
+
method: "POST",
|
|
257
|
+
requestBody: result.item
|
|
258
|
+
})
|
|
259
|
+
)
|
|
260
|
+
);
|
|
261
|
+
});
|
|
262
|
+
},
|
|
263
|
+
onError: (error) => {
|
|
264
|
+
if (emitEvent) {
|
|
265
|
+
emitEvent(createErrorEvent("cart:item_added", toWooError(error)));
|
|
223
266
|
}
|
|
224
267
|
}
|
|
225
268
|
});
|
|
@@ -242,15 +285,7 @@ var useUpdateCartItem = () => {
|
|
|
242
285
|
},
|
|
243
286
|
onError: (error) => {
|
|
244
287
|
if (emitEvent) {
|
|
245
|
-
|
|
246
|
-
code: "unknown_error",
|
|
247
|
-
message: error instanceof Error ? error.message : "Unknown error",
|
|
248
|
-
statusCode: 500,
|
|
249
|
-
url: "",
|
|
250
|
-
method: "POST",
|
|
251
|
-
originalCode: "unknown_error"
|
|
252
|
-
});
|
|
253
|
-
emitEvent(createErrorEvent("cart:item_updated", wooError));
|
|
288
|
+
emitEvent(createErrorEvent("cart:item_updated", toWooError(error)));
|
|
254
289
|
}
|
|
255
290
|
}
|
|
256
291
|
});
|
|
@@ -272,15 +307,7 @@ var useRemoveFromCart = () => {
|
|
|
272
307
|
},
|
|
273
308
|
onError: (error) => {
|
|
274
309
|
if (emitEvent) {
|
|
275
|
-
|
|
276
|
-
code: "unknown_error",
|
|
277
|
-
message: error instanceof Error ? error.message : "Unknown error",
|
|
278
|
-
statusCode: 500,
|
|
279
|
-
url: "",
|
|
280
|
-
method: "POST",
|
|
281
|
-
originalCode: "unknown_error"
|
|
282
|
-
});
|
|
283
|
-
emitEvent(createErrorEvent("cart:item_removed", wooError));
|
|
310
|
+
emitEvent(createErrorEvent("cart:item_removed", toWooError(error)));
|
|
284
311
|
}
|
|
285
312
|
}
|
|
286
313
|
});
|
|
@@ -302,15 +329,7 @@ var useApplyCoupon = () => {
|
|
|
302
329
|
},
|
|
303
330
|
onError: (error) => {
|
|
304
331
|
if (emitEvent) {
|
|
305
|
-
|
|
306
|
-
code: "unknown_error",
|
|
307
|
-
message: error instanceof Error ? error.message : "Unknown error",
|
|
308
|
-
statusCode: 500,
|
|
309
|
-
url: "",
|
|
310
|
-
method: "POST",
|
|
311
|
-
originalCode: "unknown_error"
|
|
312
|
-
});
|
|
313
|
-
emitEvent(createErrorEvent("cart:coupon_applied", wooError));
|
|
332
|
+
emitEvent(createErrorEvent("cart:coupon_applied", toWooError(error)));
|
|
314
333
|
}
|
|
315
334
|
}
|
|
316
335
|
});
|
|
@@ -332,15 +351,7 @@ var useRemoveCoupon = () => {
|
|
|
332
351
|
},
|
|
333
352
|
onError: (error) => {
|
|
334
353
|
if (emitEvent) {
|
|
335
|
-
|
|
336
|
-
code: "unknown_error",
|
|
337
|
-
message: error instanceof Error ? error.message : "Unknown error",
|
|
338
|
-
statusCode: 500,
|
|
339
|
-
url: "",
|
|
340
|
-
method: "POST",
|
|
341
|
-
originalCode: "unknown_error"
|
|
342
|
-
});
|
|
343
|
-
emitEvent(createErrorEvent("cart:coupon_removed", wooError));
|
|
354
|
+
emitEvent(createErrorEvent("cart:coupon_removed", toWooError(error)));
|
|
344
355
|
}
|
|
345
356
|
}
|
|
346
357
|
});
|
|
@@ -362,15 +373,7 @@ var useUpdateCustomer = () => {
|
|
|
362
373
|
},
|
|
363
374
|
onError: (error) => {
|
|
364
375
|
if (emitEvent) {
|
|
365
|
-
|
|
366
|
-
code: "unknown_error",
|
|
367
|
-
message: error instanceof Error ? error.message : "Unknown error",
|
|
368
|
-
statusCode: 500,
|
|
369
|
-
url: "",
|
|
370
|
-
method: "POST",
|
|
371
|
-
originalCode: "unknown_error"
|
|
372
|
-
});
|
|
373
|
-
emitEvent(createErrorEvent("cart:customer_updated", wooError));
|
|
376
|
+
emitEvent(createErrorEvent("cart:customer_updated", toWooError(error)));
|
|
374
377
|
}
|
|
375
378
|
}
|
|
376
379
|
});
|
|
@@ -392,15 +395,7 @@ var useSelectShippingRate = () => {
|
|
|
392
395
|
},
|
|
393
396
|
onError: (error) => {
|
|
394
397
|
if (emitEvent) {
|
|
395
|
-
|
|
396
|
-
code: "unknown_error",
|
|
397
|
-
message: error instanceof Error ? error.message : "Unknown error",
|
|
398
|
-
statusCode: 500,
|
|
399
|
-
url: "",
|
|
400
|
-
method: "POST",
|
|
401
|
-
originalCode: "unknown_error"
|
|
402
|
-
});
|
|
403
|
-
emitEvent(createErrorEvent("cart:shipping_selected", wooError));
|
|
398
|
+
emitEvent(createErrorEvent("cart:shipping_selected", toWooError(error)));
|
|
404
399
|
}
|
|
405
400
|
}
|
|
406
401
|
});
|
|
@@ -477,6 +472,7 @@ var createQueryKeys = (prefix = ["woocommerce"]) => ({
|
|
|
477
472
|
// Mutation keys
|
|
478
473
|
mutations: {
|
|
479
474
|
addItem: () => [...prefix, "cart", "mutation", "addItem"],
|
|
475
|
+
addItems: () => [...prefix, "cart", "mutation", "addItems"],
|
|
480
476
|
updateItem: () => [...prefix, "cart", "mutation", "updateItem"],
|
|
481
477
|
removeItem: () => [...prefix, "cart", "mutation", "removeItem"],
|
|
482
478
|
applyCoupon: () => [...prefix, "cart", "mutation", "applyCoupon"],
|
|
@@ -697,6 +693,15 @@ function isPurchasable(status, quantity, manageStock = true) {
|
|
|
697
693
|
}
|
|
698
694
|
return true;
|
|
699
695
|
}
|
|
696
|
+
function unavailableReason(product) {
|
|
697
|
+
if (!product.is_in_stock || product.is_on_backorder || product.add_to_cart.maximum <= 0) {
|
|
698
|
+
return "out_of_stock";
|
|
699
|
+
}
|
|
700
|
+
if (!product.is_purchasable) {
|
|
701
|
+
return "not_purchasable";
|
|
702
|
+
}
|
|
703
|
+
return void 0;
|
|
704
|
+
}
|
|
700
705
|
function isLowStock(status, quantity, threshold = 5) {
|
|
701
706
|
return status === "instock" && quantity !== null && quantity !== void 0 && quantity <= threshold && quantity > 0;
|
|
702
707
|
}
|
|
@@ -1133,6 +1138,98 @@ function useCartView(options = {}) {
|
|
|
1133
1138
|
isBusy
|
|
1134
1139
|
};
|
|
1135
1140
|
}
|
|
1141
|
+
function useReorderView(items, options = {}) {
|
|
1142
|
+
const { imageSize = "thumbnail" } = options;
|
|
1143
|
+
const storeContext = useContext(WooCommerceStoreContext);
|
|
1144
|
+
const storeConfig = storeContext?.config;
|
|
1145
|
+
const localeTag = options.localeTag ?? storeConfig?.locale?.localeTag ?? DEFAULT_STORE_CONFIG.locale.localeTag;
|
|
1146
|
+
const fallbackImageUrl = options.fallbackImageUrl ?? storeConfig?.fallbackImageUrl;
|
|
1147
|
+
const imageUrlTransformer = storeConfig?.imageUrlTransformer;
|
|
1148
|
+
const productIds = useMemo(() => [...new Set(items.map((item) => item.productId))], [items]);
|
|
1149
|
+
const {
|
|
1150
|
+
data: productsPage,
|
|
1151
|
+
isLoading,
|
|
1152
|
+
error
|
|
1153
|
+
} = useProducts(
|
|
1154
|
+
{ include: productIds, page: 1, per_page: 100 },
|
|
1155
|
+
{ enabled: productIds.length > 0 }
|
|
1156
|
+
);
|
|
1157
|
+
const { data: cart } = useCart();
|
|
1158
|
+
const getProcessedImageUrl = useCallback(
|
|
1159
|
+
(images) => {
|
|
1160
|
+
const baseUrl = getImageUrl(images, imageSize, fallbackImageUrl);
|
|
1161
|
+
if (baseUrl && imageUrlTransformer) {
|
|
1162
|
+
return imageUrlTransformer(baseUrl, imageSize);
|
|
1163
|
+
}
|
|
1164
|
+
return baseUrl;
|
|
1165
|
+
},
|
|
1166
|
+
[imageSize, fallbackImageUrl, imageUrlTransformer]
|
|
1167
|
+
);
|
|
1168
|
+
const views = useMemo(() => {
|
|
1169
|
+
const products = new Map((productsPage?.data ?? []).map((product) => [product.id, product]));
|
|
1170
|
+
return items.map((item) => {
|
|
1171
|
+
const product = products.get(item.productId);
|
|
1172
|
+
if (!product) {
|
|
1173
|
+
return {
|
|
1174
|
+
productId: item.productId,
|
|
1175
|
+
name: "",
|
|
1176
|
+
requestedQuantity: item.quantity,
|
|
1177
|
+
minQuantity: 1,
|
|
1178
|
+
maxQuantity: 0,
|
|
1179
|
+
unitPrice: "-",
|
|
1180
|
+
lineTotal: "-",
|
|
1181
|
+
reason: "missing"
|
|
1182
|
+
};
|
|
1183
|
+
}
|
|
1184
|
+
const currencyCode = product.prices.currency_code;
|
|
1185
|
+
const minQuantity = product.add_to_cart.minimum || 1;
|
|
1186
|
+
const maxQuantity = product.add_to_cart.maximum;
|
|
1187
|
+
const step = product.add_to_cart.multiple_of || 1;
|
|
1188
|
+
const upperQuantity = maxQuantity > 0 ? maxQuantity : item.quantity;
|
|
1189
|
+
const clampedQuantity = Math.min(Math.max(item.quantity, minQuantity), upperQuantity);
|
|
1190
|
+
const requestedQuantity = step > 1 ? Math.max(step, Math.floor(clampedQuantity / step) * step) : clampedQuantity;
|
|
1191
|
+
const unitAmount = Number(product.prices.price);
|
|
1192
|
+
const reason = unavailableReason(product);
|
|
1193
|
+
return {
|
|
1194
|
+
productId: product.id,
|
|
1195
|
+
name: product.name,
|
|
1196
|
+
imageUrl: getProcessedImageUrl(product.images),
|
|
1197
|
+
requestedQuantity,
|
|
1198
|
+
minQuantity,
|
|
1199
|
+
maxQuantity,
|
|
1200
|
+
unitPrice: formatStoreApiPriceIntl(product.prices.price, currencyCode, localeTag),
|
|
1201
|
+
lineTotal: formatStoreApiPriceIntl(
|
|
1202
|
+
Number.isNaN(unitAmount) ? null : String(unitAmount * requestedQuantity),
|
|
1203
|
+
currencyCode,
|
|
1204
|
+
localeTag
|
|
1205
|
+
),
|
|
1206
|
+
...reason ? { reason } : {},
|
|
1207
|
+
raw: product
|
|
1208
|
+
};
|
|
1209
|
+
});
|
|
1210
|
+
}, [items, productsPage?.data, localeTag, getProcessedImageUrl]);
|
|
1211
|
+
const cartProductIds = useMemo(
|
|
1212
|
+
() => new Set((cart?.items ?? []).map((item) => item.id)),
|
|
1213
|
+
[cart?.items]
|
|
1214
|
+
);
|
|
1215
|
+
const { addable, inCart, unavailable } = useMemo(
|
|
1216
|
+
() => ({
|
|
1217
|
+
addable: views.filter((view) => !view.reason && !cartProductIds.has(view.productId)),
|
|
1218
|
+
inCart: views.filter((view) => !view.reason && cartProductIds.has(view.productId)),
|
|
1219
|
+
unavailable: views.filter((view) => !!view.reason)
|
|
1220
|
+
}),
|
|
1221
|
+
[views, cartProductIds]
|
|
1222
|
+
);
|
|
1223
|
+
return {
|
|
1224
|
+
addable,
|
|
1225
|
+
inCart,
|
|
1226
|
+
unavailable,
|
|
1227
|
+
availableCount: addable.length + inCart.length,
|
|
1228
|
+
totalCount: views.length,
|
|
1229
|
+
isLoading,
|
|
1230
|
+
error
|
|
1231
|
+
};
|
|
1232
|
+
}
|
|
1136
1233
|
var ORDER_STATUS_LABELS = {
|
|
1137
1234
|
pending: "Pending Payment",
|
|
1138
1235
|
processing: "Processing",
|
|
@@ -1182,6 +1279,6 @@ function useOrderView(input, options = {}) {
|
|
|
1182
1279
|
};
|
|
1183
1280
|
}
|
|
1184
1281
|
|
|
1185
|
-
export { createQueryKeys, getOrderStatusLabel, useAddToCart, useApplyCoupon, useCart, useCartView, useCheckout, useInfiniteProducts, useOrder, useOrderView, useProcessCheckout, useProduct, useProductCategories, useProductCategoryTree, useProductView, useProducts, useProductsFeed, useRemoveCoupon, useRemoveFromCart, useSelectShippingRate, useStore, useStoreFeatures, useStoreLocale, useStoreTheme, useUpdateCartItem, useUpdateCustomer, useWooCommerceClient, useWooCommerceQueryKeys };
|
|
1282
|
+
export { createQueryKeys, getOrderStatusLabel, useAddManyToCart, useAddToCart, useApplyCoupon, useCart, useCartView, useCheckout, useInfiniteProducts, useOrder, useOrderView, useProcessCheckout, useProduct, useProductCategories, useProductCategoryTree, useProductView, useProducts, useProductsFeed, useRemoveCoupon, useRemoveFromCart, useReorderView, useSelectShippingRate, useStore, useStoreFeatures, useStoreLocale, useStoreTheme, useUpdateCartItem, useUpdateCustomer, useWooCommerceClient, useWooCommerceQueryKeys };
|
|
1186
1283
|
//# sourceMappingURL=index.js.map
|
|
1187
1284
|
//# sourceMappingURL=index.js.map
|