@plasmicpkgs/commerce-swell 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.
@@ -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,50 +1545,220 @@ 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);
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
1571
1700
  });
1572
- return _context.abrupt("return", {
1573
- products: products,
1574
- found: found
1701
+
1702
+ case 17:
1703
+ case "end":
1704
+ return _context3.stop();
1705
+ }
1706
+ }
1707
+ }, _callee3);
1708
+ }))();
1709
+ },
1710
+ useHook: function useHook(_ref4) {
1711
+ var useData = _ref4.useData;
1712
+ return function (input) {
1713
+ if (input === void 0) {
1714
+ input = {};
1715
+ }
1716
+
1717
+ return useData({
1718
+ input: [['search', input.search], ['categoryId', input.categoryId], ['includeSubCategories', input.includeSubCategories], ['categories', input.categories], ['brandId', input.brandId], ['sort', input.sort], ['count', input.count]],
1719
+ swrOptions: _extends({
1720
+ revalidateOnFocus: false
1721
+ }, input.swrOptions)
1722
+ });
1723
+ };
1724
+ }
1725
+ };
1726
+
1727
+ var handler$5 = {
1728
+ fetchOptions: {
1729
+ query: 'products',
1730
+ method: 'get'
1731
+ },
1732
+ fetcher: function fetcher(_ref) {
1733
+ return _asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee() {
1734
+ var input, options, fetch, id, product;
1735
+ return runtime_1.wrap(function _callee$(_context) {
1736
+ while (1) {
1737
+ switch (_context.prev = _context.next) {
1738
+ case 0:
1739
+ input = _ref.input, options = _ref.options, fetch = _ref.fetch;
1740
+ id = input.id;
1741
+ _context.next = 4;
1742
+ return fetch({
1743
+ query: options.query,
1744
+ method: options.method,
1745
+ variables: [id]
1575
1746
  });
1576
1747
 
1577
- case 11:
1748
+ case 4:
1749
+ product = _context.sent;
1750
+
1751
+ if (product) {
1752
+ _context.next = 7;
1753
+ break;
1754
+ }
1755
+
1756
+ return _context.abrupt("return", null);
1757
+
1758
+ case 7:
1759
+ return _context.abrupt("return", normalizeProduct(product));
1760
+
1761
+ case 8:
1578
1762
  case "end":
1579
1763
  return _context.stop();
1580
1764
  }
@@ -1590,7 +1774,7 @@ var handler$4 = {
1590
1774
  }
1591
1775
 
1592
1776
  return useData({
1593
- input: [['search', input.search], ['categoryId', input.categoryId], ['brandId', input.brandId], ['sort', input.sort], ['count', input.count]],
1777
+ input: [['id', input.id]],
1594
1778
  swrOptions: _extends({
1595
1779
  revalidateOnFocus: false
1596
1780
  }, input.swrOptions)
@@ -1599,53 +1783,102 @@ var handler$4 = {
1599
1783
  }
1600
1784
  };
1601
1785
 
1602
- var handler$5 = {
1786
+ var handler$6 = {
1603
1787
  fetchOptions: {
1604
1788
  query: 'categories',
1605
1789
  method: 'get'
1606
1790
  },
1607
1791
  fetcher: function fetcher(_ref) {
1608
- return _asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee() {
1609
- var _data$results$map;
1792
+ return _asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee2() {
1793
+ var _data$results;
1610
1794
 
1611
- var fetch, data;
1612
- 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) {
1613
1797
  while (1) {
1614
- switch (_context.prev = _context.next) {
1798
+ switch (_context2.prev = _context2.next) {
1615
1799
  case 0:
1616
- fetch = _ref.fetch;
1617
- _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;
1618
1803
  return fetch({
1619
- query: 'categories',
1620
- method: 'get'
1804
+ query: options.query,
1805
+ method: options.method,
1806
+ variables: {
1807
+ expand: [].concat(topologicalSort ? ["children", "parent_id"] : []),
1808
+ id: categoryId
1809
+ }
1621
1810
  });
1622
1811
 
1623
- case 3:
1624
- data = _context.sent;
1625
- return _context.abrupt("return", (_data$results$map = data.results.map(function (_ref2) {
1626
- var id = _ref2.id,
1627
- name = _ref2.name,
1628
- slug = _ref2.slug;
1629
- return {
1630
- id: id,
1631
- name: name,
1632
- slug: slug,
1633
- 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);
1634
1858
  };
1635
- })) != null ? _data$results$map : []);
1859
+ }()));
1636
1860
 
1637
- 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:
1638
1870
  case "end":
1639
- return _context.stop();
1871
+ return _context2.stop();
1640
1872
  }
1641
1873
  }
1642
- }, _callee);
1874
+ }, _callee2);
1643
1875
  }))();
1644
1876
  },
1645
1877
  useHook: function useHook(_ref3) {
1646
1878
  var useData = _ref3.useData;
1647
1879
  return function (input) {
1648
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]],
1649
1882
  swrOptions: _extends({
1650
1883
  revalidateOnFocus: false
1651
1884
  }, input == null ? void 0 : input.swrOptions)
@@ -1666,7 +1899,7 @@ var handler$5 = {
1666
1899
  }
1667
1900
  };
1668
1901
 
1669
- var handler$6 = {
1902
+ var handler$7 = {
1670
1903
  fetchOptions: {
1671
1904
  query: 'attributes',
1672
1905
  method: 'get'
@@ -1862,11 +2095,15 @@ var getSwellProvider = function getSwellProvider(storeId, publicKey) {
1862
2095
  useRemoveItem: handler$2
1863
2096
  },
1864
2097
  products: {
1865
- useSearch: handler$4
2098
+ useSearch: handler$4,
2099
+ useProduct: handler$5
1866
2100
  },
1867
2101
  site: {
1868
- useCategories: handler$5,
1869
- useBrands: handler$6
2102
+ useCategories: handler$6,
2103
+ useBrands: handler$7
2104
+ },
2105
+ extraFeatures: {
2106
+ includeSubCategories: true
1870
2107
  }
1871
2108
  };
1872
2109
  };