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.

103 lines
2.3 KiB

  1. package react.store;
  2. import react.ReactComponent;
  3. import react.ReactMacro.jsx;
  4. import Common;
  5. typedef CartProps = {
  6. var order:OrderSimple;
  7. var addToCart:ProductInfo -> Int -> Void;
  8. var removeFromCart:ProductInfo -> ?Int -> Void;
  9. var submitOrder:OrderSimple -> Void;
  10. };
  11. class Cart extends react.ReactComponentOfProps<CartProps>
  12. {
  13. function addToCart(product:ProductInfo, quantity:Int):Void {
  14. props.addToCart(product, quantity);
  15. }
  16. function removeFromCart(product:ProductInfo, quantity:Int):Void {
  17. props.removeFromCart(product, quantity);
  18. }
  19. function removeAllFromCart(product:ProductInfo):Void {
  20. props.removeFromCart(product);
  21. }
  22. function submitOrder():Void {
  23. props.submitOrder(props.order);
  24. }
  25. override public function render(){
  26. return jsx('
  27. <div className="cart">
  28. <h3>Ma Commande</h3>
  29. ${renderProducts()}
  30. ${renderFooter()}
  31. </div>
  32. ');
  33. }
  34. function renderProducts() {
  35. var productsToOrder = props.order.products.map(function(product:ProductWithQuantity) {
  36. var quantity = product.quantity;
  37. var product = product.product;
  38. return jsx('
  39. <div className="product-to-order" key=${product.name}>
  40. <div>${product.name}</div>
  41. <div>$quantity</div>
  42. <div className="cart-action-buttons">
  43. <div onClick=${function(){
  44. this.addToCart(product, 1);
  45. }}>
  46. +
  47. </div>
  48. <div onClick=${function(){
  49. this.removeFromCart(product, 1);
  50. }}>
  51. -
  52. </div>
  53. <div onClick=${function(){
  54. this.removeAllFromCart(product);
  55. }}>
  56. x
  57. </div>
  58. </div>
  59. </div>
  60. ');
  61. });
  62. return jsx('
  63. <div className="products-to-order">
  64. ${productsToOrder}
  65. </div>
  66. ');
  67. }
  68. function renderFooter() {
  69. var buttonClasses = ["order-button"];
  70. var submit = submitOrder;
  71. if (props.order.products.length == 0) {
  72. buttonClasses.push("order-button--disabled");
  73. submit = null;
  74. }
  75. return jsx('
  76. <div className="cart-footer">
  77. <div className="total">
  78. Total
  79. <div>${props.order.total} </div>
  80. </div>
  81. <div className=${buttonClasses.join(" ")} onClick=$submit>Commander</div>
  82. </div>
  83. ');
  84. }
  85. }