@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.
@@ -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({});
@@ -1227,12 +1271,13 @@ function AddToCartButton(props) {
1227
1271
  var _ref = _asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee() {
1228
1272
  var _form$getValues$Produ;
1229
1273
 
1230
- var quantity, variantId;
1274
+ var quantity, _form$getValues$Produ2, variantId;
1275
+
1231
1276
  return runtime_1.wrap(function _callee$(_context) {
1232
1277
  while (1) {
1233
1278
  switch (_context.prev = _context.next) {
1234
1279
  case 0:
1235
- quantity = (_form$getValues$Produ = +form.getValues()["ProductQuantity"]) != null ? _form$getValues$Produ : 1;
1280
+ quantity = +((_form$getValues$Produ = form.getValues()["ProductQuantity"]) != null ? _form$getValues$Produ : 1);
1236
1281
 
1237
1282
  if (!(isNaN(quantity) || quantity < 1)) {
1238
1283
  _context.next = 3;
@@ -1249,7 +1294,7 @@ function AddToCartButton(props) {
1249
1294
  break;
1250
1295
  }
1251
1296
 
1252
- variantId = form.getValues()["ProductVariant"];
1297
+ variantId = (_form$getValues$Produ2 = form.getValues()["ProductVariant"]) != null ? _form$getValues$Produ2 : product.variants[0].id;
1253
1298
  _context.next = 7;
1254
1299
  return addItem({
1255
1300
  productId: product.id,
@@ -1428,6 +1473,13 @@ var useBrands = function useBrands(input) {
1428
1473
  }, hook))(input);
1429
1474
  };
1430
1475
 
1476
+ var useCommerceExtraFeatures = function useCommerceExtraFeatures() {
1477
+ var _useCommerce = useCommerce(),
1478
+ providerRef = _useCommerce.providerRef;
1479
+
1480
+ return providerRef.current.extraFeatures;
1481
+ };
1482
+
1431
1483
  var productCollectionMeta = {
1432
1484
  name: "plasmic-commerce-product-collection",
1433
1485
  displayName: "Product Collection",
@@ -1452,6 +1504,20 @@ var productCollectionMeta = {
1452
1504
  }
1453
1505
  }]
1454
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
+ },
1455
1521
  count: "number",
1456
1522
  category: {
1457
1523
  type: "choice",
@@ -1464,6 +1530,17 @@ var productCollectionMeta = {
1464
1530
  value: category.id
1465
1531
  };
1466
1532
  })) != null ? _ctx$categories$map : [];
1533
+ },
1534
+ hidden: function hidden(props, ctx) {
1535
+ return !!(ctx != null && ctx.hasCategoryCtx);
1536
+ }
1537
+ },
1538
+ includeSubCategories: {
1539
+ type: "boolean",
1540
+ hidden: function hidden(props, ctx) {
1541
+ var _ctx$features;
1542
+
1543
+ return !(ctx != null && (_ctx$features = ctx.features) != null && _ctx$features.includeSubCategories);
1467
1544
  }
1468
1545
  },
