@plasmicpkgs/commerce 0.0.7 → 0.0.10

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.
@@ -938,7 +938,39 @@ var runtime_1 = /*#__PURE__*/createCommonjsModule(function (module) {
938
938
  }
939
939
  });
940
940
 
941
+ var defaultProduct = {
942
+ id: "123456789",
943
+ name: "Product Placeholder",
944
+ description: "",
945
+ 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 ",
946
+ images: [{
947
+ url: "https://static1.plasmic.app/commerce/lightweight-jacket-0.png",
948
+ alt: "Lightweight Jacket"
949
+ }, {
950
+ url: "https://static1.plasmic.app/commerce/lightweight-jacket-1.png",
951
+ alt: "Lightweight Jacket"
952
+ }, {
953
+ url: "https://static1.plasmic.app/commerce/lightweight-jacket-2.png",
954
+ alt: "Lightweight Jacket"
955
+ }],
956
+ variants: [{
957
+ id: "variiant1",
958
+ name: "Variant Placeholder",
959
+ options: []
960
+ }, {
961
+ id: "variant2",
962
+ name: "Variant Placeholder 2",
963
+ options: []
964
+ }],
965
+ price: {
966
+ value: 0,
967
+ currencyCode: "USD"
968
+ },
969
+ options: []
970
+ };
971
+
941
972
  var ProductContext = /*#__PURE__*/React__default.createContext(undefined);
973
+ var CategoryContext = /*#__PURE__*/React__default.createContext(undefined);
942
974
  function ProductProvider(_ref) {
943
975
  var product = _ref.product,
944
976
  children = _ref.children;
@@ -948,9 +980,21 @@ function ProductProvider(_ref) {
948
980
  key: product.id
949
981
  }, React__default.createElement(reactHookForm.FormProvider, Object.assign({}, methods), children));
950
982
  }
