1import{Component}from"@theme/component";import{morphSection}from"@theme/section-renderer";import{fetchConfig}from"@theme/utilities";import{cartPerformance}from"@theme/performance";import{CartDiscountUpdateEvent,CartErrorEvent}from"@shopify/events";class CartDiscount extends Component{requiredRefs=["cartDiscountError","cartDiscountErrorDiscountCode","cartDiscountErrorShipping"];#activeFetch=null;#createAbortController(){this.#activeFetch&&this.#activeFetch.abort();const abortController=new AbortController;return this.#activeFetch=abortController,abortController}applyDiscount=async event=>{const{cartDiscountError,cartDiscountErrorDiscountCode,cartDiscountErrorShipping}=this.refs;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 discountCodeValue=discountCode.value,abortController=this.#createAbortController(),existingDiscounts=this.#existingDiscounts();if(existingDiscounts.includes(discountCodeValue))return;cartDiscountError.classList.add("hidden"),cartDiscountErrorDiscountCode.classList.add("hidden"),cartDiscountErrorShipping.classList.add("hidden");const allDiscountCodes=[...existingDiscounts,discountCodeValue],deferredPromise=CartDiscountUpdateEvent.createPromise();this.dispatchEvent(new CartDiscountUpdateEvent({discountCodes:allDiscountCodes.map(code=>({code})),promise:deferredPromise.promise}));try{const config=fetchConfig("json",{body:JSON.stringify({discount:allDiscountCodes.join(","),sections:[this.dataset.sectionId]})}),data=await(await fetch(Theme.routes.cart_update_url,{...config,signal:abortController.signal})).json();if(data.discount_codes.find(discount=>discount.code===discountCodeValue&&discount.applicable===!1)){discountCode.value="",this.#handleDiscountError("discount_code"),deferredPromise.resolve({cart:CartDiscountUpdateEvent.createCartFromAjaxResponse(data)});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="",deferredPromise.resolve({cart:CartDiscountUpdateEvent.createCartFromAjaxResponse(data)});return}}deferredPromise.resolve({cart:CartDiscountUpdateEvent.createCartFromAjaxResponse(data)}),discountCode.value="",morphSection(this.dataset.sectionId,newHtml,{mode:this.closest("theme-drawer")?"hydration":"full"})}catch(error){deferredPromise.reject(error),error instanceof Error&&error.name!=="AbortError"&&this.dispatchEvent(new CartErrorEvent({error:error.message||"Failed to apply discount",code:"SERVICE_UNAVAILABLE"}))}finally{this.#activeFetch=null,cartPerformance.measureFromEvent("discount-update:user-action",event)}};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(),deferredPromise=CartDiscountUpdateEvent.createPromise();this.dispatchEvent(new CartDiscountUpdateEvent({discountCodes:existingDiscounts.map(code=>({code})),promise:deferredPromise.promise}));try{const config=fetchConfig("json",{body:JSON.stringify({discount:existingDiscounts.join(","),sections:[this.dataset.sectionId]})}),data=await(await fetch(Theme.routes.cart_update_url,{...config,signal:abortController.signal})).json();deferredPromise.resolve({cart:CartDiscountUpdateEvent.createCartFromAjaxResponse(data)}),morphSection(this.dataset.sectionId,data.sections[this.dataset.sectionId],{mode:this.closest("theme-drawer")?"hydration":"full"})}catch(error){deferredPromise.reject(error),error instanceof Error&&error.
1name!=="AbortError"&&this.dispatchEvent(new CartErrorEvent({error:error.message||"Failed to remove discount",code:"SERVICE_UNAVAILABLE"}))}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");const errorMessage=type==="discount_code"?"Invalid discount code":"Discount not applicable for shipping";this.dispatchEvent(new CartErrorEvent({error:errorMessage,code:"VALIDATION_CUSTOM"}))}#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}}customElements.get("cart-discount-component")||customElements.define("cart-discount-component",CartDiscount); 2//# sourceMappingURL=/cdn/shop/t/18/assets/cart-discount.js.map?v=138990160074728809221789404390
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.