@plasmicpkgs/commerce-swell 0.0.8 → 0.0.11

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.
@@ -1051,6 +1051,20 @@ function normalizeLineItem(_ref6) {
1051
1051
  return item;
1052
1052
  }
1053
1053
 
1054
+ function normalizeCategory(_ref7) {
1055
+ var id = _ref7.id,
1056
+ name = _ref7.name,
1057
+ slug = _ref7.slug,
1058
+ products = _ref7.products;
1059
+ return {
1060
+ id: id,
1061
+ name: name,
1062
+ slug: slug,
1063
+ path: "/" + slug,
1064
+ isEmpty: (products == null ? void 0 : products.length) === 0
1065
+ };
1066
+ }
1067
+
1054
1068
  var handleFetchResponse = /*#__PURE__*/function () {
1055
1069
  var _ref = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee(res) {
1056
1070
  return runtime_1.wrap(function _callee$(_context) {
@@ -1531,66 +1545,177 @@ var handler$3 = {
1531
1545
  }
1532
1546
  };
1533
1547
 
1548
+ function ensure(x, msg) {
1549
+ if (msg === void 0) {
1550
+ msg = "";
1551
+ }
1552
+
1553
+ if (x === null || x === undefined) {
1554
+ debugger;
1555
+ throw new Error("Value must not be undefined or null" + (msg ? "- " + msg : ""));
1556
+ } else {
1557
+ return x;
1558
+ }
1559
+ }
1560
+
1561
+ var walkCategoryTree = function walkCategoryTree(category, categories) {
1562
+ if (!category || !categories) {
1563
+ return [];
1564
+ }
1565
+
1566
+ var queue = [category];
1567
+ var result = [];
1568
+
1569
+ while (queue.length > 0) {
1570
+ var _curr$children$result, _curr$children;
1571
+
1572
+ var curr = ensure(queue.shift());
1573
+ result.push(curr);
1574
+ queue.push.apply(queue, (_curr$children$result = (_curr$children = curr.children) == null ? void 0 : _curr$children.results.map(function (child) {
1575
+ return ensure(categories.find(function (category) {
1576
+ return category.id === child.id;
1577
+ }));
1578
+ })) != null ? _curr$children$result : []);
1579
+ }
1580
+
1581
+ return result;
1582
+ };
1583
+ var topologicalSortForCategoryTree = function topologicalSortForCategoryTree(categories) {
1584
+ return categories.filter(function (category) {
1585
+ return !category.parent_id;
1586
+ }).flatMap(function (category) {
1587
+ return walkCategoryTree(category, categories);
1588
+ });
1589
+ };
1590
+
1534
1591
  var handler$4 = {
1535
1592
  fetchOptions: {
1536
1593
  query: 'products',
1537
1594
  method: 'list'
1538
1595
  },
1539
1596
  fetcher: function fetcher(_ref) {
1540
- return _asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee() {
1541
- var input, fetch, sortMap, categoryId, brandId, search, _input$sort, sort, count, mappedSort, _yield$fetch, results, found, products;
1597
+ return _asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee3() {
1598
+ var input, options, fetch, sortMap, categoryId, includeSubCategories, categories, brandId, search, _input$sort, sort, count, mappedSort, products, _fetch, includedCategories;
1542
1599
 
1543
- return runtime_1.wrap(function _callee$(_context) {
1600
+ return runtime_1.wrap(function _callee3$(_context3) {
1544
1601
  while (1) {
1545
- switch (_context.prev = _context.next) {
1602
+ switch (_context3.prev = _context3.next) {
1546
1603
  case 0:
1547
- input = _ref.input, fetch = _ref.fetch;
1604
+ input = _ref.input, options = _ref.options, fetch = _ref.fetch;
1548
1605
  sortMap = new Map([['latest-desc', ''], ['price-asc', 'price_asc'], ['price-desc', 'price_desc'], ['trending-desc', 'popularity']]);
1549
- categoryId = input.categoryId, brandId = input.brandId, search = input.search, _input$sort = input.sort, sort = _input$sort === void 0 ? 'latest-desc' : _input$sort, count = input.count;
1606
+ categoryId = input.categoryId, includeSubCategories = input.includeSubCategories, categories = input.categories, brandId = input.brandId, search = input.search, _input$sort = input.sort, sort = _input$sort === void 0 ? 'latest-desc' : _input$sort, count = input.count;
1550
1607
  mappedSort = sortMap.get(sort);
1551
- _context.next = 6;
1552
- return fetch({
1553
- query: 'products',
1554
- method: 'list',
1555
- variables: {
1556
- category: categoryId,
1557
- brand: brandId,
1558
- search: search,
1559
- sort: mappedSort,
1560
- expand: ["variants"],
1561
- limit: count
1562
- }
1563
- });
1608
+ products = [];
1609
+
1610
+ _fetch = /*#__PURE__*/function () {
1611
+ var _ref2 = _asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee(categoryId) {
1612
+ var _yield$fetch, results;
1613
+
1614
+ return runtime_1.wrap(function _callee$(_context) {
1615
+ while (1) {
1616
+ switch (_context.prev = _context.next) {
1617
+ case 0:
1618
+ _context.next = 2;
1619
+ return fetch({
1620
+ query: options.query,
1621
+ method: options.method,
1622
+ variables: {
1623
+ category: categoryId,
1624
+ brand: brandId,
1625
+ search: search,
1626
+ sort: mappedSort,
1627
+ expand: ["variants"],
1628
+ limit: count
1629
+ }
1630
+ });
1631
+
1632
+ case 2:
1633
+ _yield$fetch = _context.sent;
1634
+ results = _yield$fetch.results;
1635
+ return _context.abrupt("return", results);
1636
+
1637
+ case 5:
1638
+ case "end":
1639
+ return _context.stop();
1640
+ }
1641
+ }
1642
+ }, _callee);
1643
+ }));
1564
1644
 
1565
- case 6:
1566
- _yield$fetch = _context.sent;
1567
- results = _yield$fetch.results;
1568
- found = _yield$fetch.count;
1569
- products = results.map(function (product) {
1570
- return normalizeProduct(product);
1571
- });
1572
- return _context.abrupt("return", {
1573
- products: products,
1574
- found: found
1645
+ return function _fetch(_x) {
1646
+ return _ref2.apply(this, arguments);
1647
+ };
1648
+ }();
1649
+
1650
+ if (includeSubCategories) {
1651
+ _context3.next = 12;
1652
+ break;
1653
+ }
1654
+
1655
+ _context3.next = 9;
1656
+ return _fetch(categoryId);
1657
+
1658
+ case 9:
1659
+ products = _context3.sent;
1660
+ _context3.next = 16;
1661
+ break;
1662
+
1663
+ case 12:
1664
+ includedCategories = walkCategoryTree(categories == null ? void 0 : categories.find(function (category) {
1665
+ return category.id === categoryId;
1666
+ }), categories);
1667
+ _context3.next = 15;
1668
+ return Promise.all(includedCategories.flatMap( /*#__PURE__*/function () {
1669
+ var _ref3 = _asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee2(category) {
1670
+ return runtime_1.wrap(function _callee2$(_context2) {
1671
+ while (1) {
1672
+ switch (_context2.prev = _context2.next) {
1673
+ case 0:
1674
+ return _context2.abrupt("return", _fetch(category.id));
1675
+
1676
+ case 1:
1677
+ case "end":
1678
+ return _context2.stop();
1679
+ }
1680
+ }
1681
+ }, _callee2);
1682
+ }));
1683
+
1684
+ return function (_x2) {
1685
+ return _ref3.apply(this, arguments);
1686
+ };
1687
+ }()));
1688
+
1689
+ case 15:
1690
+ products = _context3.sent.flatMap(function (products) {
1691
+ return products;
1692
+ }).slice(0, count);
1693
+
1694
+ case 16:
1695
+ return _context3.abrupt("return", {
1696
+ products: products.map(function (product) {
1697
+ return normalizeProduct(product);
1698
+ }),
1699
+ found: products.length > 0
1575
1700
  });
1576
1701
 
1577
- case 11:
1702
+ case 17:
1578
1703
  case "end":
1579
- return _context.stop();
1704
+ return _context3.stop();
1580
1705
  }
1581
1706
  }
1582
- }, _callee);
1707
+ }, _callee3);
1583
1708
  }))();
1584
1709
  },
1585
- useHook: function useHook(_ref2) {
1586
- var useData = _ref2.useData;
1710
+ useHook: function useHook(_ref4) {
1711
+ var useData = _ref4.useData;
1587
1712
  return function (input) {
1588
1713
  if (input === void 0) {
1589
1714
  input = {};
1590
1715
  }
1591
1716
 
1592
1717
  return useData({
1593
- input: [['search', input.search], ['categoryId', input.categoryId], ['brandId', input.brandId], ['sort', input.sort], ['count', input.count]],
1718
+ input: [['search', input.search], ['categoryId', input.categoryId], ['includeSubCategories', input.includeSubCategories], ['categories', input.categories], ['brandId', input.brandId], ['sort', input.sort], ['count', input.count]],
1594
1719
  swrOptions: _extends({
1595
1720
  revalidateOnFocus: false
1596
1721
  }, input.swrOptions)
@@ -1664,47 +1789,96 @@ var handler$6 = {
1664
1789
  method: 'get'
1665
1790
  },
1666
1791
  fetcher: function fetcher(_ref) {
1667
- return _asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee() {
1668
- var _data$results$map;
1792
+ return _asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee2() {
1793
+ var _data$results;
1669
1794
 
1670
- var fetch, data;
1671
- return runtime_1.wrap(function _callee$(_context) {
1795
+ var input, options, fetch, topologicalSort, addIsEmptyField, categoryId, data, categories;
1796
+ return runtime_1.wrap(function _callee2$(_context2) {
1672
1797
  while (1) {
1673
- switch (_context.prev = _context.next) {
1798
+ switch (_context2.prev = _context2.next) {
1674
1799
  case 0:
1675
- fetch = _ref.fetch;
1676
- _context.next = 3;
1800
+ input = _ref.input, options = _ref.options, fetch = _ref.fetch;
1801
+ topologicalSort = input.topologicalSort, addIsEmptyField = input.addIsEmptyField, categoryId = input.categoryId;
1802
+ _context2.next = 4;
1677
1803
  return fetch({
1678
- query: 'categories',
1679
- method: 'get'
1804
+ query: options.query,
1805
+ method: options.method,
1806
+ variables: {
1807
+ expand: [].concat(topologicalSort ? ["children", "parent_id"] : []),
1808
+ id: categoryId
1809
+ }
1680
1810
  });
1681
1811
 
1682
- case 3:
1683
- data = _context.sent;
1684
- return _context.abrupt("return", (_data$results$map = data.results.map(function (_ref2) {
1685
- var id = _ref2.id,
1686
- name = _ref2.name,
1687
- slug = _ref2.slug;
1688
- return {
1689
- id: id,
1690
- name: name,
1691
- slug: slug,
1692
- path: "/" + slug
1812
+ case 4:
1813
+ data = _context2.sent;
1814
+ categories = (_data$results = data == null ? void 0 : data.results) != null ? _data$results : [];
1815
+
1816
+ if (!addIsEmptyField) {
1817
+ _context2.next = 10;
1818
+ break;
1819
+ }
1820
+
1821
+ _context2.next = 9;
1822
+ return Promise.all(categories.map( /*#__PURE__*/function () {
1823
+ var _ref2 = _asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee(category) {
1824
+ return runtime_1.wrap(function _callee$(_context) {
1825
+ while (1) {
1826
+ switch (_context.prev = _context.next) {
1827
+ case 0:
1828
+ _context.t0 = _extends;
1829
+ _context.t1 = {};
1830
+ _context.t2 = category;
1831
+ _context.next = 5;
1832
+ return fetch({
1833
+ query: 'products',
1834
+ method: 'list',
1835
+ variables: {
1836
+ limit: 1,
1837
+ category: category.id
1838
+ }
1839
+ });
1840
+
1841
+ case 5:
1842
+ _context.t3 = _context.sent.results;
1843
+ _context.t4 = {
1844
+ products: _context.t3
1845
+ };
1846
+ return _context.abrupt("return", (0, _context.t0)(_context.t1, _context.t2, _context.t4));
1847
+
1848
+ case 8:
1849
+ case "end":
1850
+ return _context.stop();
1851
+ }
1852
+ }
1853
+ }, _callee);
1854
+ }));
1855
+
1856
+ return function (_x) {
1857
+ return _ref2.apply(this, arguments);
1693
1858
  };
1694
- })) != null ? _data$results$map : []);
1859
+ }()));
1695
1860
 
1696
- case 5:
1861
+ case 9:
1862
+ categories = _context2.sent;
1863
+
1864
+ case 10:
1865
+ return _context2.abrupt("return", (topologicalSort ? topologicalSortForCategoryTree(categories) : categories).map(function (category) {
1866
+ return normalizeCategory(category);
1867
+ }));
1868
+
1869
+ case 11:
1697
1870
  case "end":
1698
- return _context.stop();
1871
+ return _context2.stop();
1699
1872
  }
1700
1873
  }
1701
- }, _callee);
1874
+ }, _callee2);
1702
1875
  }))();
1703
1876
  },
1704
1877
  useHook: function useHook(_ref3) {
1705
1878
  var useData = _ref3.useData;
1706
1879
  return function (input) {
1707
1880
  var response = useData({
1881
+ input: [["topologicalSort", input == null ? void 0 : input.topologicalSort], ["addIsEmptyField", input == null ? void 0 : input.addIsEmptyField], ["categoryId", input == null ? void 0 : input.categoryId]],
1708
1882
  swrOptions: _extends({
1709
1883
  revalidateOnFocus: false
1710
1884
  }, input == null ? void 0 : input.swrOptions)
@@ -1927,6 +2101,9 @@ var getSwellProvider = function getSwellProvider(storeId, publicKey) {
1927
2101
  site: {
1928
2102
  useCategories: handler$6,
1929
2103
  useBrands: handler$7
2104
+ },
2105
+ extraFeatures: {
2106
+ includeSubCategories: true
1930
2107
  }
1931
2108
  };
1932
2109
  };