951
- function useProduct() {
952
- return React.useContext(ProductContext);
983
+ var useProduct = function useProduct() {
984
+ var product = React.useContext(ProductContext);
985
+ return product != null ? product : defaultProduct;
986
+ };
987
+ function CategoryProvider(_ref2) {
988
+ var category = _ref2.category,
989
+ children = _ref2.children;
990
+ return React__default.createElement(CategoryContext.Provider, {
991
+ value: category,
992
+ key: category.id
993
+ }, children);
953
994
  }
995
+ var useCategoryContext = function useCategoryContext() {
996
+ return React.useContext(CategoryContext);
997
+ };
954
998
 
955
999
  var _excluded = ["children"];
956
1000
  var Commerce = /*#__PURE__*/React.createContext({});
@@ -1234,12 +1278,13 @@ function AddToCartButton(props) {
1234
1278
  var _ref = _asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee() {
1235
1279
  var _form$getValues$Produ;
1236
1280
 
1237
- var quantity, variantId;
1281
+ var quantity, _form$getValues$Produ2, variantId;
1282
+
1238
1283
  return runtime_1.wrap(function _callee$(_context) {
1239
1284
  while (1) {
1240
1285
  switch (_context.prev = _context.next) {
1241
1286
  case 0:
1242
- quantity = (_form$getValues$Produ = +form.getValues()["ProductQuantity"]) != null ? _form$getValues$Produ : 1;
1287
+ quantity = +((_form$getValues$Produ = form.getValues()["ProductQuantity"]) != null ? _form$getValues$Produ : 1);
1243
1288
 
1244
1289
  if (!(isNaN(quantity) || quantity < 1)) {
1245
1290
  _context.next = 3;
@@ -1256,7 +1301,7 @@ function AddToCartButton(props) {
1256
1301
  break;
1257
1302
  }
1258
1303
 
1259
- variantId = form.getValues()["ProductVariant"];
1304
+ variantId = (_form$getValues$Produ2 = form.getValues()["ProductVariant"]) != null ? _form$getValues$Produ2 : product.variants[0].id;
1260
1305
  _context.next = 7;
1261
1306
  return addItem({
1262
1307
  productId: product.id,
@@ -1435,6 +1480,13 @@ var useBrands = function useBrands(input) {
1435
1480
  }, hook))(input);
1436
1481
  };
1437
1482
 
1483
+ var useCommerceExtraFeatures = function useCommerceExtraFeatures() {
1484
+ var _useCommerce = useCommerce(),
1485
+ providerRef = _useCommerce.providerRef;
1486
+
1487
+ return providerRef.current.extraFeatures;
1488
+ };
1489
+
1438
1490
  var productCollectionMeta = {
1439
1491
  name: "plasmic-commerce-product-collection",
1440
1492
  displayName: "Product Collection",
@@ -1459,6 +1511,20 @@ var productCollectionMeta = {
1459
1511
  }
1460
1512
  }]
1461
1513
  },
1514
+ emptyMessage: {
1515
+ type: "slot",
1516
+ defaultValue: {
1517
+ type: "text",
1518
+ value: "No product found!"
1519
+ }
1520
+ },
1521
+ loadingMessage: {
1522
+ type: "slot",
1523
+ defaultValue: {
1524
+ type: "text",
1525
+ value: "Loading..."
1526
+ }
1527
+ },
1462
1528
  count: "number",
1463
1529
  category: {
1464
1530
  type: "choice",
@@ -1471,6 +1537,17 @@ var productCollectionMeta = {
1471
1537
  value: category.id
1472
1538
  };
1473
1539
  })) != null ? _ctx$categories$map : [];
1540
+ },
1541
+ hidden: function hidden(props, ctx) {
1542
+ return !!(ctx != null && ctx.hasCategoryCtx);
1543
+ }
1544
+ },
1545
+ includeSubCategories: {
1546
+ type: "boolean",
1547
+ hidden: function hidden(props, ctx) {
1548
+ var _ctx$features;
1549
+
1550
+ return !(ctx != null && (_ctx$features = ctx.features) != null && _ctx$features.includeSubCategories);
1474
1551
  }
1475
1552
  },
1476
1553
  brand: {
@@ -1500,31 +1577,47 @@ var productCollectionMeta = {
1500
1577
  importName: "ProductCollection"
1501
1578
  };
1502
1579
  function ProductCollection(props) {
1580
+ var _categoryCtx$id;
1581
+
1503
1582
  var className = props.className,
1504
1583
  children = props.children,
1505
1584
  count = props.count,
1506
1585
  category = props.category,
1586
+ includeSubCategories = props.includeSubCategories,
1507
1587
  brand = props.brand,
1508
1588
  noLayout = props.noLayout,
1509
- setControlContextData = props.setControlContextData;
1589
+ setControlContextData = props.setControlContextData,
1590
+ emptyMessage = props.emptyMessage,
1591
+ loadingMessage = props.loadingMessage;
1592
+
1593
+ var _useCategories = useCategories(),
1594
+ categories = _useCategories.data,
1595
+ isCategoriesLoading = _useCategories.isLoading;
1596
+
1597
+ var _useBrands = useBrands(),
1598
+ brands = _useBrands.data,
1599
+ isBrandsLoading = _useBrands.isLoading;
1600
+
1601
+ var categoryCtx = useCategoryContext();
1510
1602
 
1511
1603
  var _useSearch = useSearch({
1512
- categoryId: category,
1604
+ categoryId: (_categoryCtx$id = categoryCtx == null ? void 0 : categoryCtx.id) != null ? _categoryCtx$id : category,
1513
1605
  brandId: brand,
1514
- count: count
1606
+ count: count,
1607
+ categories: categories != null ? categories : [],
1608
+ includeSubCategories: includeSubCategories
1515
1609
  }),
1516
- data = _useSearch.data;
1610
+ data = _useSearch.data,
1611
+ isSearchLoading = _useSearch.isLoading;
1517
1612
 
1518
- var _useCategories = useCategories(),
1519
- categories = _useCategories.data;
1520
-
1521
- var _useBrands = useBrands(),
1522
- brands = _useBrands.data;
1613
+ var features = useCommerceExtraFeatures();
1523
1614
 
1524
1615
  if (categories && brands) {
1525
1616
  setControlContextData == null ? void 0 : setControlContextData({
1526
1617
  categories: categories,
1527
- brands: brands
1618
+ brands: brands,
1619
+ features: features,
1620
+ hasCategoryCtx: !!categoryCtx
1528
1621
  });
1529
1622
  }
1530
1623
 
@@ -1534,6 +1627,15 @@ function ProductCollection(props) {
1534
1627
  key: product.id
1535
1628
  }, host.repeatedElement(i === 0, children));
1536
1629
  });
