{"version":3,"file":"cart-cf4d77e5.js","sources":["../../../../../../src/scripts/modules/cartCounter.ts","../../../../../../src/scripts/modules/cart.ts"],"sourcesContent":["import { Component } from '@verndale/core';\n\nclass CartCounter extends Component {\n setupDefaults() {\n this.dom = {\n cartList: document.querySelector('.cart__items'),\n cartCounterMobile: document.querySelector('.mobile-header__cart-button-counter'),\n cartCounterDesktop: document.querySelector('.desktop-header__cart-button-counter')\n };\n }\n\n checkCartItems() {\n const cartItems = (this.dom.cartList as HTMLElement).querySelectorAll('.cart__item');\n if (cartItems.length > 0) {\n (this.dom.cartCounterMobile as HTMLElement).innerHTML = cartItems.length.toString();\n (this.dom.cartCounterMobile as HTMLElement).removeAttribute('hidden');\n (this.dom.cartCounterDesktop as HTMLElement).innerHTML = cartItems.length.toString();\n (this.dom.cartCounterDesktop as HTMLElement).removeAttribute('hidden');\n } else {\n (this.dom.cartCounterMobile as HTMLElement).setAttribute('hidden', \"true\");\n (this.dom.cartCounterDesktop as HTMLElement).setAttribute('hidden', \"true\");\n }\n }\n}\n\nexport default CartCounter;\n","import { Component } from '@verndale/core';\nimport axios from 'axios';\nimport CartCounter from './cartCounter';\n\nexport type ProductProps = {\n ImageUrl: string;\n ImageDescription: string;\n Size: string;\n Title: string;\n Url: string;\n ColorValue: string;\n ColorName: string;\n Id: string;\n}\n\ndeclare global {\n interface Window {\n addProduct: (productId: string) => void;\n getProducts: () => void;\n removeProduct: (e: Event) => void;\n }\n}\n\nclass Cart extends Component {\n counter: CartCounter;\n mq: MediaQueryList;\n\n constructor(el: HTMLElement) {\n super(el);\n this.getProducts();\n\n window.addProduct = this.addProduct.bind(this);\n window.getProducts = this.getProducts.bind(this);\n window.removeProduct = this.removeProduct.bind(this);\n this.counter = new CartCounter(this.dom.counter as HTMLElement);\n this.mq = window.matchMedia('(max-width: 1024px)');\n this.resizeBasedOnSafari();\n }\n\n setupDefaults() {\n this.dom = {\n buttonsContainer: this.el.querySelector('.cart__buttons'),\n mainContainer: this.el.querySelector('.cart__content'),\n emptyCart: this.el.querySelector('.cart__content-empty'),\n itemsList: this.el.querySelector('.cart__items'),\n cartDescription: this.el.querySelector('.cart__description'),\n counter: document.querySelector('body'),\n cartContainerDesktop: document.querySelector(\n '.cart__container'\n ),\n cartBtnDesktop: document.querySelector('.desktop-header__nav__utility-nav-item--cart'),\n cartBtnMobile: document.querySelector('.mobile-header__cart-button button'),\n cartContainerMobile: document.querySelector(\n '.cart__container'\n )\n };\n }\n\n addListeners() {\n if (this.mq) {\n this.mq.addEventListener('change', this.resizeBasedOnSafari.bind(this));\n }\n }\n\n cleanCart() {\n (this.dom.itemsList as HTMLElement).innerHTML = '';\n }\n\n async addProduct(productId: string) {\n if (this.el.dataset.addProductsEndpoint) {\n try {\n const { data } = await axios.post(this.el.dataset.addProductsEndpoint, { id: productId });\n if (data && data.d && data.d.code) {\n this.renderAlert(data.d.errorMessage);\n } else {\n this.counter.checkCartItems();\n if (window.innerWidth > 1024) {\n this.toggleCart(this.dom.cartBtnDesktop as HTMLElement, this.dom.cartContainerDesktop as HTMLElement);\n } else {\n this.toggleCart(this.dom.cartBtnMobile as HTMLElement, this.dom.cartContainerMobile as HTMLElement);\n }\n }\n } catch (error) {\n console.error(error);\n } finally {\n this.cleanCart();\n this.getProducts();\n }\n }\n }\n\n toggleCart(button: HTMLElement, container: HTMLElement) {\n // eslint-disable-next-line @typescript-eslint/ban-ts-comment\n // @ts-ignore\n window.scrollTo(0, 0, { behavior: 'smooth' });\n button.classList.toggle('is-active');\n\n if (container.hidden) {\n container.removeAttribute('hidden');\n document.body.style.overflow = 'hidden';\n } else {\n container.setAttribute('hidden', 'true');\n document.body.style.overflow = 'auto';\n }\n }\n\n renderAlert(message: string) {\n const alert = `\n \n
\n
\n \n \n \n
\n
\n \n ${message}\n \n
\n \n
\n
\n `;\n // eslint-disable-next-line @typescript-eslint/ban-ts-comment\n // @ts-ignore\n window.scrollTo(0, 0, { behavior: 'smooth' });\n document.body.insertAdjacentHTML('beforeend', alert);\n const alertCloseButton = document.querySelector('.cart-alert__close-button');\n const alertOverlay = document.querySelector('.cart-alert__close-button');\n (alertCloseButton as HTMLElement).addEventListener('click', this.removeAlert.bind(this));\n (alertOverlay as HTMLElement).addEventListener('click', this.removeAlert.bind(this));\n }\n\n removeAlert() {\n const alert = document.querySelector('.cart-alert');\n if (alert) {\n alert.remove();\n }\n }\n\n async getProducts() {\n console.log(this.el.dataset.getProductsEndpoint);\n if (this.el.dataset.getProductsEndpoint) {\n try {\n const { data } = await axios.post(this.el.dataset.getProductsEndpoint, {});\n if (data && data.d && data.d.Products && data.d.Products.length > 0) {\n (this.dom.emptyCart as HTMLElement).setAttribute('hidden', 'true');\n (this.dom.itemsList as HTMLElement).removeAttribute('hidden');\n (this.dom.mainContainer as HTMLElement).removeAttribute('hidden');\n (this.dom.cartDescription as HTMLElement).innerHTML = data.d.CartDescription;\n this.cleanCart();\n this.renderProducts(data.d.Products);\n this.addRemoveProductListeners();\n this.counter.checkCartItems();\n } else {\n (this.dom.mainContainer as HTMLElement).setAttribute('hidden', 'true');\n (this.dom.itemsList as HTMLElement).setAttribute('hidden', 'true');\n (this.dom.emptyCart as HTMLElement).removeAttribute('hidden');\n }\n } catch (error) {\n console.error(error);\n }\n }\n }\n\n addRemoveProductListeners() {\n const removeProductBtns = this.el.querySelectorAll('.cart__item__remove-button');\n removeProductBtns.forEach(btn => {\n btn.addEventListener('click', this.removeProduct.bind(this));\n });\n }\n\n removeProduct(e: Event) {\n const target = e.target as HTMLElement;\n const cartItem = (target as HTMLElement).closest('.cart__item');\n if(!cartItem) return;\n const productId = (cartItem as HTMLElement).dataset.productId;\n if(!productId) return;\n this.setRemoveProduct(productId);\n target.closest('.cart__item')?.remove();\n\n const cartItems = this.el.querySelectorAll('.cart__item');\n if (cartItems.length === 0) {\n (this.dom.mainContainer as HTMLElement).setAttribute('hidden', 'true');\n (this.dom.itemsList as HTMLElement).setAttribute('hidden', 'true');\n (this.dom.emptyCart as HTMLElement).removeAttribute('hidden');\n }\n }\n\n async setRemoveProduct(productId: string) {\n if (this.el.dataset.removeProductsEndpoint) {\n try {\n const { data } = await axios.post(this.el.dataset.removeProductsEndpoint, { id: productId });\n if (data) {\n this.counter.checkCartItems();\n }\n } catch (error) {\n console.error(error);\n }\n }\n }\n\n renderProducts(products: ProductProps[]) {\n products.forEach(product => {\n const item = this.createProductItem(product);\n (this.dom.itemsList as HTMLElement).insertAdjacentHTML('beforeend', item);\n });\n }\n\n createProductItem(product: ProductProps) {\n return `\n
  • \n
    \n \n
    \n
    \n \n ${product.Title}\n \n
    \n
    \n ${product.Size}\n
    \n ${product.ColorName && product.ColorValue ? `
    \n Color:\n ${product.ColorName}\n
    ` : ''}\n
    \n
    \n \n
  • \n `;\n }\n\n resizeBasedOnSafari() {\n if (this.mq.matches) {\n\n const isSafari = /^((?!chrome|android).)*safari/i.test(navigator.userAgent);\n\n if (isSafari) {\n console.log('Safari detected');\n\n (this.dom.buttonsContainer as HTMLElement).style.paddingBlockEnd = '10vh';\n }\n }\n }\n}\n\nexport default Cart;\n"],"names":["CartCounter","Component","cartItems","Cart","el","__publicField","productId","data","axios","error","button","container","message","alert","alertCloseButton","alertOverlay","btn","e","target","cartItem","_a","products","product","item"],"mappings":"yPAEA,MAAMA,UAAoBC,CAAU,CAClC,eAAgB,CACd,KAAK,IAAM,CACT,SAAU,SAAS,cAA2B,cAAc,EAC5D,kBAAmB,SAAS,cAA2B,qCAAqC,EAC5F,mBAAoB,SAAS,cAA2B,sCAAsC,CAAA,CAElG,CAEA,gBAAiB,CACf,MAAMC,EAAa,KAAK,IAAI,SAAyB,iBAAiB,aAAa,EAC/EA,EAAU,OAAS,GACpB,KAAK,IAAI,kBAAkC,UAAYA,EAAU,OAAO,WACxE,KAAK,IAAI,kBAAkC,gBAAgB,QAAQ,EACnE,KAAK,IAAI,mBAAmC,UAAYA,EAAU,OAAO,WACzE,KAAK,IAAI,mBAAmC,gBAAgB,QAAQ,IAEpE,KAAK,IAAI,kBAAkC,aAAa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