UNPKG

@beamimpact/web-sdk

Version:

The Beam SDK enables brands to connect with their customers over shared values, not transactional discounts, to build stronger loyalty. Our integration achieves this by allowing customers to (a) choose a nonprofit where the brand will donate part of their

33 lines (32 loc) 7.68 kB
import{g as o,t as a,h as I,y as h}from"../chunks/lit-WqMxC_PA.esm.js";import{M as f}from"../chunks/beam-errors-Ci0d3926.esm.js";import{deleteCookieValue as v,getCookieValue as b}from"../utils/cookies.esm.js";import{c as S,d as P,e as $}from"../chunks/enforce-config-CZ3ToOgr.esm.js";import{createScopedLocalStorage as T}from"../utils/local-storage.esm.js";import{A as U,_ as E}from"../chunks/localize-Btu9xYcE.esm.js";import{D as O,L as u}from"../chunks/routes-DguZveS3.esm.js";import{p as A}from"../chunks/index-C5gnsl34.esm.js";import"./impact-overview.esm.js";import{B as C}from"../chunks/cart-contents-DkoytiZh.esm.js";import{b as D}from"../chunks/events-Dki0ka4F.esm.js";import{formatStoreAndReturnPromoCodes as R,getAllPromoCodesFromCookie as _,mapToUnvalidatedPromoCodes as M,isEmptyPromoData as w,parseJsonStringArray as K}from"../utils/promoManager.esm.js";import{P as N,a as L}from"../chunks/promo-types-DKAOFHJr.esm.js";import"../utils/logger.esm.js";import"../chunks/lodash-D3TLHRR_.esm.js";import"../chunks/progress-bar-DF7UuuHb.esm.js";import"../chunks/promo-pill-label-WGLW3bql.esm.js";import"../chunks/_share-dialog-dependencies-dHlBUqo0.esm.js";import"../chunks/vendor-KKSARHWL.esm.js";import"./beam-partner-logos.esm.js";import"../chunks/loading-template-DG4lkIIc.esm.js";import"../chunks/css-card-grid-CvoGpsJH.esm.js";import"../chunks/share-button-DmjRJmXh.esm.js";import"../chunks/is-all-html-elements-fcB7UUA1.esm.js";import"../chunks/share-button-M8cMa6ol.esm.js";import"../chunks/responsive-BR8qUfBa.esm.js";var x=Object.defineProperty,e=(l,r,i,m)=>{for(var s=void 0,d=l.length-1,c;d>=0;d--)(c=l[d])&&(s=c(r,i,s)||s);return s&&x(r,i,s),s};const y=["baseUrl","apiKey","orderId","cartTotal","currencyCode","lang"],F=["storeId","postalCode","countryCode","cart","discountCodes","promoCodes","htmlPromoCodes","email","partnerUserId"];class t extends I{constructor(){super(...arguments),this.baseUrl=O,this.currencyCode="USD",this.lang="en",this.debug=!1,this.activeWidget=null,this.nonprofitId=null,this.selectionId=null,this.cartId=null,this.beamCartId=null,this.userId=null,this.transactionId=null,this.excludeDonationsForRecurringTransactions=!1,this.creationMethod="",this.draftConfig=!1,this.localStorage=T(this),this.handlePostTransaction=async()=>{this.enforceConfig(),this.updateComponentState();const r=this.getManualPromoCodes(),i=R(r.map(p=>({value:p})),this.apiKey),m=_(),s={unvalidatedPromoCodes:M(r.map(p=>({value:p}))),validatedPromoCodes:m?.validatedPromoCodes??[]},d=i??s,c=d&&!w(d),n=await A({baseUrl:this.baseUrl,apiRoot:"/api",headers:{authorization:`Api-Key ${this.apiKey}`},requestBody:{selectionId:this.selectionId||void 0,cartId:this.cartId||void 0,beamCartId:this.beamCartId||void 0,storeId:this.storeId,orderId:this.orderId,cartTotal:this.cartTotal,email:this.email,partnerUserId:this.partnerUserId,currencyCode:this.currencyCode,postalCode:this.postalCode,countryCode:this.countryCode,cart:this.cart,...c&&{promos:d},discountCodes:this.discountCodes}});this.transactionId=n.transactionId,this.userId=n.beamUserId,this.nonprofitId=n.nonprofitId,this.excludeDonationsForRecurringTransactions=!!n.reportSettings?.excludeRecurringDonations,this.creationMethod=n.creationMethod||"";const g=!!this.nonprofitId;return this.activeWidget=g?"impact-overview":"redeem-transaction",n.transactionCreated&&(["transaction","cart",N,u.NONPROFIT,u.NONPROFIT_SELECTED_AT].forEach(p=>{this.localStorage.removeItem(p)}),[{name:C,path:"/",domain:this.domain},{name:L,path:"/",domain:this.domain}].forEach(p=>v(p)),dispatchEvent(new D({transactionId:n.transactionId,orderId:this.orderId,cartTotal:this.cartTotal,currencyCode:this.currencyCode}))),n},this.transactionDataController=new U(this,this.handlePostTransaction)}get parsedPromoCodes(){return K(this.htmlPromoCodes)}updateComponentState(){this.localStorage.getItem("transaction")?.toLocaleLowerCase()==="undefined"&&this.localStorage.removeItem("transaction"),this.selectionId=this.selectionId||this.localStorage.getItem("transaction")||null,this.cartId=this.cartId||this.localStorage.getItemJson("cart")?.cartId||null,this.beamCartId=this.beamCartId||this.localStorage.getItemJson("cart")?.beamCartId||b(C)||null,this.selectionId&&this.nonprofitId&&(this.activeWidget="impact-overview")}firstUpdated(r){this.enforceConfig(),this.updateComponentState()}async updated(r){[...y,...F].some(i=>r.has(i))&&await this.transactionDataController.exec()}enforceConfig(){if($(y,this),!this.email&&!this.partnerUserId)throw new f([this.email?"":"email",this.partnerUserId?"":"partnerUserId"].filter(Boolean),"At least one of [email, partnerUserId] must be provided")}getManualPromoCodes(){return this.parsedPromoCodes?.length?this.parsedPromoCodes:this.promoCodes?.length?this.promoCodes:[]}render(){return this.transactionDataController.loading?"":this.transactionDataController.error?this.debug?E({error:this.transactionDataController.error}):"":this.excludeDonationsForRecurringTransactions&&this.creationMethod==="recurring"?h``:this.activeWidget==="redeem-transaction"?h` <beam-redeem-transaction part="redeem-transaction" baseUrl="${this.baseUrl}" apiKey="${this.apiKey}" storeId="${this.storeId}" postalCode="${this.postalCode}" countryCode="${this.countryCode}" transactionId="${this.transactionId}" cart="${JSON.stringify(this.cart)}" lang="${this.lang}" excludeDonationsForRecurringTransactions="${this.excludeDonationsForRecurringTransactions}" ?draftConfig="${this.draftConfig}" ?debug="${this.debug}" @beamnonprofitselect="${r=>{if(!(r instanceof CustomEvent))return;const{selectedNonprofitId:i}=r.detail;this.nonprofitId=i??null,this.activeWidget="impact-overview"}}" ></beam-redeem-transaction> `:this.activeWidget==="impact-overview"?h` <beam-impact-overview part="impact-overview" storeId="${this.storeId}" baseUrl="${this.baseUrl}" apiKey="${this.apiKey}" userId="${this.userId}" nonprofitId="${this.nonprofitId}" lang="${this.lang}" discountCodes="${JSON.stringify(this.discountCodes)}" transactionId="${this.transactionId}" ?draftConfig="${this.draftConfig}" ?debug="${this.debug}" ></beam-impact-overview> `:""}}t.tagName="beam-post-purchase",t.styles=[S],e([o({type:String})],t.prototype,"baseUrl"),e([o({type:String})],t.prototype,"apiKey"),e([o({type:Number,reflect:!0})],t.prototype,"storeId"),e([o({type:String})],t.prototype,"postalCode"),e([o({type:String})],t.prototype,"countryCode"),e([o({type:String})],t.prototype,"orderId"),e([o({type:String})],t.prototype,"email"),e([o({type:String})],t.prototype,"partnerUserId"),e([o({type:Number})],t.prototype,"cartTotal"),e([o({type:String})],t.prototype,"currencyCode"),e([o({type:String})],t.prototype,"lang"),e([o({type:Object})],t.prototype,"cart"),e([o({type:Array})],t.prototype,"discountCodes"),e([o({type:Array})],t.prototype,"promoCodes"),e([o({type:String})],t.prototype,"htmlPromoCodes"),e([o({type:String})],t.prototype,"domain"),e([o({type:Boolean})],t.prototype,"debug"),e([a()],t.prototype,"activeWidget"),e([a()],t.prototype,"nonprofitId"),e([a()],t.prototype,"selectionId"),e([a()],t.prototype,"cartId"),e([a()],t.prototype,"beamCartId"),e([a()],t.prototype,"userId"),e([a()],t.prototype,"transactionId"),e([a()],t.prototype,"excludeDonationsForRecurringTransactions"),e([a()],t.prototype,"creationMethod"),e([o({type:Boolean})],t.prototype,"draftConfig"),P(t);export{t as BeamPostPurchase}; //# sourceMappingURL=post-purchase.esm.js.map