1630
+
1631
+ if ([isSearchLoading, isBrandsLoading, isCategoriesLoading].includes(true)) {
1632
+ return React__default.isValidElement(loadingMessage) ? loadingMessage : null;
1633
+ }
1634
+
1635
+ if (!data || data.products.length === 0) {
1636
+ return React__default.isValidElement(emptyMessage) ? emptyMessage : null;
1637
+ }
1638
+
1537
1639
  return noLayout ? React__default.createElement(React__default.Fragment, null, renderedData) : React__default.createElement("div", {
1538
1640
  className: className
1539
1641
  }, renderedData);
@@ -1725,15 +1827,12 @@ var productPriceMeta = {
1725
1827
  importName: "ProductPrice"
1726
1828
  };
1727
1829
  function ProductPriceComponent(props) {
1830
+ var _product$variants$0$i;
1831
+
1728
1832
  var className = props.className;
1729
1833
  var product = useProduct();
1730
1834
  var form = reactHookForm.useFormContext();
1731
-
1732
- if (!product) {
1733
- throw new Error("This component must be within a product context.");
1734
- }
1735
-
1736
- var watchProductVariant = form.watch("ProductVariant", product == null ? void 0 : product.variants[0].id);
1835
+ var watchProductVariant = form == null ? void 0 : form.watch("ProductVariant", (_product$variants$0$i = product.variants[0].id) != null ? _product$variants$0$i : "");
1737
1836
 
1738
1837
  var _usePrice = usePrice({
1739
1838
  amount: product ? getProductPrice(product, watchProductVariant) : 0,
@@ -1860,9 +1959,11 @@ var productVariantPickerMeta = {
1860
1959
  importName: "ProductVariantPicker"
1861
1960
  };
1862
1961
  function ProductVariantPicker(props) {
1962
+ var _useFormContext;
1963
+
1863
1964
  var className = props.className;
1864
1965
  var product = useProduct();
1865
- var form = reactHookForm.useFormContext();
1966
+ var form = (_useFormContext = reactHookForm.useFormContext()) != null ? _useFormContext : reactHookForm.useForm();
1866
1967
  return React__default.createElement(reactHookForm.Controller, {
1867
1968
  name: "ProductVariant",
1868
1969
  control: form == null ? void 0 : form.control,
@@ -1877,7 +1978,7 @@ function ProductVariantPicker(props) {
1877
1978
  return React__default.createElement("option", {
1878
1979
  value: variant.id
1879
1980
  }, variant.name);
1880
- })) != null ? _product$variants$map : React__default.createElement("option", null, "Fake Product Variant"));
1981
+ })) != null ? _product$variants$map : React__default.createElement("option", null, "Product Variant Placeholder"));
1881
1982
  }
1882
1983
  });
1883
1984
  }
@@ -1963,6 +2064,9 @@ var cartMeta = {
1963
2064
  field: {
1964
2065
  type: "choice",
1965
2066
  options: ["Size", "Total Price"]
2067
+ },
2068
+ hideIfIsEmpty: {
2069
+ type: "boolean"
1966
2070
  }
1967
2071
  },
1968
2072
  importPath: "@plasmicpkgs/commerce",
