@atomic-solutions/woocommerce-react 0.6.1 → 0.7.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.
@@ -42,6 +42,8 @@ var StaleTimes = {
42
42
  product: STALE_TIME_SHORT,
43
43
  /** Product categories - always fresh */
44
44
  categories: STALE_TIME_NONE,
45
+ /** Product tags - names rarely change, 1 hour */
46
+ tags: STALE_TIME_LONG,
45
47
  /** Orders - always fresh */
46
48
  orders: STALE_TIME_NONE,
47
49
  /** Default for infinite queries - 1 hour */
@@ -139,6 +141,16 @@ var processCategoriesToTree = (data) => {
139
141
  });
140
142
  return rootCategories?.map(processCategory) ?? [];
141
143
  };
144
+ var useProductTags = (params, options) => {
145
+ const client = useWooCommerceClient();
146
+ const queryKeys = useWooCommerceQueryKeys();
147
+ return reactQuery.useQuery({
148
+ queryKey: queryKeys.products.tags(params),
149
+ queryFn: () => client.products.tags(params),
150
+ staleTime: StaleTimes.tags,
151
+ ...options
152
+ });
153
+ };
142
154
  var useProductCategoryTree = () => {
143
155
  const query = useProductCategories();
144
156
  const flatData = query.data ?? [];
@@ -463,7 +475,9 @@ var createQueryKeys = (prefix = ["woocommerce"]) => ({
463
475
  list: (params) => [...prefix, "products", "list", params],
464
476
  details: () => [...prefix, "products", "detail"],
465
477
  detail: (id) => [...prefix, "products", "detail", id],
466
- categories: () => [...prefix, "products", "categories"]
478
+ categories: () => [...prefix, "products", "categories"],
479
+ // Params are in the key, so each set of tag ids is cached apart.
480
+ tags: (params) => [...prefix, "products", "tags", params]
467
481
  },
468
482
  // Cart
469
483
  cart: {
@@ -753,6 +767,18 @@ function getAllImageUrls(images, size = "full") {
753
767
  }
754
768
  return images.map((img) => getSizedImageUrl(img.src, size));
755
769
  }
770
+ var useProcessedImageUrl = (imageSize, fallbackOverride) => {
771
+ const config = react.useContext(WooCommerceStoreContext)?.config;
772
+ const transformer = config?.imageUrlTransformer;
773
+ const fallbackImageUrl = fallbackOverride ?? config?.fallbackImageUrl;
774
+ return react.useCallback(
775
+ (images) => {
776
+ const baseUrl = getImageUrl(images, imageSize, fallbackImageUrl);
777
+ return baseUrl && transformer ? transformer(baseUrl, imageSize) : baseUrl;
778
+ },
779
+ [imageSize, fallbackImageUrl, transformer]
780
+ );
781
+ };
756
782
 
757
783
  // src/hooks/useProductsFeed.ts
758
784
  function useProductsFeed(options = {}) {
@@ -760,8 +786,6 @@ function useProductsFeed(options = {}) {
760
786
  const storeContext = react.useContext(WooCommerceStoreContext);
761
787
  const storeConfig = storeContext?.config;
762
788
  const localeTag = options.localeTag ?? storeConfig?.locale?.localeTag ?? DEFAULT_STORE_CONFIG.locale.localeTag;
763
- const fallbackImageUrl = options.fallbackImageUrl ?? storeConfig?.fallbackImageUrl;
764
- const imageUrlTransformer = storeConfig?.imageUrlTransformer;
765
789
  const {
766
790
  data: productsData,
767
791
  isLoading,
@@ -773,16 +797,7 @@ function useProductsFeed(options = {}) {
773
797
  isRefetching
774
798
  } = useInfiniteProducts(params);
775
799
  const { treeData: categoryTree = [], findById } = useProductCategoryTree();
776
- const getProcessedImageUrl = react.useCallback(
777
- (images) => {
778
- const baseUrl = getImageUrl(images, imageSize, fallbackImageUrl);
779
- if (baseUrl && imageUrlTransformer) {
780
- return imageUrlTransformer(baseUrl, imageSize);
781
- }
782
- return baseUrl;
783
- },
784
- [imageSize, fallbackImageUrl, imageUrlTransformer]
785
- );
800
+ const getProcessedImageUrl = useProcessedImageUrl(imageSize, options.fallbackImageUrl);
786
801
  const products = react.useMemo(() => {
787
802
  if (!productsData) return [];
788
803
  const productList = Array.isArray(productsData) ? productsData : [];
@@ -834,7 +849,6 @@ function useProductView(options) {
834
849
  const storeContext = react.useContext(WooCommerceStoreContext);
835
850
  const storeConfig = storeContext?.config;
836
851
  const localeTag = options.localeTag ?? storeConfig?.locale?.localeTag ?? DEFAULT_STORE_CONFIG.locale.localeTag;
837
- const fallbackImageUrl = options.fallbackImageUrl ?? storeConfig?.fallbackImageUrl;
838
852
  const imageUrlTransformer = storeConfig?.imageUrlTransformer;
839
853
  const [draftQuantity, setDraftQuantity] = react.useState(1);
840
854
  const { data: productData, isLoading, error, refetch } = useProduct(productId);
@@ -842,6 +856,7 @@ function useProductView(options) {
842
856
  const addToCartMutation = useAddToCart();
843
857
  const removeFromCartMutation = useRemoveFromCart();
844
858
  const updateCartMutation = useUpdateCartItem();
859
+ const getProcessedImageUrl = useProcessedImageUrl(imageSize, options.fallbackImageUrl);
845
860
  const processImageUrl = react.useCallback(
846
861
  (url) => {
847
862
  if (!url) return url;
@@ -857,7 +872,6 @@ function useProductView(options) {
857
872
  const currencyCode = productData.prices?.currency_code;
858
873
  const stockStatus = productData.is_on_backorder ? "onbackorder" : productData.is_in_stock ? "instock" : "outofstock";
859
874
  const lowStockRemaining = productData.low_stock_remaining;
860
- const baseImageUrl = getImageUrl(productData.images, imageSize, fallbackImageUrl);
861
875
  const baseImageUrls = getAllImageUrls(productData.images, imageSize);
862
876
  return {
863
877
  id: productData.id,
@@ -867,7 +881,7 @@ function useProductView(options) {
867
881
  description: productData.description,
868
882
  shortDescription: productData.short_description,
869
883
  sku: productData.sku,
870
- imageUrl: processImageUrl(baseImageUrl),
884
+ imageUrl: getProcessedImageUrl(productData.images),
871
885
  imageUrls: baseImageUrls.map((url) => processImageUrl(url) ?? url),
872
886
  price: formatStoreApiPriceIntl(productData.prices?.price, currencyCode, localeTag),
873
887
  regularPrice: formatStoreApiPriceIntl(
@@ -897,7 +911,7 @@ function useProductView(options) {
897
911
  minQuantity: productData.add_to_cart?.minimum ?? 1,
898
912
  raw: productData
899
913
  };
900
- }, [productData, localeTag, fallbackImageUrl, imageSize, lowStockThreshold, processImageUrl]);
914
+ }, [productData, localeTag, imageSize, lowStockThreshold, getProcessedImageUrl, processImageUrl]);
901
915
  const cartItem = react.useMemo(() => {
902
916
  return cartData?.items.find((item) => item.id === productId);
903
917
  }, [cartData?.items, productId]);
@@ -980,24 +994,13 @@ function useCartView(options = {}) {
980
994
  const storeContext = react.useContext(WooCommerceStoreContext);
981
995
  const storeConfig = storeContext?.config;
982
996
  const localeTag = options.localeTag ?? storeConfig?.locale?.localeTag ?? DEFAULT_STORE_CONFIG.locale.localeTag;
983
- const fallbackImageUrl = options.fallbackImageUrl ?? storeConfig?.fallbackImageUrl;
984
- const imageUrlTransformer = storeConfig?.imageUrlTransformer;
985
997
  const { data: cartData, isLoading, error, refetch } = useCart();
986
998
  const updateItemMutation = useUpdateCartItem();
987
999
  const removeItemMutation = useRemoveFromCart();
988
1000
  const applyCouponMutation = useApplyCoupon();
989
1001
  const removeCouponMutation = useRemoveCoupon();
990
1002
  const selectShippingMutation = useSelectShippingRate();
991
- const getProcessedImageUrl = react.useCallback(
992
- (images) => {
993
- const baseUrl = getImageUrl(images, imageSize, fallbackImageUrl);
994
- if (baseUrl && imageUrlTransformer) {
995
- return imageUrlTransformer(baseUrl, imageSize);
996
- }
997
- return baseUrl;
998
- },
999
- [imageSize, fallbackImageUrl, imageUrlTransformer]
1000
- );
1003
+ const getProcessedImageUrl = useProcessedImageUrl(imageSize, options.fallbackImageUrl);
1001
1004
  const items = react.useMemo(() => {
1002
1005
  if (!cartData?.items) return [];
1003
1006
  const currencyCode = cartData.totals.currency_code;
@@ -1143,8 +1146,6 @@ function useReorderView(items, options = {}) {
1143
1146
  const storeContext = react.useContext(WooCommerceStoreContext);
1144
1147
  const storeConfig = storeContext?.config;
1145
1148
  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
1149
  const productIds = react.useMemo(() => [...new Set(items.map((item) => item.productId))], [items]);
1149
1150
  const {
1150
1151
  data: productsPage,
@@ -1155,16 +1156,7 @@ function useReorderView(items, options = {}) {
1155
1156
  { enabled: productIds.length > 0 }
1156
1157
  );
1157
1158
  const { data: cart } = useCart();
1158
- const getProcessedImageUrl = react.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
- );
1159
+ const getProcessedImageUrl = useProcessedImageUrl(imageSize, options.fallbackImageUrl);
1168
1160
  const views = react.useMemo(() => {
1169
1161
  const products = new Map((productsPage?.data ?? []).map((product) => [product.id, product]));
1170
1162
  return items.map((item) => {
@@ -1246,6 +1238,8 @@ function useOrderView(input, options = {}) {
1246
1238
  const storeContext = react.useContext(WooCommerceStoreContext);
1247
1239
  const storeConfig = storeContext?.config;
1248
1240
  const localeTag = options.localeTag ?? storeConfig?.locale?.localeTag ?? DEFAULT_STORE_CONFIG.locale.localeTag;
1241
+ const { imageSize = "thumbnail" } = options;
1242
+ const getProcessedImageUrl = useProcessedImageUrl(imageSize, options.fallbackImageUrl);
1249
1243
  const { data: rawOrder, isLoading, error } = useOrder(input);
1250
1244
  const order = react.useMemo(() => {
1251
1245
  if (!rawOrder) return void 0;
@@ -1256,7 +1250,7 @@ function useOrderView(input, options = {}) {
1256
1250
  quantity: item.quantity,
1257
1251
  price: formatStoreApiPriceIntl(item.prices.price, currencyCode, localeTag),
1258
1252
  total: formatStoreApiPriceIntl(item.totals.line_total, currencyCode, localeTag),
1259
- imageUrl: item.images?.[0]?.src
1253
+ imageUrl: getProcessedImageUrl(item.images)
1260
1254
  }));
1261
1255
  const status = rawOrder.status;
1262
1256
  return {
@@ -1271,7 +1265,7 @@ function useOrderView(input, options = {}) {
1271
1265
  paymentMethod: rawOrder.payment_method ?? "",
1272
1266
  raw: rawOrder
1273
1267
  };
1274
- }, [rawOrder, localeTag]);
1268
+ }, [rawOrder, localeTag, getProcessedImageUrl]);
1275
1269
  return {
1276
1270
  order,
1277
1271
  isLoading,
@@ -1294,6 +1288,7 @@ exports.useProcessCheckout = useProcessCheckout;
1294
1288
  exports.useProduct = useProduct;
1295
1289
  exports.useProductCategories = useProductCategories;
1296
1290
  exports.useProductCategoryTree = useProductCategoryTree;
1291
+ exports.useProductTags = useProductTags;
1297
1292
  exports.useProductView = useProductView;
1298
1293
  exports.useProducts = useProducts;
1299
1294
  exports.useProductsFeed = useProductsFeed;