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.

223 lines
5.6 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.router.HashRouter;
  8. import react.router.Route;
  9. import react.router.Switch;
  10. import react.router.Link;
  11. typedef OrderBoxState = {
  12. orders:Array<UserOrder>,
  13. error:String,
  14. users:Null<Array<UserInfo>>,
  15. };
  16. typedef OrderBoxProps = {
  17. userId:Int,
  18. distributionId:Int,
  19. contractId:Int,
  20. contractType:Int,
  21. date:String,
  22. place:String,
  23. userName:String,
  24. onValidate:Void->Void,
  25. currency:String,
  26. hasPayments:Bool
  27. };
  28. /**
  29. * A box to edit/add orders of a member
  30. * @author fbarbut
  31. */
  32. class OrderBox extends react.ReactComponentOfPropsAndState<OrderBoxProps,OrderBoxState>
  33. {
  34. public function new(props)
  35. {
  36. super(props);
  37. state = { orders : [], error : null, users:null };
  38. }
  39. override function componentDidMount()
  40. {
  41. //request api avec user + distrib
  42. HttpUtil.fetch("/api/order/get/"+props.userId, GET, {distributionId:props.distributionId,contractId:props.contractId}, PLAIN_TEXT)
  43. .then(function(data:String) {
  44. var data : {orders:Array<UserOrder>} = tink.Json.parse(data);
  45. /*for( o in orders){
  46. //convert ints to enums, enums have been lost in json serialization
  47. o.productUnit = Type.createEnumIndex(Unit, cast o.productUnit );
  48. }*/
  49. setState({orders:data.orders, error:null});
  50. if(props.contractType==0) loadUsers();
  51. }).catchError(function(data) {
  52. var data = Std.string(data);
  53. trace("Error",data);
  54. if(data.substr(0,1)=="{"){
  55. //json error from server
  56. var data : ErrorInfos = haxe.Json.parse(data);
  57. setState( cast {error:data.error.message} );
  58. }else{
  59. //js error
  60. setState( cast {error:data} );
  61. }
  62. });
  63. }
  64. /**
  65. * load user list when contract is constant orders
  66. */
  67. function loadUsers(){
  68. HttpUtil.fetch("/api/user/getFromGroup/", GET, {}, PLAIN_TEXT)
  69. .then(function(data:String) {
  70. var data : {users:Array<UserInfo>} = tink.Json.parse(data);
  71. setState({users:data.users, error:null});
  72. }).catchError(function(data) {
  73. var data = Std.string(data);
  74. if(data.substr(0,1)=="{"){
  75. //json error from server
  76. var data : ErrorInfos = haxe.Json.parse(data);
  77. setState( cast {error:data.error.message} );
  78. }else{
  79. //js error
  80. setState( cast {error:data} );
  81. }
  82. });
  83. }
  84. override public function render(){
  85. //edit orders
  86. var renderOrders = this.state.orders.map(function(o){
  87. var k :String = if(o.id!=null) {
  88. Std.string(o.id);
  89. } else {
  90. o.productId+"-"+Std.random(99999);
  91. };
  92. return jsx('<$Order key="$k" order="$o" onUpdate=$onUpdate parentBox=${this} />') ;
  93. } );
  94. var delivery = if(props.date==null){
  95. null;
  96. }else{
  97. jsx('<p>Pour la livraison du <b>${props.date}</b> à <b>${props.place}</b></p>');
  98. }
  99. var renderOrderBox = function() return jsx('
  100. <div onKeyPress=${onKeyPress}>
  101. <h3>Commandes de ${props.userName}</h3>
  102. $delivery
  103. <$Error error="${state.error}" />
  104. <hr/>
  105. <div className="row tableHeader">
  106. <div className="col-md-4">Produit</div>
  107. <div className="col-md-1">Ref.</div>
  108. <div className="col-md-1">Prix</div>
  109. <div className="col-md-2">Qté</div>
  110. <div className="col-md-1">Payé</div>
  111. <div className="col-md-3">Alterné avec</div>
  112. </div>
  113. ${renderOrders}
  114. <div>
  115. <a onClick=${onClick} className="btn btn-primary">
  116. <span className="glyphicon glyphicon-chevron-right"></span> Valider
  117. </a>
  118. &nbsp;
  119. <$Link className="btn btn-default" to="/insert"><span className="glyphicon glyphicon-plus-sign"></span> Ajouter un produit</$Link>
  120. </div>
  121. </div>
  122. ');
  123. var onProductSelected = function(uo:UserOrder){
  124. var existingOrder = Lambda.find(state.orders,function(x) return x.productId==uo.productId );
  125. if(existingOrder!=null){
  126. existingOrder.quantity += uo.quantity;
  127. this.setState(this.state);
  128. }else{
  129. this.state.orders.push(uo);
  130. this.setState(this.state);
  131. }
  132. };
  133. //insert product box
  134. var renderInsertBox = function(){
  135. return jsx('<$InsertOrder contractId="${props.contractId}" userId="${props.userId}" distributionId="${props.distributionId}" onInsert=$onProductSelected/>');
  136. }
  137. return jsx('<$HashRouter>
  138. <$Switch>
  139. <$Route path="/" exact=$true render=$renderOrderBox />
  140. <$Route path="/insert" exact=$true render=$renderInsertBox />
  141. </$Switch>
  142. </$HashRouter>');
  143. }
  144. /**
  145. * called when an order is updated
  146. */
  147. function onUpdate(data:UserOrder){
  148. /*trace("ON UPDATE : " + data);
  149. for ( o in state.orders){
  150. if (o.id == data.id) {
  151. o.quantity = data.quantity;
  152. o.paid = data.paid;
  153. break;
  154. }
  155. }
  156. setState(this.state);*/
  157. }
  158. /**
  159. * submit updated orders to the API
  160. */
  161. function onClick(?_){
  162. var data = new Array<{id:Int,productId:Int,qt:Float,paid:Bool,invertSharedOrder:Bool,userId2:Int}>();
  163. for ( o in state.orders) data.push({id:o.id, productId : o.productId, qt: o.quantity, paid : o.paid, invertSharedOrder:o.invertSharedOrder, userId2:o.userId2});
  164. var req = { orders:data };
  165. var p = HttpUtil.fetch("/api/order/update/"+props.userId+"?distributionId="+props.distributionId+"&contractId="+props.contractId, POST, req, JSON);
  166. p.then(function(data:Dynamic) {
  167. //WOOT
  168. if (props.onValidate != null) props.onValidate();
  169. }).catchError(function(data) {
  170. var data = Std.string(data);
  171. trace("Error",data);
  172. if(data.substr(0,1)=="{"){
  173. //json error from server
  174. var data : ErrorInfos = haxe.Json.parse(data);
  175. setState( cast {error:data.error.message} );
  176. }else{
  177. //js error
  178. setState( cast {error:data} );
  179. }
  180. });
  181. }
  182. function onKeyPress(e:js.html.KeyboardEvent){
  183. if(e.key=="Enter") onClick();
  184. }
  185. }