Files
old-wiaas-legacy/api-wiaas/client/js/components/shop/shop-cart.service.js
2018-06-11 11:09:35 +02:00

75 lines
2.3 KiB
JavaScript

(function () {
global.dashModule
.service('shopCartService', ['$http', 'utilsService', shopCartService])
.controller('shopCartIconCtrl', ['$scope', 'shopCartService', shopCartIconCtrl]);
function shopCartService($http, utilsService) {
let shopCartItems = 0;
let isTermsAndConditionsChecked = false;
const calbackFunctions = [];
return {
updateShopCartCount,
registerCallbackFunction,
setTermsCheckboxValues,
getTermsCheckboxValue,
sumPrice
};
function updateShopCartCount() {
$http({
method: 'POST',
url: 'shop/api/getShopCartCount'
}).then((response) => {
if (response.data && response.data.newShopCartItemsCount) {
shopCartItems = response.data.newShopCartItemsCount;
} else {
shopCartItems = 0;
}
calbackFunctions.forEach((calbackFunction) => {
calbackFunction(shopCartItems);
});
});
}
function registerCallbackFunction(newFunction) {
calbackFunctions.push(newFunction);
}
function setTermsCheckboxValues(isTermsChecked) {
isTermsAndConditionsChecked = isTermsChecked;
utilsService.executeRegisteredFunction('isActionBtnDisabled', !isTermsChecked);
}
function getTermsCheckboxValue() {
return isTermsAndConditionsChecked;
}
function sumPrice(values, quantity) {
let total = 0;
values.forEach((val) => {
total += parseFloat(val);
});
return quantity ? total * quantity : 0;
}
}
function shopCartIconCtrl($scope, shopCartService) {
$scope.shopCartItemsCount = shopCartService.shopCartItems;
$scope.hasPackagesInCart = hasPackagesInCart;
shopCartService.registerCallbackFunction(updateCartValue);
shopCartService.updateShopCartCount();
function updateCartValue(newCartValue) {
$scope.shopCartItemsCount = newCartValue;
}
function hasPackagesInCart() {
return $scope.shopCartItemsCount > 0;
}
}
})();