You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

73 lines
1.8 KiB

  1. package react.store;
  2. import react.ReactComponent;
  3. import react.ReactMacro.jsx;
  4. import Common;
  5. using Lambda;
  6. typedef ProductListProps = {
  7. var categories:Array<CategoryInfo>;
  8. var productsBySubcategoryIdMap:Map<Int, Array<ProductInfo>>;
  9. var filters:Array<String>;
  10. var addToCart:ProductInfo -> Int -> Void;
  11. };
  12. class ProductList extends react.ReactComponentOfProps<ProductListProps>
  13. {
  14. override public function render(){
  15. return jsx('
  16. <div className="categories">
  17. ${renderCategories()}
  18. </div>
  19. ');
  20. }
  21. function renderCategories() {
  22. return props.categories.map(function(category) {
  23. if (!props.filters.has(category.name))
  24. return null;
  25. return jsx('
  26. <div className="category" key=${category.name}>
  27. <h2>${category.name}</h2>
  28. <div className="subCategories">
  29. ${renderSubCategories(category)}
  30. </div>
  31. </div>
  32. ');
  33. });
  34. }
  35. function renderSubCategories(category) {
  36. var subCategories = category.subcategories.map(function(category) {
  37. if (!props.productsBySubcategoryIdMap.exists(category.id))
  38. return jsx('<div key=${category.name}>Loading...</div>');
  39. var products = props.productsBySubcategoryIdMap.get(category.id);
  40. return jsx('
  41. <div className="sub-category" key=${category.name}>
  42. <h3>${category.name}</h3>
  43. <div className="products">
  44. ${renderProducts(products)}
  45. </div>
  46. </div>
  47. ');
  48. });
  49. return jsx('
  50. <div className="sub-categories">
  51. $subCategories
  52. </div>
  53. ');
  54. }
  55. function renderProducts(products) {
  56. return products.map(function(product) {
  57. return jsx('
  58. <Product product=${product} key=${product.id} addToCart=${props.addToCart}/>
  59. ');
  60. });
  61. }
  62. }