Initial commit
This commit is contained in:
74
api-wiaas/client/js/components/shop/shop-cart.service.js
Normal file
74
api-wiaas/client/js/components/shop/shop-cart.service.js
Normal file
@@ -0,0 +1,74 @@
|
||||
(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;
|
||||
}
|
||||
}
|
||||
})();
|
||||
Reference in New Issue
Block a user