code from amapei
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
package utils;
|
||||
|
||||
import Math;
|
||||
using Lambda;
|
||||
|
||||
import Common;
|
||||
|
||||
class CartUtils {
|
||||
public static function addToCart(order:OrderSimple, productToAdd:ProductInfo, quantity:Int):OrderSimple {
|
||||
var products = order.products.copy();
|
||||
var total = order.total;
|
||||
|
||||
var existingProduct = products.find(function(p) {
|
||||
return p.product.id == productToAdd.id;
|
||||
});
|
||||
|
||||
if (existingProduct == null)
|
||||
products.push({
|
||||
product: productToAdd,
|
||||
quantity: quantity
|
||||
});
|
||||
else
|
||||
existingProduct.quantity += quantity;
|
||||
|
||||
total += quantity * productToAdd.price;
|
||||
total = Math.round(total * 100) / 100; // to avoid calculation errors
|
||||
|
||||
return {
|
||||
products: products,
|
||||
total: total
|
||||
};
|
||||
}
|
||||
|
||||
public static function removeFromCart(order:OrderSimple, productToRemove:ProductInfo, ?quantity:Int):OrderSimple {
|
||||
var products = order.products.copy();
|
||||
var total = order.total;
|
||||
|
||||
var existingProduct = products.find(function(p) {
|
||||
return p.product.id == productToRemove.id;
|
||||
});
|
||||
|
||||
if (quantity == null)
|
||||
quantity = existingProduct.quantity;
|
||||
|
||||
if (existingProduct == null)
|
||||
throw "Can't remove a non existing product";
|
||||
else if (quantity >= existingProduct.quantity)
|
||||
products.remove(existingProduct)
|
||||
else
|
||||
existingProduct.quantity -= quantity;
|
||||
|
||||
if (products.length == 0)
|
||||
total = 0;
|
||||
else {
|
||||
total -= Math.min(quantity, existingProduct.quantity) * productToRemove.price;
|
||||
total = Math.round(total * 100) / 100; // to avoid calculation errors
|
||||
}
|
||||
|
||||
return {
|
||||
products: products,
|
||||
total: total
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,115 @@
|
||||
package utils;
|
||||
|
||||
import haxe.Json;
|
||||
import js.Promise;
|
||||
import js.html.XMLHttpRequest;
|
||||
|
||||
@:enum abstract HttpMethod(String) to String {
|
||||
var POST = 'POST';
|
||||
var GET = 'GET';
|
||||
var HEAD = 'HEAD';
|
||||
var PUT = 'PUT';
|
||||
var DELETE = 'DELETE';
|
||||
var TRACE = 'TRACE';
|
||||
var OPTIONS = 'OPTIONS';
|
||||
var CONNECT = 'CONNECT';
|
||||
var PATCH = 'GET';
|
||||
}
|
||||
|
||||
@:enum abstract FetchFormat(String) from String to String {
|
||||
var PLAIN_TEXT = "text/plain";
|
||||
var JSON = "application/json";
|
||||
}
|
||||
|
||||
//json version of a tink.core.Error
|
||||
typedef ErrorInfos = {error:{code:Int,message:String,stack:String}}
|
||||
|
||||
/**
|
||||
* Manage HTTP request to a REST API.
|
||||
*
|
||||
* POST requests can only have a single JSON object (payload)
|
||||
*/
|
||||
class HttpUtil
|
||||
{
|
||||
static public function fetch(
|
||||
url: String,
|
||||
?method: HttpMethod = GET,
|
||||
?params: Dynamic = null,
|
||||
?accept: FetchFormat = PLAIN_TEXT,
|
||||
?contentType: String = JSON
|
||||
): Promise<Dynamic> {
|
||||
|
||||
return new Promise(function(resolve: Dynamic->Void, reject) {
|
||||
var data: String = null;
|
||||
if (params != null)
|
||||
{
|
||||
if (params.body != null){
|
||||
data = Json.stringify(params.body);
|
||||
} else if(method==POST) {
|
||||
data = Json.stringify(params);
|
||||
} else {
|
||||
url += (url.indexOf('?') > -1) ? '&' : '?';
|
||||
url += objToString(params);
|
||||
}
|
||||
}
|
||||
|
||||
var http = new XMLHttpRequest();
|
||||
http.open(method, url, true);
|
||||
|
||||
if (contentType != null && contentType.length > 0)
|
||||
http.setRequestHeader("Content-type", contentType);
|
||||
|
||||
if (accept != null)
|
||||
http.setRequestHeader("Accept", accept);
|
||||
|
||||
http.onreadystatechange = function() {
|
||||
//trace("readystate",http.readyState, http.status);
|
||||
if (http.readyState == 4){
|
||||
switch (http.status){
|
||||
case 200:
|
||||
switch (accept){
|
||||
case JSON:
|
||||
try {
|
||||
var json = Json.parse(http.responseText);
|
||||
resolve(json);
|
||||
} catch (err: Dynamic){
|
||||
reject(err);
|
||||
}
|
||||
default:
|
||||
resolve(http.responseText);
|
||||
}
|
||||
|
||||
case 204:
|
||||
resolve(true);
|
||||
|
||||
default:
|
||||
reject(http.responseText);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
http.send(data);
|
||||
});
|
||||
}
|
||||
|
||||
static public function objToString(obj: Dynamic): String
|
||||
{
|
||||
var str = "";
|
||||
var cpt = 0;
|
||||
for (key in Reflect.fields(obj))
|
||||
{
|
||||
var value: Dynamic = Reflect.field(obj, key);
|
||||
if (value == null) continue;
|
||||
|
||||
if (cpt++ > 0)
|
||||
str += "&";
|
||||
|
||||
if (Std.is(value, Array) && value.length > 0)
|
||||
str += '$key=${value.join(";")}';
|
||||
else if (Std.string(value) != "") // String / Int / Float
|
||||
str += '$key=${StringTools.trim(Std.string(value))}';
|
||||
}
|
||||
return str;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user