@plasmicpkgs/commerce 0.0.8 → 0.0.9

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.
@@ -2,7 +2,7 @@ import registerComponent from '@plasmicapp/host/registerComponent';
2
2
  import React, { useContext, useRef, useMemo, createContext, useCallback } from 'react';
3
3
  import { useForm, FormProvider, useFormContext, Controller } from 'react-hook-form';
4
4
  import useSWR from 'swr';
5
- import { repeatedElement } from '@plasmicapp/host';
5
+ import { repeatedElement, usePlasmicCanvasContext } from '@plasmicapp/host';
6
6
  import Cookies from 'js-cookie';
7
7
 
8
8
  function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
@@ -931,7 +931,39 @@ var runtime_1 = /*#__PURE__*/createCommonjsModule(function (module) {
931
931
  }
932
932
  });
933
933
 
934
+ var defaultProduct = {
935
+ id: "123456789",
936
+ name: "Product Placeholder",
937
+ description: "",
938
+ descriptionHtml: "\n <p>This is a <strong>placeholder</strong>.</p>\n <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>\n <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>\n ",
939
+ images: [{
940
+ url: "https://static1.plasmic.app/commerce/lightweight-jacket-0.png",
941
+ alt: "Lightweight Jacket"
942
+ }, {
943
+ url: "https://static1.plasmic.app/commerce/lightweight-jacket-1.png",
944
+ alt: "Lightweight Jacket"
945
+ }, {
946
+ url: "https://static1.plasmic.app/commerce/lightweight-jacket-2.png",
947
+ alt: "Lightweight Jacket"
948
+ }],
949
+ variants: [{
950
+ id: "variiant1",
951
+ name: "Variant Placeholder",
952
+ options: []
953
+ }, {
954
+ id: "variant2",
955
+ name: "Variant Placeholder 2",
956
+ options: []
957
+ }],
958
+ price: {
959
+ value: 0,
960
+ currencyCode: "USD"
961
+ },
962
+ options: []
963
+ };
964
+
934
965
  var ProductContext = /*#__PURE__*/React.createContext(undefined);
966
+ var CategoryContext = /*#__PURE__*/React.createContext(undefined);
935
967
  function ProductProvider(_ref) {
936
968
  var product = _ref.product,
937
969
  children = _ref.children;
@@ -941,9 +973,21 @@ function ProductProvider(_ref) {
941
973
  key: product.id
942
974
  }, React.createElement(FormProvider, Object.assign({}, methods), children));
943
975
  }
944
- function useProduct() {
945
- return useContext(ProductContext);
976
+ var useProduct = function useProduct() {
977
+ var product = useContext(ProductContext);
978
+ return product != null ? product : defaultProduct;
979
+ };
980
+ function CategoryProvider(_ref2) {
981
+ var category = _ref2.category,
982
+ children = _ref2.children;
983
+ return React.createElement(CategoryContext.Provider, {
984
+ value: category,
985
+ key: category.id
986
+ }, children);
946
987
  }
988
+ var useCategoryContext = function useCategoryContext() {
989
+ return useContext(CategoryContext);
990
+ };
947
991
 
948
992
  var _excluded = ["children"];
949
993
  var Commerce = /*#__PURE__*/createContext({});
@@ -1460,6 +1504,20 @@ var productCollectionMeta = {
1460
1504
  }
1461
1505
  }]
1462
1506
  },
1507
+ emptyMessage: {
1508
+ type: "slot",
1509
+ defaultValue: {
1510
+ type: "text",
1511
+ value: "No product found!"
1512
+ }
1513
+ },
1514
+ loadingMessage: {
1515
+ type: "slot",
1516
+ defaultValue: {
1517
+ type: "text",
1518
+ value: "Loading..."
1519
+ }
1520
+ },
1463
1521
  count: "number",
1464
1522
  category: {
1465
1523
  type: "choice",
@@ -1472,6 +1530,9 @@ var productCollectionMeta = {
1472
1530
  value: category.id
1473
1531
  };
1474
1532
  })) != null ? _ctx$categories$map : [];
1533
+ },
1534
+ hidden: function hidden(props, ctx) {
1535
+ return !!(ctx != null && ctx.hasCategoryCtx);
1475
1536
  }
1476
1537
  },
1477
1538
  includeSubCategories: {
@@ -1509,6 +1570,8 @@ var productCollectionMeta = {
1509
1570
  importName: "ProductCollection"
1510
1571
  };
1511
1572
  function ProductCollection(props) {
1573
+ var _categoryCtx$id;
1574
+
1512
1575
  var className = props.className,
1513
1576
  children = props.children,
1514
1577
  count = props.count,
@@ -1516,22 +1579,29 @@ function ProductCollection(props) {
1516
1579
  includeSubCategories = props.includeSubCategories,
1517
1580
  brand = props.brand,
1518
1581
  noLayout = props.noLayout,
1519
- setControlContextData = props.setControlContextData;
1582
+ setControlContextData = props.setControlContextData,
1583
+ emptyMessage = props.emptyMessage,
1584
+ loadingMessage = props.loadingMessage;
1520
1585
 
1521
1586
  var _useCategories = useCategories(),
1522
- categories = _useCategories.data;
1587
+ categories = _useCategories.data,
1588
+ isCategoriesLoading = _useCategories.isLoading;
1523
1589
 
1524
1590
  var _useBrands = useBrands(),
1525
- brands = _useBrands.data;
1591
+ brands = _useBrands.data,
1592
+ isBrandsLoading = _useBrands.isLoading;
1593
+
1594
+ var categoryCtx = useCategoryContext();
1526
1595
 
1527
1596
  var _useSearch = useSearch({
1528
- categoryId: category,
1597
+ categoryId: (_categoryCtx$id = categoryCtx == null ? void 0 : categoryCtx.id) != null ? _categoryCtx$id : category,
1529
1598
  brandId: brand,
1530
1599
  count: count,
1531
1600
  categories: categories != null ? categories : [],
1532
1601
  includeSubCategories: includeSubCategories
1533
1602
  }),
1534
- data = _useSearch.data;
1603
+ data = _useSearch.data,
1604
+ isSearchLoading = _useSearch.isLoading;
1535
1605
 
1536
1606
  var features = useCommerceExtraFeatures();
1537
1607
 
@@ -1539,7 +1609,8 @@ function ProductCollection(props) {
1539
1609
  setControlContextData == null ? void 0 : setControlContextData({
1540
1610
  categories: categories,
1541
1611
  brands: brands,
1542
- features: features
1612
+ features: features,
1613
+ hasCategoryCtx: !!categoryCtx
1543
1614
  });
1544
1615
  }
1545
1616
 
@@ -1549,6 +1620,15 @@ function ProductCollection(props) {
1549
1620
  key: product.id
1550
1621
  }, repeatedElement(i === 0, children));
1551
1622
  });
1623
+
1624
+ if ([isSearchLoading, isBrandsLoading, isCategoriesLoading].includes(true)) {
1625
+ return React.isValidElement(loadingMessage) ? loadingMessage : null;
1626
+ }
1627
+
1628
+ if (!data || data.products.length === 0) {
1629
+ return React.isValidElement(emptyMessage) ? emptyMessage : null;
1630
+ }
1631
+
1552
1632
  return noLayout ? React.createElement(React.Fragment, null, renderedData) : React.createElement("div", {
1553
1633
  className: className
1554
1634
  }, renderedData);
@@ -1740,15 +1820,12 @@ var productPriceMeta = {
1740
1820
  importName: "ProductPrice"
1741
1821
  };
