package react.order; import react.ReactDOM; import react.ReactComponent; import react.ReactMacro.jsx; import Common; import utils.HttpUtil; import react.router.HashRouter; import react.router.Route; import react.router.Switch; import react.router.Link; typedef OrderBoxState = { orders:Array, error:String, users:Null>, }; typedef OrderBoxProps = { userId:Int, distributionId:Int, contractId:Int, contractType:Int, date:String, place:String, userName:String, onValidate:Void->Void, currency:String, hasPayments:Bool }; /** * A box to edit/add orders of a member * @author fbarbut */ class OrderBox extends react.ReactComponentOfPropsAndState { public function new(props) { super(props); state = { orders : [], error : null, users:null }; } override function componentDidMount() { //request api avec user + distrib HttpUtil.fetch("/api/order/get/"+props.userId, GET, {distributionId:props.distributionId,contractId:props.contractId}, PLAIN_TEXT) .then(function(data:String) { var data : {orders:Array} = tink.Json.parse(data); /*for( o in orders){ //convert ints to enums, enums have been lost in json serialization o.productUnit = Type.createEnumIndex(Unit, cast o.productUnit ); }*/ setState({orders:data.orders, error:null}); if(props.contractType==0) loadUsers(); }).catchError(function(data) { var data = Std.string(data); trace("Error",data); if(data.substr(0,1)=="{"){ //json error from server var data : ErrorInfos = haxe.Json.parse(data); setState( cast {error:data.error.message} ); }else{ //js error setState( cast {error:data} ); } }); } /** * load user list when contract is constant orders */ function loadUsers(){ HttpUtil.fetch("/api/user/getFromGroup/", GET, {}, PLAIN_TEXT) .then(function(data:String) { var data : {users:Array} = tink.Json.parse(data); setState({users:data.users, error:null}); }).catchError(function(data) { var data = Std.string(data); if(data.substr(0,1)=="{"){ //json error from server var data : ErrorInfos = haxe.Json.parse(data); setState( cast {error:data.error.message} ); }else{ //js error setState( cast {error:data} ); } }); } override public function render(){ //edit orders var renderOrders = this.state.orders.map(function(o){ var k :String = if(o.id!=null) { Std.string(o.id); } else { o.productId+"-"+Std.random(99999); }; return jsx('<$Order key="$k" order="$o" onUpdate=$onUpdate parentBox=${this} />') ; } ); var delivery = if(props.date==null){ null; }else{ jsx('

Pour la livraison du ${props.date} à ${props.place}

'); } var renderOrderBox = function() return jsx('

Commandes de ${props.userName}

$delivery <$Error error="${state.error}" />
Produit
Ref.
Prix
Qté
Payé
Alterné avec
${renderOrders}
Valider   <$Link className="btn btn-default" to="/insert"> Ajouter un produit
'); var onProductSelected = function(uo:UserOrder){ var existingOrder = Lambda.find(state.orders,function(x) return x.productId==uo.productId ); if(existingOrder!=null){ existingOrder.quantity += uo.quantity; this.setState(this.state); }else{ this.state.orders.push(uo); this.setState(this.state); } }; //insert product box var renderInsertBox = function(){ return jsx('<$InsertOrder contractId="${props.contractId}" userId="${props.userId}" distributionId="${props.distributionId}" onInsert=$onProductSelected/>'); } return jsx('<$HashRouter> <$Switch> <$Route path="/" exact=$true render=$renderOrderBox /> <$Route path="/insert" exact=$true render=$renderInsertBox /> '); } /** * called when an order is updated */ function onUpdate(data:UserOrder){ /*trace("ON UPDATE : " + data); for ( o in state.orders){ if (o.id == data.id) { o.quantity = data.quantity; o.paid = data.paid; break; } } setState(this.state);*/ } /** * submit updated orders to the API */ function onClick(?_){ var data = new Array<{id:Int,productId:Int,qt:Float,paid:Bool,invertSharedOrder:Bool,userId2:Int}>(); 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}); var req = { orders:data }; var p = HttpUtil.fetch("/api/order/update/"+props.userId+"?distributionId="+props.distributionId+"&contractId="+props.contractId, POST, req, JSON); p.then(function(data:Dynamic) { //WOOT if (props.onValidate != null) props.onValidate(); }).catchError(function(data) { var data = Std.string(data); trace("Error",data); if(data.substr(0,1)=="{"){ //json error from server var data : ErrorInfos = haxe.Json.parse(data); setState( cast {error:data.error.message} ); }else{ //js error setState( cast {error:data} ); } }); } function onKeyPress(e:js.html.KeyboardEvent){ if(e.key=="Enter") onClick(); } }