1469
1546
  brand: {
@@ -1493,31 +1570,47 @@ var productCollectionMeta = {
1493
1570
  importName: "ProductCollection"
1494
1571
  };
1495
1572
  function ProductCollection(props) {
1573
+ var _categoryCtx$id;
1574
+
1496
1575
  var className = props.className,
1497
1576
  children = props.children,
1498
1577
  count = props.count,
1499
1578
  category = props.category,
1579
+ includeSubCategories = props.includeSubCategories,
1500
1580
  brand = props.brand,
1501
1581
  noLayout = props.noLayout,
1502
- setControlContextData = props.setControlContextData;
1582
+ setControlContextData = props.setControlContextData,
1583
+ emptyMessage = props.emptyMessage,
1584
+ loadingMessage = props.loadingMessage;
1585
+
1586
+ var _useCategories = useCategories(),
1587
+ categories = _useCategories.data,
1588
+ isCategoriesLoading = _useCategories.isLoading;
1589
+
1590
+ var _useBrands = useBrands(),
1591
+ brands = _useBrands.data,
1592
+ isBrandsLoading = _useBrands.isLoading;
1593
+
1594
+ var categoryCtx = useCategoryContext();
1503
1595
 
1504
1596
  var _useSearch = useSearch({
1505
- categoryId: category,
1597
+ categoryId: (_categoryCtx$id = categoryCtx == null ? void 0 : categoryCtx.id) != null ? _categoryCtx$id : category,
1506
1598
  brandId: brand,
1507
- count: count
1599
+ count: count,
1600
+ categories: categories != null ? categories : [],
1601
+ includeSubCategories: includeSubCategories
1508
1602
  }),
1509
- data = _useSearch.data;
1603
+ data = _useSearch.data,
1604
+ isSearchLoading = _useSearch.isLoading;
1510
1605
 
1511
- var _useCategories = useCategories(),
1512
- categories = _useCategories.data;
1513
-
1514
- var _useBrands = useBrands(),
1515
- brands = _useBrands.data;
1606
+ var features = useCommerceExtraFeatures();
1516
1607
 
1517
1608
  if (categories && brands) {
1518
1609
  setControlContextData == null ? void 0 : setControlContextData({
1519
1610
  categories: categories,
1520
- brands: brands
1611
+ brands: brands,
1612
+ features: features,
1613
+ hasCategoryCtx: !!categoryCtx
1521
1614
  });
1522
1615
  }
1523
1616
 
@@ -1527,6 +1620,15 @@ function ProductCollection(props) {
1527
1620
  key: product.id
1528
1621
  }, repeatedElement(i === 0, children));
1529
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
+
1530
1632
  return noLayout ? React.createElement(React.Fragment, null, renderedData) : React.createElement("div", {
1531
1633
  className: className
1532
1634
  }, renderedData);
@@ -1718,15 +1820,12 @@ var productPriceMeta = {
1718
1820
  importName: "ProductPrice"
1719
1821
  };
1720
1822
  function ProductPriceComponent(props) {
1823
+ var _product$variants$0$i;
1824
+
1721
1825
  var className = props.className;
1722
1826
  var product = useProduct();
1723
1827
  var form = useFormContext();
1724
-
1725
- if (!product) {
1726
- throw new Error("This component must be within a product context.");
1727
- }
1728
-
1729
- 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 : "");
1730
1829
 
1731
1830
  var _usePrice = usePrice({
1732
1831
  amount: product ? getProductPrice(product, watchProductVariant) : 0,
@@ -1853,9 +1952,11 @@ var productVariantPickerMeta = {
1853
1952
  importName: "ProductVariantPicker"
1854
1953
  };
1855
1954
  function ProductVariantPicker(props) {
1955
+ var _useFormContext;
1956
+
1856
1957
  var className = props.className;
1857
1958
  var product = useProduct();
1858
- var form = useFormContext();
1959
+ var form = (_useFormContext = useFormContext()) != null ? _useFormContext : useForm();
1859
1960
  return React.createElement(Controller, {
1860
1961
  name: "ProductVariant",
1861
1962
  control: form == null ? void 0 : form.control,
@@ -1870,7 +1971,7 @@ function ProductVariantPicker(props) {
1870
1971
  return React.createElement("option", {
1871
1972
  value: variant.id
1872
1973
  }, variant.name);
1873
- })) != null ? _product$variants$map : React.createElement("option", null, "Fake Product Variant"));
1974
+ })) != null ? _product$variants$map : React.createElement("option", null, "Product Variant Placeholder"));
1874
1975
  }
1875
1976
  });
1876
1977
  }
@@ -1956,6 +2057,9 @@ var cartMeta = {
1956
2057
  field: {
1957
2058
  type: "choice",
1958
2059
  options: ["Size", "Total Price"]
2060
+ },
2061
+ hideIfIsEmpty: {
2062
+ type: "boolean"
1959
2063
  }
1960
2064
  },
1961
2065
  importPath: "@plasmicpkgs/commerce",
@@ -1965,7 +2069,8 @@ function CartComponent(props) {
1965
2069
  var _data$totalPrice, _data$currency$code;
1966
2070
 
1967
2071
  var className = props.className,
1968
- field = props.field;
2072
+ field = props.field,
2073
+ hideIfIsEmpty = props.hideIfIsEmpty;
1969
2074
 
1970
2075
  var _useCart = useCart(),
1971
2076
  data = _useCart.data;
@@ -1985,12 +2090,12 @@ function CartComponent(props) {
1985
2090
  if (field === "Size") {
1986
2091
  var _data$lineItems$lengt;
1987
2092
 
1988
- 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;
1989
2094
  } else if (field === "Total Price") {
1990
- value = "" + price;
2095
+ value = price != null ? price : 0;
1991
2096
  }
1992
2097
 
1993
- return React.createElement("span", {
2098
+ return hideIfIsEmpty && value === 0 ? null : React.createElement("span", {
1994
2099
  className: className
1995
2100
  }, value);
1996
2101
  }
@@ -2002,9 +2107,163 @@ function registerCart(loader, customCartMeta) {
2002
2107
  doRegisterComponent(CartComponent, customCartMeta != null ? customCartMeta : cartMeta);
2003
2108
  }
2004
2109
 
2005
- var x = function x() {
2006
- return 2;
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"
2007
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
+ }
2008
2267
 
2009
2268
 
2010
2269
 
@@ -2065,7 +2324,9 @@ function registerAll(loader) {
2065
2324
  registerProductVariantPicker(loader);
2066
2325
  registerProductBox(loader);
2067
2326
  registerProductLink(loader);
2327
+ registerCategoryCollection(loader);
2328
+ registerCategoryField(loader);
2068
2329
  }
2069
2330
 
2070
- 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, x };
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 };
2071
2332
  //# sourceMappingURL=commerce.esm.js.map