1import{Component}from"@theme/component";import{morphSection}from"@theme/section-renderer";import{DiscountUpdateEvent}from"@theme/events";import{fetchConfig}from"@theme/utilities";class CartDiscount extends Component{requiredRefs=["cartDiscountError","cartDiscountErrorDiscountCode","cartDiscountErrorShipping"];#activeFetch=null;updatedCallback(){super.updatedCallback(),this.#activeFetch&&(this.#activeFetch.abort(),this.#activeFetch=null),this.refs.cartDiscountButton&&this.#toggleButtonLoading(!1)}#createAbortController(){this.#activeFetch&&this.#activeFetch.abort();const abortController=new AbortController;return this.#activeFetch=abortController,abortController}applyDiscount=async event=>{event.preventDefault(),event.stopPropagation();const form=event.target;if(!(form instanceof HTMLFormElement))return;const discountCode=form.querySelector('input[name="discount"]');if(!(discountCode instanceof HTMLInputElement)||typeof this.dataset.sectionId!="string")return;const{cartDiscountError,cartDiscountErrorDiscountCode,cartDiscountErrorShipping,cartDiscountErrorExists,cartDiscountButton}=this.refs;if(cartDiscountButton.getAttribute("disabled"))return;cartDiscountError.classList.add("hidden"),cartDiscountErrorDiscountCode.classList.add("hidden"),cartDiscountErrorShipping.classList.add("hidden"),cartDiscountErrorExists.classList.add("hidden");const discountCodeValue=discountCode.value.trim().toLowerCase(),abortController=this.#createAbortController();try{const existingDiscounts=this.#existingDiscounts();if(existingDiscounts.map(code=>code.trim().toLowerCase()).includes(discountCodeValue)){this.#handleDiscountErrorExists();return}this.#toggleButtonLoading(!0);const config=fetchConfig("json",{body:JSON.stringify({discount:[...existingDiscounts,discountCodeValue].join(","),sections:[this.dataset.sectionId]})}),data=await(await fetch(FoxTheme.routes.cart_update_url,{...config,signal:abortController.signal})).json();if(this.#toggleButtonLoading(!1),data.discount_codes.find(discount=>discount.code===discountCodeValue&&discount.applicable===!1)){discountCode.value="",this.#handleDiscountError("discount_code");return}const newHtml=data.sections[this.dataset.sectionId],section=new DOMParser().parseFromString(newHtml,"text/html").getElementById(`shopify-section-${this.dataset.sectionId}`),discountCodes=section?.querySelectorAll(".cart-discount__pill")||[];if(section){const codes=Array.from(discountCodes).map(element=>element instanceof HTMLLIElement?element.dataset.discountCode:null).filter(Boolean);if(codes.length===existingDiscounts.length&&codes.every(code=>existingDiscounts.includes(code))&&data.discount_codes.find(discount=>discount.code===discountCodeValue&&discount.applicable===!0)){this.#handleDiscountError("shipping"),discountCode.value="";return}}document.dispatchEvent(new DiscountUpdateEvent(data,this.dataset.sectionId)),morphSection(this.dataset.sectionId,newHtml)}catch{}finally{this.#activeFetch=null}};removeDiscount=async event=>{if(event.preventDefault(),event.stopPropagation(),event instanceof KeyboardEvent&&event.key!=="Enter"||!(event instanceof MouseEvent)||!(event.target instanceof HTMLElement)||typeof this.dataset.sectionId!="string")return;const pill=event.target.closest(".cart-discount__pill");if(!(pill instanceof HTMLLIElement))return;const discountCode=pill.dataset.discountCode;if(!discountCode)return;const existingDiscounts=this.#existingDiscounts(),index=existingDiscounts.indexOf(discountCode);if(index===-1)return;existingDiscounts.splice(index,1);const abortController=this.#createAbortController();try{const config=fetchConfig("json",{body:JSON.stringify({discount:existingDiscounts.join(","),sections:[this.dataset.sectionId]})}),data=await(await fetch(FoxTheme.routes.cart_update_url,{...config,signal:abortController.signal})).json();document.dispatchEvent(new DiscountUpdateEvent(data,this.dataset.sectionId)),morphSection(this.dataset.sectionId,data.sections[this.dataset.sectionId])}catch{}finally{this.#activeFetch=null}};#handleDiscountError(type){const{cartDiscountError,cartDiscountErrorDiscountCode,cartDiscountErrorShipping}=this.refs,target=type==="discount_code"?cartDiscountErrorDiscountCode:cartDiscountErrorShipping;cartDiscountError.classList.remove("hidden"),target.classList.remove("hidden")}#handleDiscountErrorExists(){const{cartDiscountErrorExists,cartDiscountError}=this.refs;cartDiscountError.classList.remove("hidden"),cartDiscountErrorExists.classList.remove("hidden")}#existingDiscounts(){const discountCodes=[],discountPills=this.querySelectorAll(".cart-discount__pill");for(const pill of discountPills)pill instanceof HTMLLIElement&&typeof pill.dataset.discountCode=="string"&&discountCodes.push(pill.dataset.discountCode);return discountCodes}#toggleButtonLoading(isLoading=!0){const{cartDiscountButton}=this.refs;cartDiscountButton.classList.toggle("btn--loading",isLoading),isLoading?cartDiscountButton.setAttribute("disabled",!0):cartDiscountButton.removeAttribute("disabled")}}customElements.get("cart-discount")||customElements.define("cart-discount",CartDiscount); 2//# sourceMappingURL=/cdn/shop/t/66/assets/cart-discount.js.map?v=166537729360890343331781871785
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.