1742
1822
  function ProductPriceComponent(props) {
1823
+ var _product$variants$0$i;
1824
+
1743
1825
  var className = props.className;
1744
1826
  var product = useProduct();
1745
1827
  var form = useFormContext();
1746
-
1747
- if (!product) {
1748
- throw new Error("This component must be within a product context.");
1749
- }
1750
-
1751
- var watchProductVariant = form.watch("ProductVariant", product == null ? void 0 : product.variants[0].id);
1828
+ var watchProductVariant = form == null ? void 0 : form.watch("ProductVariant", (_product$variants$0$i = product.variants[0].id) != null ? _product$variants$0$i : "");
1752
1829
 
1753
1830
  var _usePrice = usePrice({
1754
1831
  amount: product ? getProductPrice(product, watchProductVariant) : 0,
@@ -1875,9 +1952,11 @@ var productVariantPickerMeta = {
1875
1952
  importName: "ProductVariantPicker"
1876
1953
  };
1877
1954
  function ProductVariantPicker(props) {
1955
+ var _useFormContext;
1956
+
1878
1957
  var className = props.className;
1879
1958
  var product = useProduct();
1880
- var form = useFormContext();
1959
+ var form = (_useFormContext = useFormContext()) != null ? _useFormContext : useForm();
1881
1960
  return React.createElement(Controller, {
1882
1961
  name: "ProductVariant",
1883
1962
  control: form == null ? void 0 : form.control,
@@ -1892,7 +1971,7 @@ function ProductVariantPicker(props) {
1892
1971
  return React.createElement("option", {
1893
1972
  value: variant.id
1894
1973
  }, variant.name);
1895
- })) != null ? _product$variants$map : React.createElement("option", null, "Fake Product Variant"));
1974
+ })) != null ? _product$variants$map : React.createElement("option", null, "Product Variant Placeholder"));
1896
1975
  }
1897
1976
  });
1898
1977
  }
@@ -1978,6 +2057,9 @@ var cartMeta = {
1978
2057
  field: {
1979
2058
  type: "choice",
1980
2059
  options: ["Size", "Total Price"]
2060
+ },
2061
+ hideIfIsEmpty: {
2062
+ type: "boolean"
1981
2063
  }
1982
2064
  },
1983
2065
  importPath: "@plasmicpkgs/commerce",
