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