@@ -1972,7 +2076,8 @@ function CartComponent(props) {
1972
2076
  var _data$totalPrice, _data$currency$code;
1973
2077
 
1974
2078
  var className = props.className,
1975
- field = props.field;
2079
+ field = props.field,
2080
+ hideIfIsEmpty = props.hideIfIsEmpty;
1976
2081
 
1977
2082
  var _useCart = useCart(),
1978
2083
  data = _useCart.data;
@@ -1992,12 +2097,12 @@ function CartComponent(props) {
1992
2097
  if (field === "Size") {
1993
2098
  var _data$lineItems$lengt;
1994
2099
 
1995
- value = "" + ((_data$lineItems$lengt = data == null ? void 0 : data.lineItems.length) != null ? _data$lineItems$lengt : 0);
2100
+ value = (_data$lineItems$lengt = data == null ? void 0 : data.lineItems.length) != null ? _data$lineItems$lengt : 0;
1996
2101
  } else if (field === "Total Price") {
1997
- value = "" + price;
2102
+ value = price != null ? price : 0;
1998
2103
  }
1999
2104
 
2000
- return React__default.createElement("span", {
2105
+ return hideIfIsEmpty && value === 0 ? null : React__default.createElement("span", {
2001
2106
  className: className
2002
2107
  }, value);
2003
2108
  }
@@ -2009,9 +2114,163 @@ function registerCart(loader, customCartMeta) {
2009
2114
  doRegisterComponent(CartComponent, customCartMeta != null ? customCartMeta : cartMeta);
2010
2115
  }
2011
2116
 
2012
- var x = function x() {
2013
- return 2;
2117
+ var categoryCollectionMeta = {
2118
+ name: "plasmic-commerce-category-collection",
2119
+ displayName: "Category Collection",
2120
+ props: {
2121
+ children: {
2122
+ type: "slot",
2123
+ defaultValue: [{
2124
+ type: "vbox",
2125
+ children: [{
2126
+ type: "component",
2127
+ name: "plasmic-commerce-category-field",
2128
+ props: {
2129
+ field: "name"
2130
+ }
2131
+ }, {
2132
+ type: "component",
2133
+ name: "plasmic-commerce-product-collection"
2134
+ }],
2135
+ styles: {
2136
+ width: "100%",
2137
+ minWidth: 0
2138
+ }
2139
+ }]
2140
+ },
2141
+ noLayout: {
2142
+ type: "boolean"
2143
+ },
2144
+ emptyMessage: {
2145
+ type: "slot",
2146
+ defaultValue: {
2147
+ type: "text",
2148
+ value: "No collection found!"
2149
+ }
2150
+ },
2151
+ loadingMessage: {
2152
+ type: "slot",
2153
+ defaultValue: {
2154
+ type: "text",
2155
+ value: "Loading..."
2156
+ }
2157
+ },
2158
+ category: {
2159
+ type: "choice",
2160
+ options: function options(props, ctx) {
2161
+ var _ctx$categories$map;
2162
+
2163
+ return (_ctx$categories$map = ctx == null ? void 0 : ctx.categories.map(function (category) {
2164
+ return {
2165
+ label: category.name,
2166
+ value: category.id
2167
+ };
2168
+ })) != null ? _ctx$categories$map : [];
2169
+ }
2170
+ }
2171
+ },
2172
+ defaultStyles: {
2173
+ display: "grid",
2174
+ gridTemplateColumns: "1fr",
2175
+ gridRowGap: "8px",
2176
+ padding: "8px",
2177
+ maxWidth: "100%"
2178
+ },
2179
+ importPath: "@plasmicpkgs/commerce",
2180
+ importName: "CategoryCollection"
2181
+ };
2182
+ function CategoryCollection(props) {
2183
+ var children = props.children,
2184
+ noLayout = props.noLayout,
2185
+ className = props.className,
2186
+ loadingMessage = props.loadingMessage,
2187
+ emptyMessage = props.emptyMessage,
2188
+ selectedCategory = props.category,
2189
+ setControlContextData = props.setControlContextData;
2190
+ var features = useCommerceExtraFeatures();
2191
+ var inEditor = host.usePlasmicCanvasContext();
2192
+
2193
+ var _useCategories = useCategories(),
2194
+ allCategories = _useCategories.data,
2195
+ isAllCategoriesLoading = _useCategories.isLoading;
2196
+
2197
+ var _useCategories2 = useCategories({
2198
+ categoryId: selectedCategory,
2199
+ topologicalSort: !selectedCategory && (features == null ? void 0 : features.includeSubCategories),
2200
+ addIsEmptyField: !!inEditor
2201
+ }),
2202
+ categories = _useCategories2.data,
2203
+ isLoading = _useCategories2.isLoading;
2204
+
2205
+ if (allCategories) {
2206
+ setControlContextData == null ? void 0 : setControlContextData({
2207
+ categories: allCategories
2208
+ });
2209
+ }
2210
+
2211
+ var firstCategoryNotEmpty = categories == null ? void 0 : categories.find(function (category) {
2212
+ return !category.isEmpty;
2213
+ });
2214
+ var renderedData = categories == null ? void 0 : categories.map(function (category, i) {
2215
+ return React__default.createElement(CategoryProvider, {
2216
+ category: category,
2217
+ key: category.id
2218
+ }, host.repeatedElement(firstCategoryNotEmpty ? category === firstCategoryNotEmpty : i === 0, children));
2219
+ });
2220
+
2221
+ if ([isAllCategoriesLoading, isLoading].includes(true)) {
2222
+ return React__default.isValidElement(loadingMessage) ? loadingMessage : null;
2223
+ }
2224
+
2225
+ if (!categories || categories.length === 0) {
2226
+ return React__default.isValidElement(emptyMessage) ? emptyMessage : null;
2227
+ }
2228
+
2229
+ return noLayout ? React__default.createElement(React__default.Fragment, null, renderedData) : React__default.createElement("div", {
2230
+ className: className
2231
+ }, renderedData);
2232
+ }
2233
+ function registerCategoryCollection(loader, customCategoryCollectionMeta) {
2234
+ var doRegisterComponent = function doRegisterComponent() {
2235
+ return loader ? loader.registerComponent.apply(loader, arguments) : registerComponent.apply(void 0, arguments);
2236
+ };
2237
+
2238
+ doRegisterComponent(CategoryCollection, customCategoryCollectionMeta != null ? customCategoryCollectionMeta : categoryCollectionMeta);
2239
+ }
2240
+
2241
+ var categoryFieldMeta = {
2242
+ name: "plasmic-commerce-category-field",
2243
+ displayName: "Category Field",
2244
+ props: {
2245
+ field: {
2246
+ type: "choice",
2247
+ options: ["id", "name", "slug", "path"]
2248
+ }
2249
+ },
2250
+ importPath: "@plasmicpkgs/commerce",
2251
+ importName: "CategoryField"
2014
2252
  };
2253
+ function CategoryField(props) {
2254
+ var className = props.className,
2255
+ field = props.field;
2256
+ var category = useCategoryContext();
2257
+
2258
+ if (!field) {
2259
+ return React__default.createElement("span", null, "You must set the field prop");
2260
+ }
2261
+
2262
+ var data = category ? category[field] : "Category field placeholder";
2263
+ return React__default.createElement("span", {
2264
+ className: className
2265
+ }, data);
2266
+ }
2267
+ function registerCategoryField(loader, customCategoryFieldMeta) {
2268
+ var doRegisterComponent = function doRegisterComponent() {
2269
+ return loader ? loader.registerComponent.apply(loader, arguments) : registerComponent.apply(void 0, arguments);
2270
+ };
2271
+
2272
+ doRegisterComponent(CategoryField, customCategoryFieldMeta != null ? customCategoryFieldMeta : categoryFieldMeta);
2273
+ }
2015
2274
 
2016
2275
 
2017
2276
 
@@ -2072,11 +2331,15 @@ function registerAll(loader) {
2072
2331
  registerProductVariantPicker(loader);
2073
2332
  registerProductBox(loader);
2074
2333
  registerProductLink(loader);
2334
+ registerCategoryCollection(loader);
2335
+ registerCategoryField(loader);
2075
2336
  }
2076
2337
 
2077
2338
  exports.AddToCartButton = AddToCartButton;
2078
2339
  exports.CartComponent = CartComponent;
2079
2340
  exports.CartType = cart;
2341
+ exports.CategoryCollection = CategoryCollection;
2342
+ exports.CategoryField = CategoryField;
2080
2343
  exports.CommerceError = CommerceError;
2081
2344
  exports.CoreCommerceProvider = CoreCommerceProvider;
2082
2345
  exports.FetcherError = FetcherError;
@@ -2093,6 +2356,8 @@ exports.SiteTypes = site;
2093
2356
  exports.ValidationError = ValidationError;
2094
2357
  exports.addToCartButtonMeta = addToCartButtonMeta;
2095
2358
  exports.cartMeta = cartMeta;
2359
+ exports.categoryCollectionMeta = categoryCollectionMeta;
2360
+ exports.categoryFieldMeta = categoryFieldMeta;
2096
2361
  exports.fetcher = fetcher$2;
2097
2362
  exports.getCommerceProvider = getCommerceProvider;
2098
2363
  exports.productBoxMeta = productBoxMeta;
@@ -2106,6 +2371,8 @@ exports.productVariantPickerMeta = productVariantPickerMeta;
2106
2371
  exports.registerAddToCartButton = registerAddToCartButton;
2107
2372
  exports.registerAll = registerAll;
2108
2373
  exports.registerCart = registerCart;
2374
+ exports.registerCategoryCollection = registerCategoryCollection;
2375
+ exports.registerCategoryField = registerCategoryField;
2109
2376
  exports.registerProductBox = registerProductBox;
2110
2377
  exports.registerProductCollection = registerProductCollection;
2111
2378
  exports.registerProductLink = registerProductLink;
@@ -2123,5 +2390,4 @@ exports.useProduct = useProduct$1;
2123
2390
  exports.useRemoveItem = useRemoveItem;
2124
2391
  exports.useSearch = useSearch;
2125
2392
  exports.useUpdateItem = useUpdateItem;
2126
- exports.x = x;
2127
2393
  //# sourceMappingURL=commerce.cjs.development.js.map