{"version":3,"file":"js/chunks/addToCart.js","sources":["webpack:///./org_colony/cartridge/js/pages/product/addToCart.js"],"sourcesContent":["const Promise = require('promise');\nconst map = require('lodash.map');\nconst dialog = require('../../dialog');\nconst minicart = require('../../minicart');\nconst page = require('../../page');\nconst productUtil = require('./productUtil');\nconst util = require('../../util');\n\n/**\n * @description Make the AJAX request to add an item to cart\n * @param {Element} form The form element that contains the item quantity and ID data\n * @returns {Promise}\n */\nfunction addItemToCart(form) {\n const $form = $(form);\n let url;\n const $qty = $form.find('input[name=\"Quantity\"], select[name=\"Quantity\"]');\n\n if ($qty.length === 0 || Number.isNaN($qty.val()) || parseInt($qty.val(), 10) === 0 || Number($qty.val()) < 0) {\n $qty.val('1');\n }\n\n setTimeout(() => {\n $form.find('button[type=\"submit\"]').css('pointer-events', 'all');\n // Make sure to enable Add All button for Sets\n if ($('#add-all-to-cart').length) {\n $('#add-all-to-cart').css('pointer-events', 'all');\n }\n }, 1000);\n if ($('#add-all-to-cart').length > 0 && $('#add-all-to-cart').hasClass('added-to-cart')) {\n const pSetimgUrl = $('.pdpMain').find('#showPSetImg').val();\n const pSetName = $('.pdpMain').find('#showPSetName').val();\n const itemQty = $('.product-set-item form').not('.suppressed').find('input[name=\"Quantity\"]');\n let pSetQtyVal = 0;\n itemQty.each((index, element) => {\n pSetQtyVal += parseInt($(element).val(), 10);\n });\n\n url = util.appendParamsToUrl(Urls.addProduct, {\n pSetAdded: true, pSetimgUrl, pSetName, pSetQty: pSetQtyVal,\n });\n } else {\n url = Urls.addProduct;\n }\n return Promise.resolve($.ajax({\n type: 'POST',\n url: util.ajaxUrl(url),\n data: $form.serialize(),\n })).then((response) => {\n // handle error in the response\n if (response.error) {\n throw new Error(response.error);\n } else {\n return response;\n }\n });\n}\n\n/**\n * @description Handler to handle the add to cart event\n */\nconst disabledAddToCart = (e) => {\n e.preventDefault();\n $(e.currentTarget).parents('form').find('.availability-novariation').addClass('error-msg');\n};\nconst addToCart = (e) => {\n e.preventDefault();\n\n // check product option for fee\n const formID = $(e.currentTarget).closest('form').attr('id');\n productUtil.checkForOptionProductFee(formID);\n\n const $form = $(e.currentTarget).closest('form');\n\n // add form validation for personalization and validate Pick 'n Choose\n if (!$form.valid() || !productUtil.validatePickNChoose()) {\n // reveal any hidden fields\n $form.find('a.show-more').click();\n return;\n }\n\n $(e.currentTarget).css('pointer-events', 'none');\n\n addItemToCart($form).then((response) => {\n const $uuid = $form.find('input[name=\"uuid\"]');\n if ($uuid.length > 0 && $uuid.val().length > 0) {\n const params = util.getQueryStringParams(window.location);\n\n // redirect to referring source or refresh\n if (params.source === 'cart') {\n page.redirect(Urls.cartShow);\n } else {\n // wishlist or registry\n page.refresh();\n }\n } else {\n // Always close open dialogs and show the Add to Cart modal\n dialog.close();\n minicart.showmodal(response);\n }\n }, () => {\n productUtil.showLimitErrorDialog();\n });\n};\n\n/**\n * @description Handler to handle the add all items to cart event\n */\nconst addAllToCart = (e) => {\n e.preventDefault();\n const $productForms = $('#product-set-list').find('form').not('.suppressed').toArray();\n $(e.currentTarget).css('pointer-events', 'none').addClass('added-to-cart');\n\n Promise.all(map($productForms, addItemToCart)).then((responses) => {\n dialog.close();\n // show the final response only, which would include all the other items\n minicart.showmodal(responses[responses.length - 1]);\n $('#add-all-to-cart').removeClass('added-to-cart');\n });\n};\n\n/**\n * @function\n * @description Binds the click event to a given target for the add-to-cart handling\n */\nmodule.exports = function addToCartHandlers() {\n $('.product-detail, .svc-result-items').on('click', '.add-to-cart-disabled', disabledAddToCart);\n $('.product-detail, .svc-result-items').on('click', '.add-to-cart', addToCart);\n $('#add-all-to-cart').on('click', addAllToCart);\n};\n"],"mappings":";;;;;;;;;AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AAEA;AACA;AACA;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AAEA;AACA;AAAA;AAAA;AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AAEA;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;;;A","sourceRoot":""}