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