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.

65 lines
1.5 KiB

  1. package react.store;
  2. import js.Browser.window;
  3. import react.ReactComponent;
  4. import react.ReactMacro.jsx;
  5. import Common;
  6. typedef ProductProps = {
  7. var product:ProductInfo;
  8. var addToCart:ProductInfo -> Int -> Void;
  9. };
  10. typedef ProductState = {
  11. var quantity:Int;
  12. };
  13. class Product extends react.ReactComponentOfPropsAndState<ProductProps, ProductState>
  14. {
  15. static inline var OVERLAY_URL = '/shop/productInfo';
  16. static inline var IMAGE_WIDTH = 120;
  17. static inline var IMAGE_HEIGHT = 120;
  18. public function new() {
  19. super();
  20. state = {
  21. quantity: 1
  22. };
  23. }
  24. function openOverlay() {
  25. untyped window._.overlay('$OVERLAY_URL/${props.product.id}', props.product.name);
  26. }
  27. function updateQuantity(event:Dynamic) {
  28. var quantity = Std.parseInt(event.target.value);
  29. if (Std.is(quantity, Int) && quantity > 0)
  30. setState({
  31. quantity: Std.int(quantity)
  32. });
  33. }
  34. function addToCart() {
  35. props.addToCart(props.product, state.quantity);
  36. }
  37. override public function render(){
  38. var product = props.product;
  39. return jsx('
  40. <div className="product">
  41. <img src=${product.image} width=${IMAGE_WIDTH+'px'} height=${IMAGE_HEIGHT+'px'} alt={product.name} />
  42. <div className="body">
  43. <a onClick=$openOverlay>
  44. ${product.name}
  45. </a>
  46. <div>${product.price} </div>
  47. <input type="number" value=${state.quantity} onChange=$updateQuantity />
  48. <div className="button" onClick=$addToCart>Ajouter</div>
  49. </div>
  50. </div>
  51. ');
  52. }
  53. }