@plasmicpkgs/commerce-swell 0.0.9 → 0.0.12

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) {
@@ -1553,19 +1567,26 @@ var walkCategoryTree = function walkCategoryTree(category, categories) {
1553
1567
  var result = [];
1554
1568
 
1555
1569
  while (queue.length > 0) {
1556
- var _curr$children$map, _curr$children;
1570
+ var _curr$children$result, _curr$children;
1557
1571
 
1558
1572
  var curr = ensure(queue.shift());
1559
1573
  result.push(curr);
1560
- queue.push.apply(queue, (_curr$children$map = (_curr$children = curr.children) == null ? void 0 : _curr$children.map(function (child) {
1574
+ queue.push.apply(queue, (_curr$children$result = (_curr$children = curr.children) == null ? void 0 : _curr$children.results.map(function (child) {
1561
1575
  return ensure(categories.find(function (category) {
1562
1576
  return category.id === child.id;
1563
1577
  }));
1564
- })) != null ? _curr$children$map : []);
1578
+ })) != null ? _curr$children$result : []);
1565
1579
  }
1566
1580
 
1567
1581
  return result;
1568
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
+ };
1569
1590
 
1570
1591
  var handler$4 = {
1571
1592
  fetchOptions: {
@@ -1768,52 +1789,96 @@ var handler$6 = {
1768
1789
  method: 'get'
1769
1790
  },
1770
1791
  fetcher: function fetcher(_ref) {
1771
- return _asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee() {
1772
- var _data$results$map;
1792
+ return _asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee2() {
1793
+ var _data$results;
1773
1794
 
1774
- var fetch, data;
1775
- 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) {
1776
1797
  while (1) {
1777
- switch (_context.prev = _context.next) {
1798
+ switch (_context2.prev = _context2.next) {
1778
1799
  case 0:
1779
- fetch = _ref.fetch;
1780
- _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;
1781
1803
  return fetch({
1782
- query: 'categories',
1783
- method: 'get',
1804
+ query: options.query,
1805
+ method: options.method,
1784
1806
  variables: {
1785
- expand: ["children"]
1807
+ expand: [].concat(topologicalSort ? ["children", "parent_id"] : []),
1808
+ id: categoryId
1786
1809
  }
1787
1810
  });
1788
1811
 
1789
- case 3:
1790
- data = _context.sent;
1791
- return _context.abrupt("return", (_data$results$map = data.results.map(function (_ref2) {
1792
- var id = _ref2.id,
1793
- name = _ref2.name,
1794
- slug = _ref2.slug,
1795
- children = _ref2.children;
1796
- return {
1797
- id: id,
1798
- name: name,
1799
- slug: slug,
1800
- path: "/" + slug,
1801
- children: children.results
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);
1802
1858
  };
1803
- })) != null ? _data$results$map : []);
1859
+ }()));
1804
1860
 
1805
- 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:
1806
1870
  case "end":
1807
- return _context.stop();
1871
+ return _context2.stop();
1808
1872
  }
1809
1873
  }
1810
- }, _callee);
1874
+ }, _callee2);
1811
1875
  }))();
1812
1876
  },
1813
1877
  useHook: function useHook(_ref3) {
1814
1878
  var useData = _ref3.useData;
1815
1879
  return function (input) {
1816
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]],
1817
1882
  swrOptions: _extends({
1818
1883
  revalidateOnFocus: false
1819
1884
  }, input == null ? void 0 : input.swrOptions)