@atomic-solutions/woocommerce-react 0.6.0 → 0.6.2
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 +18 -42
- package/dist/hooks/index.cjs.map +1 -1
- package/dist/hooks/index.js +18 -42
- package/dist/hooks/index.js.map +1 -1
- package/dist/index.cjs +78 -100
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +78 -100
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -629,7 +629,6 @@ var useCart = (options) => {
|
|
|
629
629
|
var toWooError = (error) => error instanceof WooCommerceApiError ? error : new WooCommerceApiError({
|
|
630
630
|
code: "unknown_error",
|
|
631
631
|
message: error instanceof Error ? error.message : "Unknown error",
|
|
632
|
-
statusCode: 500,
|
|
633
632
|
url: "",
|
|
634
633
|
method: "POST",
|
|
635
634
|
originalCode: "unknown_error"
|
|
@@ -864,7 +863,6 @@ var useProcessCheckout = () => {
|
|
|
864
863
|
const wooError = error instanceof WooCommerceApiError ? error : new WooCommerceApiError({
|
|
865
864
|
code: "unknown_error",
|
|
866
865
|
message: error instanceof Error ? error.message : "Unknown error",
|
|
867
|
-
statusCode: 500,
|
|
868
866
|
url: "",
|
|
869
867
|
method: "POST",
|
|
870
868
|
originalCode: "unknown_error"
|
|
@@ -980,64 +978,6 @@ function useStoreTheme(options = {}) {
|
|
|
980
978
|
};
|
|
981
979
|
}
|
|
982
980
|
|
|
983
|
-
// src/utils/image.ts
|
|
984
|
-
var SIZE_SUFFIXES = {
|
|
985
|
-
thumbnail: "-150x150",
|
|
986
|
-
medium: "-300x300",
|
|
987
|
-
large: "-1024x1024",
|
|
988
|
-
full: ""
|
|
989
|
-
};
|
|
990
|
-
function getPrimaryImage(images) {
|
|
991
|
-
if (!images || images.length === 0) {
|
|
992
|
-
return void 0;
|
|
993
|
-
}
|
|
994
|
-
return images[0];
|
|
995
|
-
}
|
|
996
|
-
function getImageUrl(images, size = "full", fallback) {
|
|
997
|
-
const primary = getPrimaryImage(images);
|
|
998
|
-
if (!primary) {
|
|
999
|
-
return fallback;
|
|
1000
|
-
}
|
|
1001
|
-
return getSizedImageUrl(primary.src, size);
|
|
1002
|
-
}
|
|
1003
|
-
function getSizedImageUrl(url, size = "full") {
|
|
1004
|
-
if (!url || size === "full") {
|
|
1005
|
-
return url;
|
|
1006
|
-
}
|
|
1007
|
-
const hasSize = Object.values(SIZE_SUFFIXES).some((suffix2) => suffix2 && url.includes(suffix2));
|
|
1008
|
-
if (hasSize) {
|
|
1009
|
-
let result = url;
|
|
1010
|
-
Object.values(SIZE_SUFFIXES).forEach((suffix2) => {
|
|
1011
|
-
if (suffix2) {
|
|
1012
|
-
result = result.replace(suffix2, SIZE_SUFFIXES[size]);
|
|
1013
|
-
}
|
|
1014
|
-
});
|
|
1015
|
-
return result;
|
|
1016
|
-
}
|
|
1017
|
-
const suffix = SIZE_SUFFIXES[size];
|
|
1018
|
-
const lastDot = url.lastIndexOf(".");
|
|
1019
|
-
if (lastDot === -1) {
|
|
1020
|
-
return url;
|
|
1021
|
-
}
|
|
1022
|
-
return `${url.slice(0, lastDot)}${suffix}${url.slice(lastDot)}`;
|
|
1023
|
-
}
|
|
1024
|
-
function getAllImageUrls(images, size = "full") {
|
|
1025
|
-
if (!images || images.length === 0) {
|
|
1026
|
-
return [];
|
|
1027
|
-
}
|
|
1028
|
-
return images.map((img) => getSizedImageUrl(img.src, size));
|
|
1029
|
-
}
|
|
1030
|
-
var PLACEHOLDER_IMAGE_URL = "https://via.placeholder.com/300x300.png?text=No+Image";
|
|
1031
|
-
function getImageUrlOrPlaceholder(images, size = "medium") {
|
|
1032
|
-
return getImageUrl(images, size, PLACEHOLDER_IMAGE_URL) ?? PLACEHOLDER_IMAGE_URL;
|
|
1033
|
-
}
|
|
1034
|
-
async function preloadImage(_url) {
|
|
1035
|
-
return Promise.resolve();
|
|
1036
|
-
}
|
|
1037
|
-
async function preloadImages(urls) {
|
|
1038
|
-
await Promise.all(urls.map(preloadImage));
|
|
1039
|
-
}
|
|
1040
|
-
|
|
1041
981
|
// src/utils/price.ts
|
|
1042
982
|
var DEFAULT_LOCALE = {
|
|
1043
983
|
localeTag: "en-US",
|
|
@@ -1183,14 +1123,84 @@ function parseWooCommercePrice(priceString) {
|
|
|
1183
1123
|
return parseFloat(cleaned.replace(/,/g, ""));
|
|
1184
1124
|
}
|
|
1185
1125
|
|
|
1126
|
+
// src/utils/image.ts
|
|
1127
|
+
var SIZE_SUFFIXES = {
|
|
1128
|
+
thumbnail: "-150x150",
|
|
1129
|
+
medium: "-300x300",
|
|
1130
|
+
large: "-1024x1024",
|
|
1131
|
+
full: ""
|
|
1132
|
+
};
|
|
1133
|
+
function getPrimaryImage(images) {
|
|
1134
|
+
if (!images || images.length === 0) {
|
|
1135
|
+
return void 0;
|
|
1136
|
+
}
|
|
1137
|
+
return images[0];
|
|
1138
|
+
}
|
|
1139
|
+
function getImageUrl(images, size = "full", fallback) {
|
|
1140
|
+
const primary = getPrimaryImage(images);
|
|
1141
|
+
if (!primary) {
|
|
1142
|
+
return fallback;
|
|
1143
|
+
}
|
|
1144
|
+
return getSizedImageUrl(primary.src, size);
|
|
1145
|
+
}
|
|
1146
|
+
function getSizedImageUrl(url, size = "full") {
|
|
1147
|
+
if (!url || size === "full") {
|
|
1148
|
+
return url;
|
|
1149
|
+
}
|
|
1150
|
+
const hasSize = Object.values(SIZE_SUFFIXES).some((suffix2) => suffix2 && url.includes(suffix2));
|
|
1151
|
+
if (hasSize) {
|
|
1152
|
+
let result = url;
|
|
1153
|
+
Object.values(SIZE_SUFFIXES).forEach((suffix2) => {
|
|
1154
|
+
if (suffix2) {
|
|
1155
|
+
result = result.replace(suffix2, SIZE_SUFFIXES[size]);
|
|
1156
|
+
}
|
|
1157
|
+
});
|
|
1158
|
+
return result;
|
|
1159
|
+
}
|
|
1160
|
+
const suffix = SIZE_SUFFIXES[size];
|
|
1161
|
+
const lastDot = url.lastIndexOf(".");
|
|
1162
|
+
if (lastDot === -1) {
|
|
1163
|
+
return url;
|
|
1164
|
+
}
|
|
1165
|
+
return `${url.slice(0, lastDot)}${suffix}${url.slice(lastDot)}`;
|
|
1166
|
+
}
|
|
1167
|
+
function getAllImageUrls(images, size = "full") {
|
|
1168
|
+
if (!images || images.length === 0) {
|
|
1169
|
+
return [];
|
|
1170
|
+
}
|
|
1171
|
+
return images.map((img) => getSizedImageUrl(img.src, size));
|
|
1172
|
+
}
|
|
1173
|
+
var PLACEHOLDER_IMAGE_URL = "https://via.placeholder.com/300x300.png?text=No+Image";
|
|
1174
|
+
function getImageUrlOrPlaceholder(images, size = "medium") {
|
|
1175
|
+
return getImageUrl(images, size, PLACEHOLDER_IMAGE_URL) ?? PLACEHOLDER_IMAGE_URL;
|
|
1176
|
+
}
|
|
1177
|
+
async function preloadImage(_url) {
|
|
1178
|
+
return Promise.resolve();
|
|
1179
|
+
}
|
|
1180
|
+
async function preloadImages(urls) {
|
|
1181
|
+
await Promise.all(urls.map(preloadImage));
|
|
1182
|
+
}
|
|
1183
|
+
|
|
1184
|
+
// src/hooks/useProcessedImageUrl.ts
|
|
1185
|
+
var useProcessedImageUrl = (imageSize, fallbackOverride) => {
|
|
1186
|
+
const config = useContext(WooCommerceStoreContext)?.config;
|
|
1187
|
+
const transformer = config?.imageUrlTransformer;
|
|
1188
|
+
const fallbackImageUrl = fallbackOverride ?? config?.fallbackImageUrl;
|
|
1189
|
+
return useCallback(
|
|
1190
|
+
(images) => {
|
|
1191
|
+
const baseUrl = getImageUrl(images, imageSize, fallbackImageUrl);
|
|
1192
|
+
return baseUrl && transformer ? transformer(baseUrl, imageSize) : baseUrl;
|
|
1193
|
+
},
|
|
1194
|
+
[imageSize, fallbackImageUrl, transformer]
|
|
1195
|
+
);
|
|
1196
|
+
};
|
|
1197
|
+
|
|
1186
1198
|
// src/hooks/useProductsFeed.ts
|
|
1187
1199
|
function useProductsFeed(options = {}) {
|
|
1188
1200
|
const { params, imageSize = "medium" } = options;
|
|
1189
1201
|
const storeContext = useContext(WooCommerceStoreContext);
|
|
1190
1202
|
const storeConfig = storeContext?.config;
|
|
1191
1203
|
const localeTag = options.localeTag ?? storeConfig?.locale?.localeTag ?? DEFAULT_STORE_CONFIG.locale.localeTag;
|
|
1192
|
-
const fallbackImageUrl = options.fallbackImageUrl ?? storeConfig?.fallbackImageUrl;
|
|
1193
|
-
const imageUrlTransformer = storeConfig?.imageUrlTransformer;
|
|
1194
1204
|
const {
|
|
1195
1205
|
data: productsData,
|
|
1196
1206
|
isLoading,
|
|
@@ -1202,16 +1212,7 @@ function useProductsFeed(options = {}) {
|
|
|
1202
1212
|
isRefetching
|
|
1203
1213
|
} = useInfiniteProducts(params);
|
|
1204
1214
|
const { treeData: categoryTree = [], findById } = useProductCategoryTree();
|
|
1205
|
-
const getProcessedImageUrl =
|
|
1206
|
-
(images) => {
|
|
1207
|
-
const baseUrl = getImageUrl(images, imageSize, fallbackImageUrl);
|
|
1208
|
-
if (baseUrl && imageUrlTransformer) {
|
|
1209
|
-
return imageUrlTransformer(baseUrl, imageSize);
|
|
1210
|
-
}
|
|
1211
|
-
return baseUrl;
|
|
1212
|
-
},
|
|
1213
|
-
[imageSize, fallbackImageUrl, imageUrlTransformer]
|
|
1214
|
-
);
|
|
1215
|
+
const getProcessedImageUrl = useProcessedImageUrl(imageSize, options.fallbackImageUrl);
|
|
1215
1216
|
const products = useMemo(() => {
|
|
1216
1217
|
if (!productsData) return [];
|
|
1217
1218
|
const productList = Array.isArray(productsData) ? productsData : [];
|
|
@@ -1349,7 +1350,6 @@ function useProductView(options) {
|
|
|
1349
1350
|
const storeContext = useContext(WooCommerceStoreContext);
|
|
1350
1351
|
const storeConfig = storeContext?.config;
|
|
1351
1352
|
const localeTag = options.localeTag ?? storeConfig?.locale?.localeTag ?? DEFAULT_STORE_CONFIG.locale.localeTag;
|
|
1352
|
-
const fallbackImageUrl = options.fallbackImageUrl ?? storeConfig?.fallbackImageUrl;
|
|
1353
1353
|
const imageUrlTransformer = storeConfig?.imageUrlTransformer;
|
|
1354
1354
|
const [draftQuantity, setDraftQuantity] = useState(1);
|
|
1355
1355
|
const { data: productData, isLoading, error, refetch } = useProduct(productId);
|
|
@@ -1357,6 +1357,7 @@ function useProductView(options) {
|
|
|
1357
1357
|
const addToCartMutation = useAddToCart();
|
|
1358
1358
|
const removeFromCartMutation = useRemoveFromCart();
|
|
1359
1359
|
const updateCartMutation = useUpdateCartItem();
|
|
1360
|
+
const getProcessedImageUrl = useProcessedImageUrl(imageSize, options.fallbackImageUrl);
|
|
1360
1361
|
const processImageUrl = useCallback(
|
|
1361
1362
|
(url) => {
|
|
1362
1363
|
if (!url) return url;
|
|
@@ -1372,7 +1373,6 @@ function useProductView(options) {
|
|
|
1372
1373
|
const currencyCode = productData.prices?.currency_code;
|
|
1373
1374
|
const stockStatus = productData.is_on_backorder ? "onbackorder" : productData.is_in_stock ? "instock" : "outofstock";
|
|
1374
1375
|
const lowStockRemaining = productData.low_stock_remaining;
|
|
1375
|
-
const baseImageUrl = getImageUrl(productData.images, imageSize, fallbackImageUrl);
|
|
1376
1376
|
const baseImageUrls = getAllImageUrls(productData.images, imageSize);
|
|
1377
1377
|
return {
|
|
1378
1378
|
id: productData.id,
|
|
@@ -1382,7 +1382,7 @@ function useProductView(options) {
|
|
|
1382
1382
|
description: productData.description,
|
|
1383
1383
|
shortDescription: productData.short_description,
|
|
1384
1384
|
sku: productData.sku,
|
|
1385
|
-
imageUrl:
|
|
1385
|
+
imageUrl: getProcessedImageUrl(productData.images),
|
|
1386
1386
|
imageUrls: baseImageUrls.map((url) => processImageUrl(url) ?? url),
|
|
1387
1387
|
price: formatStoreApiPriceIntl(productData.prices?.price, currencyCode, localeTag),
|
|
1388
1388
|
regularPrice: formatStoreApiPriceIntl(
|
|
@@ -1412,7 +1412,7 @@ function useProductView(options) {
|
|
|
1412
1412
|
minQuantity: productData.add_to_cart?.minimum ?? 1,
|
|
1413
1413
|
raw: productData
|
|
1414
1414
|
};
|
|
1415
|
-
}, [productData, localeTag,
|
|
1415
|
+
}, [productData, localeTag, imageSize, lowStockThreshold, getProcessedImageUrl, processImageUrl]);
|
|
1416
1416
|
const cartItem = useMemo(() => {
|
|
1417
1417
|
return cartData?.items.find((item) => item.id === productId);
|
|
1418
1418
|
}, [cartData?.items, productId]);
|
|
@@ -1495,24 +1495,13 @@ function useCartView(options = {}) {
|
|
|
1495
1495
|
const storeContext = useContext(WooCommerceStoreContext);
|
|
1496
1496
|
const storeConfig = storeContext?.config;
|
|
1497
1497
|
const localeTag = options.localeTag ?? storeConfig?.locale?.localeTag ?? DEFAULT_STORE_CONFIG.locale.localeTag;
|
|
1498
|
-
const fallbackImageUrl = options.fallbackImageUrl ?? storeConfig?.fallbackImageUrl;
|
|
1499
|
-
const imageUrlTransformer = storeConfig?.imageUrlTransformer;
|
|
1500
1498
|
const { data: cartData, isLoading, error, refetch } = useCart();
|
|
1501
1499
|
const updateItemMutation = useUpdateCartItem();
|
|
1502
1500
|
const removeItemMutation = useRemoveFromCart();
|
|
1503
1501
|
const applyCouponMutation = useApplyCoupon();
|
|
1504
1502
|
const removeCouponMutation = useRemoveCoupon();
|
|
1505
1503
|
const selectShippingMutation = useSelectShippingRate();
|
|
1506
|
-
const getProcessedImageUrl =
|
|
1507
|
-
(images) => {
|
|
1508
|
-
const baseUrl = getImageUrl(images, imageSize, fallbackImageUrl);
|
|
1509
|
-
if (baseUrl && imageUrlTransformer) {
|
|
1510
|
-
return imageUrlTransformer(baseUrl, imageSize);
|
|
1511
|
-
}
|
|
1512
|
-
return baseUrl;
|
|
1513
|
-
},
|
|
1514
|
-
[imageSize, fallbackImageUrl, imageUrlTransformer]
|
|
1515
|
-
);
|
|
1504
|
+
const getProcessedImageUrl = useProcessedImageUrl(imageSize, options.fallbackImageUrl);
|
|
1516
1505
|
const items = useMemo(() => {
|
|
1517
1506
|
if (!cartData?.items) return [];
|
|
1518
1507
|
const currencyCode = cartData.totals.currency_code;
|
|
@@ -1658,8 +1647,6 @@ function useReorderView(items, options = {}) {
|
|
|
1658
1647
|
const storeContext = useContext(WooCommerceStoreContext);
|
|
1659
1648
|
const storeConfig = storeContext?.config;
|
|
1660
1649
|
const localeTag = options.localeTag ?? storeConfig?.locale?.localeTag ?? DEFAULT_STORE_CONFIG.locale.localeTag;
|
|
1661
|
-
const fallbackImageUrl = options.fallbackImageUrl ?? storeConfig?.fallbackImageUrl;
|
|
1662
|
-
const imageUrlTransformer = storeConfig?.imageUrlTransformer;
|
|
1663
1650
|
const productIds = useMemo(() => [...new Set(items.map((item) => item.productId))], [items]);
|
|
1664
1651
|
const {
|
|
1665
1652
|
data: productsPage,
|
|
@@ -1670,16 +1657,7 @@ function useReorderView(items, options = {}) {
|
|
|
1670
1657
|
{ enabled: productIds.length > 0 }
|
|
1671
1658
|
);
|
|
1672
1659
|
const { data: cart } = useCart();
|
|
1673
|
-
const getProcessedImageUrl =
|
|
1674
|
-
(images) => {
|
|
1675
|
-
const baseUrl = getImageUrl(images, imageSize, fallbackImageUrl);
|
|
1676
|
-
if (baseUrl && imageUrlTransformer) {
|
|
1677
|
-
return imageUrlTransformer(baseUrl, imageSize);
|
|
1678
|
-
}
|
|
1679
|
-
return baseUrl;
|
|
1680
|
-
},
|
|
1681
|
-
[imageSize, fallbackImageUrl, imageUrlTransformer]
|
|
1682
|
-
);
|
|
1660
|
+
const getProcessedImageUrl = useProcessedImageUrl(imageSize, options.fallbackImageUrl);
|
|
1683
1661
|
const views = useMemo(() => {
|
|
1684
1662
|
const products = new Map((productsPage?.data ?? []).map((product) => [product.id, product]));
|
|
1685
1663
|
return items.map((item) => {
|