@@ -1987,7 +2069,8 @@ function CartComponent(props) {
1987
2069
  var _data$totalPrice, _data$currency$code;
1988
2070
 
1989
2071
  var className = props.className,
1990
- field = props.field;
2072
+ field = props.field,
2073
+ hideIfIsEmpty = props.hideIfIsEmpty;
1991
2074
 
1992
2075
  var _useCart = useCart(),
1993
2076
  data = _useCart.data;
@@ -2007,12 +2090,12 @@ function CartComponent(props) {
2007
2090
  if (field === "Size") {
2008
2091
  var _data$lineItems$lengt;
2009
2092
 
2010
- value = "" + ((_data$lineItems$lengt = data == null ? void 0 : data.lineItems.length) != null ? _data$lineItems$lengt : 0);
2093
+ value = (_data$lineItems$lengt = data == null ? void 0 : data.lineItems.length) != null ? _data$lineItems$lengt : 0;
2011
2094
  } else if (field === "Total Price") {
2012
- value = "" + price;
2095
+ value = price != null ? price : 0;
2013
2096
  }
2014
2097
 
2015
- return React.createElement("span", {
2098
+ return hideIfIsEmpty && value === 0 ? null : React.createElement("span", {
2016
2099
  className: className
2017
2100
  }, value);
2018
2101
  }
@@ -2024,6 +2107,164 @@ function registerCart(loader, customCartMeta) {
2024
2107
  doRegisterComponent(CartComponent, customCartMeta != null ? customCartMeta : cartMeta);
2025
2108
  }
2026
2109
 
2110
+ var categoryCollectionMeta = {
2111
+ name: "plasmic-commerce-category-collection",
2112
+ displayName: "Category Collection",
2113
+ props: {
2114
+ children: {
2115
+ type: "slot",
2116
+ defaultValue: [{
2117
+ type: "vbox",
2118
+ children: [{
2119
+ type: "component",
2120
+ name: "plasmic-commerce-category-field",
2121
+ props: {
2122
+ field: "name"
2123
+ }
2124
+ }, {
2125
+ type: "component",
2126
+ name: "plasmic-commerce-product-collection"
2127
+ }],
2128
+ styles: {
2129
+ width: "100%",
2130
+ minWidth: 0
2131
+ }
2132
+ }]
2133
+ },
2134
+ noLayout: {
2135
+ type: "boolean"
2136
+ },
2137
+ emptyMessage: {
2138
+ type: "slot",
2139
+ defaultValue: {
2140
+ type: "text",
2141
+ value: "No collection found!"
2142
+ }
2143
+ },
2144
+ loadingMessage: {
2145
+ type: "slot",
2146
+ defaultValue: {
2147
+ type: "text",
2148
+ value: "Loading..."
2149
+ }
2150
+ },
2151
+ category: {
2152
+ type: "choice",
2153
+ options: function options(props, ctx) {
2154
+ var _ctx$categories$map;
2155
+
2156
+ return (_ctx$categories$map = ctx == null ? void 0 : ctx.categories.map(function (category) {
2157
+ return {
2158
+ label: category.name,
2159
+ value: category.id
2160
+ };
2161
+ })) != null ? _ctx$categories$map : [];
2162
+ }
2163
+ }
2164
+ },
2165
+ defaultStyles: {
2166
+ display: "grid",
2167
+ gridTemplateColumns: "1fr",
2168
+ gridRowGap: "8px",
2169
+ padding: "8px",
2170
+ maxWidth: "100%"
2171
+ },
2172
+ importPath: "@plasmicpkgs/commerce",
2173
+ importName: "CategoryCollection"
2174
+ };
2175
+ function CategoryCollection(props) {
2176
+ var children = props.children,
2177
+ noLayout = props.noLayout,
2178
+ className = props.className,
2179
+ loadingMessage = props.loadingMessage,
2180
+ emptyMessage = props.emptyMessage,
2181
+ selectedCategory = props.category,
2182
+ setControlContextData = props.setControlContextData;
2183
+ var features = useCommerceExtraFeatures();
2184
+ var inEditor = usePlasmicCanvasContext();
2185
+
2186
+ var _useCategories = useCategories(),
2187
+ allCategories = _useCategories.data,
2188
+ isAllCategoriesLoading = _useCategories.isLoading;
2189
+
2190
+ var _useCategories2 = useCategories({
2191
+ categoryId: selectedCategory,
2192
+ topologicalSort: !selectedCategory && (features == null ? void 0 : features.includeSubCategories),
2193
+ addIsEmptyField: !!inEditor
2194
+ }),
2195
+ categories = _useCategories2.data,
2196
+ isLoading = _useCategories2.isLoading;
2197
+
2198
+ if (allCategories) {
2199
+ setControlContextData == null ? void 0 : setControlContextData({
2200
+ categories: allCategories
2201
+ });
2202
+ }
2203
+
2204
+ var firstCategoryNotEmpty = categories == null ? void 0 : categories.find(function (category) {
2205
+ return !category.isEmpty;
2206
+ });
2207
+ var renderedData = categories == null ? void 0 : categories.map(function (category, i) {
2208
+ return React.createElement(CategoryProvider, {
2209
+ category: category,
2210
+ key: category.id
2211
+ }, repeatedElement(firstCategoryNotEmpty ? category === firstCategoryNotEmpty : i === 0, children));
2212
+ });
2213
+
2214
+ if ([isAllCategoriesLoading, isLoading].includes(true)) {
2215
+ return React.isValidElement(loadingMessage) ? loadingMessage : null;
2216
+ }
2217
+
2218
+ if (!categories || categories.length === 0) {
2219
+ return React.isValidElement(emptyMessage) ? emptyMessage : null;
2220
+ }
2221
+
2222
+ return noLayout ? React.createElement(React.Fragment, null, renderedData) : React.createElement("div", {
2223
+ className: className
2224
+ }, renderedData);
2225
+ }
2226
+ function registerCategoryCollection(loader, customCategoryCollectionMeta) {
2227
+ var doRegisterComponent = function doRegisterComponent() {
2228
+ return loader ? loader.registerComponent.apply(loader, arguments) : registerComponent.apply(void 0, arguments);
2229
+ };
2230
+
2231
+ doRegisterComponent(CategoryCollection, customCategoryCollectionMeta != null ? customCategoryCollectionMeta : categoryCollectionMeta);
2232
+ }
2233
+
2234
+ var categoryFieldMeta = {
2235
+ name: "plasmic-commerce-category-field",
2236
+ displayName: "Category Field",
2237
+ props: {
2238
+ field: {
2239
+ type: "choice",
2240
+ options: ["id", "name", "slug", "path"]
2241
+ }
2242
+ },
2243
+ importPath: "@plasmicpkgs/commerce",
2244
+ importName: "CategoryField"
2245
+ };
2246
+ function CategoryField(props) {
2247
+ var className = props.className,
2248
+ field = props.field;
2249
+ var category = useCategoryContext();
2250
+
2251
+ if (!field) {
2252
+ return React.createElement("span", null, "You must set the field prop");
2253
+ }
2254
+
2255
+ var data = category ? category[field] : "Category field placeholder";
2256
+ return React.createElement("span", {
2257
+ className: className
2258
+ }, data);
2259
+ }
2260
+ function registerCategoryField(loader, customCategoryFieldMeta) {
2261
+ var doRegisterComponent = function doRegisterComponent() {
2262
+ return loader ? loader.registerComponent.apply(loader, arguments) : registerComponent.apply(void 0, arguments);
2263
+ };
2264
+
2265
+ doRegisterComponent(CategoryField, customCategoryFieldMeta != null ? customCategoryFieldMeta : categoryFieldMeta);
2266
+ }
2267
+
2027
2268
 
2028
2269
 
2029
2270
  var product = {
@@ -2083,7 +2324,9 @@ function registerAll(loader) {
2083
2324
  registerProductVariantPicker(loader);
2084
2325
  registerProductBox(loader);
2085
2326
  registerProductLink(loader);
2327
+ registerCategoryCollection(loader);
2328
+ registerCategoryField(loader);
2086
2329
  }
2087
2330
 
2088
- export { AddToCartButton, CartComponent, cart as CartType, CommerceError, CoreCommerceProvider, FetcherError, ProductBox, ProductCollection, ProductLink, ProductMedia, ProductPriceComponent, ProductQuantity, ProductTextField, product as ProductTypes, ProductVariantPicker, site as SiteTypes, ValidationError, addToCartButtonMeta, cartMeta, fetcher$2 as fetcher, getCommerceProvider, productBoxMeta, productCollectionMeta, productLinkMeta, productMediaMeta, productPriceMeta, productQuantityMeta, productTextFieldMeta, productVariantPickerMeta, registerAddToCartButton, registerAll, registerCart, registerProductBox, registerProductCollection, registerProductLink, registerProductMedia, registerProductPrice, registerProductQuantity, registerProductVariantPicker, registerTextField, useAddItem, useBrands, useCart, useCategories, useCommerce, useProduct$1 as useProduct, useRemoveItem, useSearch, useUpdateItem };
2331
+ export { AddToCartButton, CartComponent, cart as CartType, CategoryCollection, CategoryField, CommerceError, CoreCommerceProvider, FetcherError, ProductBox, ProductCollection, ProductLink, ProductMedia, ProductPriceComponent, ProductQuantity, ProductTextField, product as ProductTypes, ProductVariantPicker, site as SiteTypes, ValidationError, addToCartButtonMeta, cartMeta, categoryCollectionMeta, categoryFieldMeta, fetcher$2 as fetcher, getCommerceProvider, productBoxMeta, productCollectionMeta, productLinkMeta, productMediaMeta, productPriceMeta, productQuantityMeta, productTextFieldMeta, productVariantPickerMeta, registerAddToCartButton, registerAll, registerCart, registerCategoryCollection, registerCategoryField, registerProductBox, registerProductCollection, registerProductLink, registerProductMedia, registerProductPrice, registerProductQuantity, registerProductVariantPicker, registerTextField, useAddItem, useBrands, useCart, useCategories, useCommerce, useProduct$1 as useProduct, useRemoveItem, useSearch, useUpdateItem };
2089
2332
  //# sourceMappingURL=commerce.esm.js.map