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.

111 lines
2.9 KiB

  1. package react.order;
  2. import react.ReactDOM;
  3. import react.ReactComponent;
  4. import react.ReactMacro.jsx;
  5. import Common;
  6. import utils.HttpUtil;
  7. import react.product.ProductSelect;
  8. import react.router.Redirect;
  9. import react.router.Link;
  10. /**
  11. * A box to add an order to a member
  12. * @author fbarbut
  13. */
  14. class InsertOrder extends react.ReactComponentOfPropsAndState<{contractId:Int,userId:Int,distributionId:Int,onInsert:UserOrder->Void},{products:Array<ProductInfo>,error:String,selected:Int}>
  15. {
  16. public function new(props)
  17. {
  18. super(props);
  19. state = {products:[],error:null,selected:null};
  20. }
  21. override function componentDidMount()
  22. {
  23. //load product list
  24. HttpUtil.fetch("/api/product/get/", GET, {contractId:props.contractId},PLAIN_TEXT)
  25. .then(function(data:String) {
  26. /*var data : {products:Array<ProductInfo>} = haxe.Json.parse(data);
  27. for( p in data.products) {
  28. p.unitType = Type.createEnumIndex(UnitType,untyped p.unitType);
  29. }*/
  30. var data : {products:Array<ProductInfo>} = tink.Json.parse(data);
  31. setState({products:data.products, error:null,selected:null});
  32. }).catchError(function(data) {
  33. var data = Std.string(data);
  34. trace("Error",data);
  35. if(data.substr(0,1)=="{"){
  36. //json error from server
  37. var data : ErrorInfos = haxe.Json.parse(data);
  38. setState(cast {error:data.error.message} );
  39. }else{
  40. //js error
  41. setState(cast {error:data} );
  42. }
  43. });
  44. }
  45. override public function render(){
  46. //redirect to orderBox if a product is selected
  47. var redirect = if(state.selected!=null) jsx('<$Redirect to="/" />') else null;
  48. return jsx('
  49. <div>
  50. $redirect
  51. <h3>Choisissez le produit à ajouter</h3>
  52. <$Link className="btn btn-default" to="/"><span className="glyphicon glyphicon-chevron-left"></span> Retour</$Link>
  53. <$Error error="${state.error}" />
  54. <hr />
  55. <$ProductSelect onSelect=$onSelectProduct products=${state.products} />
  56. </div>
  57. ');
  58. }
  59. function onSelectProduct(p:ProductInfo){
  60. var uo : UserOrder = cast {
  61. id:null,
  62. product:p,
  63. quantity:1,
  64. productId:p.id,
  65. productPrice:p.price,
  66. paid:false,
  67. invert:false,
  68. user2:null
  69. };
  70. props.onInsert(uo);
  71. setState(cast {selected:p.id});
  72. //do not insert order now, just warn the OrderBox
  73. /*
  74. //insert order
  75. var data = [{id:null,productId:p.id,qt:1,paid:false,invert:false,user2:null} ];
  76. var req = {
  77. orders:haxe.Json.stringify(data),
  78. distributionId : props.distributionId,
  79. contractId : props.contractId
  80. };
  81. var r = HttpUtil.fetch("/api/order/update/"+props.userId, POST, req, JSON);
  82. r.then(function(d:Dynamic) {
  83. if (Reflect.hasField(d, "error")) {
  84. setState(cast {error:d.error.message});
  85. }else{
  86. //WOOT
  87. //trace("OK");
  88. //go to OrderBox with a redirect
  89. setState(cast {selected:p.id});
  90. }
  91. }).catchError(function(d) {
  92. trace("PROMISE ERROR", d);
  93. setState(cast {error:d.error.message});
  94. });
  95. */
  96. }
  97. }