Created
November 21, 2015 16:07
-
-
Save joliveros/88293359e810131184d2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// import angular from 'angular'; | |
const DEBUG = require('../../common/Debug')('signup.controller'); //eslint-disable-line | |
class CartController { | |
constructor( | |
$http, | |
$localStorage, | |
$q, | |
$rootScope, | |
$scope, | |
$state, | |
$stateParams, | |
$timeout, | |
ENV, | |
Restangular, | |
toaster, | |
User | |
){ | |
var that = this; | |
// debugger; | |
var cartPromises, getCartItems, local, ordersCountryId; | |
local = new Date(); | |
local.setMinutes(local.getMinutes() - local.getTimezoneOffset()); | |
that.minDate = local.toJSON().slice(0, 10); | |
let { | |
isRecurring | |
} = $stateParams; | |
DEBUG($state); | |
DEBUG(JSON.stringify($stateParams)); | |
$scope.isRecurring = isRecurring === 'true'; | |
$scope.recurringDate = $scope.recurringDate || new Date(); | |
$scope.recurringDays = $scope.recurringDays || 28; | |
that.cartEdited = false; | |
that.allDone = false; | |
ordersCountryId = $localStorage.ordersCountryId || 1; | |
that.totalPrice = 0; | |
cartPromises = []; | |
that.showRecurring = function(){ | |
return !ENV.showRecurring; | |
}; | |
that.getTotalPrice = function() { | |
var orders, total; | |
total = 0; | |
orders = that.shoppingCartOrders; | |
angular.forEach(orders, function(order, index) { | |
if (order.productDetails) { | |
return total += order.productDetails.price * order.quantity; | |
} | |
}); | |
return total; | |
}; | |
that.removeItem = function(index) { | |
that.shoppingCartOrders[index].remove = true; | |
return that.updateCart().then(function() { | |
that.shoppingCartOrders.splice(index, 1); | |
}); | |
}; | |
that.updateCart = function() { | |
// debugger; | |
var requestForm; | |
that.cartEdited = false; | |
requestForm = { | |
"Products": that.shoppingCartOrders | |
}; | |
var httpHeaders = Restangular.defaultHeaders; | |
httpHeaders['Content-type']='application/json;charset=utf-8'; | |
return $http({ | |
method: 'PUT', | |
headers: httpHeaders, | |
data: requestForm, | |
url: Restangular.configuration.baseUrl+'/order/cart/' + User.getUserId() + '/country/' + ordersCountryId | |
}) | |
// return Restangular.oneUrl('order/cart/' + User.getUserId() + '/country/' + ordersCountryId).customPUT(requestForm, void 0, void 0, {}) | |
.then(function(res) { | |
getCartItems(); | |
if (res.isSuccess === false) { | |
toaster.pop('error', "error " + res.httpStatusCode, res.message); | |
} | |
}); | |
}; | |
getCartItems = function() { | |
let updateCart = false; | |
that.loading = true; | |
Restangular.oneUrl('order/cart/' + User.getUserId() + '/country/' + ordersCountryId).customGET().then(function(orders) { | |
that.shoppingCartOrders = orders.plain(); | |
that.shoppingCartOrders.map((order)=>{ | |
// debugger; | |
if(!order.quantity){ | |
updateCart = true; | |
order.quantity = 1; | |
} | |
return order; | |
}) | |
$localStorage.shoppingCartOrders = that.shoppingCartOrders; | |
angular.forEach(that.shoppingCartOrders, function(order, index) { | |
cartPromises.push(Restangular.oneUrl('product/' + order.productId + '/' + User.getUserId() + "/" + $localStorage.ordersCountryId).customGET().then(function(product) { | |
product = product.plain(); | |
that.shoppingCartOrders[index].productDetails = product; | |
// that.shoppingCartOrders[index].productDetails.Price = that.shoppingCartOrders[index].ProductPrice; | |
// that.shoppingCartOrders[index].productDetails.Volume = that.shoppingCartOrders[index].volume; | |
})); | |
}); | |
$q.all(cartPromises).then(function() { | |
$timeout(function() { | |
that.allDone = true; | |
that.loading = false; | |
}); | |
}); | |
}); | |
}; | |
getCartItems(); | |
} | |
} | |
export default CartController; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment