package react.store; import react.ReactComponent; import react.ReactMacro.jsx; import haxe.Json; using Lambda; import Common; import utils.CartUtils; import utils.HttpUtil; typedef StoreProps = { var place:Int; var date:String; }; typedef StoreState = { var place:PlaceInfos; var orderByEndDates:Array; var categories:Array; var productsBySubcategoryIdMap:Map>; var order:OrderSimple; var filters:Array; }; class Store extends react.ReactComponentOfPropsAndState { static inline var CATEGORY_URL = '/api/shop/categories'; static inline var PRODUCT_URL = '/api/shop/products'; static inline var INIT_URL = '/api/shop/init'; static inline var VIEW_URL = '/place/view'; public function new() { super(); state = { place: null, orderByEndDates: [], categories: [], filters: [], productsBySubcategoryIdMap: new Map(), order: { products: [], total: 0 } }; } override function componentDidMount() { var categoriesRequest = HttpUtil.fetch(CATEGORY_URL, GET, {date: props.date, place: props.place}, JSON); var initRequest = HttpUtil.fetch(INIT_URL, GET, {date: props.date, place: props.place}, JSON); initRequest.then(function(infos:Dynamic) { setState({ place: infos.place, orderByEndDates: infos.orderEndDates }); }) .catchError(function(error) { trace("ERROR", error); }); categoriesRequest.then(function(categories:Dynamic) { var categories:Array = categories.categories; var subCategories = []; for (category in categories) { subCategories = subCategories.concat(category.subcategories); } setState({ categories: categories, filters: categories.map(function(category) { return category.name; }) }); subCategories.map(function(category:CategoryInfo) { return HttpUtil.fetch(PRODUCT_URL, GET, {date: props.date, place: props.place, subcategory: category.id}, JSON) .then(function(result) { var productsBySubcategoryIdMapCopy = [ for (key in state.productsBySubcategoryIdMap.keys()) key => state.productsBySubcategoryIdMap.get(key) ]; productsBySubcategoryIdMapCopy.set(category.id, result.products); setState({ productsBySubcategoryIdMap: productsBySubcategoryIdMapCopy }); }); }); }) .catchError(function(error) { trace("ERROR", error); }); } function toggleFilter(category:String) { var filters = state.filters.copy(); if (state.filters.find(function(categoryInFilter) { return category == categoryInFilter; }) != null) filters.remove(category); else filters.push(category); if (filters.length == 0) filters = state.categories.map(function(category) { return category.name; }); setState({ filters: filters }); } function addToCart(productToAdd:ProductInfo, quantity:Int):Void { setState({ order: CartUtils.addToCart(state.order, productToAdd, quantity) }); } function removeFromCart(productToRemove:ProductInfo, ?quantity:Int):Void { setState({ order: CartUtils.removeFromCart(state.order, productToRemove, quantity) }); } function submitOrder(order:OrderSimple) { var orderInSession = { total: order.total, products: order.products.map(function(p:ProductWithQuantity){ return { productId: p.product.id, quantity: p.quantity }; }) } trace('Order', orderInSession); } override public function render(){ return jsx('
${renderHeader()}
'); } function renderHeader() { if (state.orderByEndDates == null || state.orderByEndDates.length == 0) return null; var endDates; if (state.orderByEndDates.length == 1) { var orderEndDate = state.orderByEndDates[0].date; endDates = [jsx('
La commande fermera le $orderEndDate
')]; } else { endDates = state.orderByEndDates.map(function(order) { if (order.contracts.length == 1) return jsx('
La commande ${order.contracts[0]} fermera le: ${order.date}
'); return jsx('
Les autres commandes fermeront: ${order.date}
'); }); } var viewUrl = '$VIEW_URL/${props.place}'; var addressBlock = Lambda.array([ state.place.address1, state.place.address2, [state.place.zipCode, state.place.city].join(" "), ].mapi(function(index, element) { if (element == null) return null; return jsx('
$element
'); })); return jsx('
Distribution le ${props.date}
$endDates
${state.place.name}
$addressBlock
'); } }