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

32 lines (31 loc) 6.14 kB
import{g as o,t as s,h as I,y as l}from"../chunks/lit-iNN5L_Qk.esm.js";import{M as y}from"../chunks/beam-errors-P-Lu07Ce.esm.js";import{deleteCookieValue as g,getCookieValue as u}from"../utils/cookies.js";import{c as f,d as C,e as b}from"../chunks/enforce-config-ZyPq_4CF.esm.js";import{createScopedLocalStorage as v}from"../utils/local-storage.js";import{A as $,_ as S}from"../chunks/localize-m-_ESmpk.esm.js";import{D as U,p as w}from"../chunks/routes-o1QpF40R.esm.js";import"./redeem-transaction.js";import"./impact-overview.js";import{B as h}from"../chunks/cart-contents-h60geKWa.esm.js";import{a as T}from"../chunks/events-6Z5MkCRr.esm.js";import"../utils/logger.js";import"../chunks/lodash-P8OIs-at.esm.js";import"../chunks/progress-bar-PMU_xI3L.esm.js";import"../chunks/promo-pill-label-P1X4xeAx.esm.js";import"../chunks/vendor-GKiOHg2N.esm.js";import"./beam-partner-logos.js";import"../chunks/loading-template-t4fFsYtr.esm.js";import"../chunks/css-card-grid-nEc--M5M.esm.js";import"../chunks/shoelace-components-wBxNHnyr.esm.js";import"../chunks/share-button-diP8wKOc.esm.js";import"../chunks/is-all-html-elements-SdSkxirh.esm.js";import"../chunks/share-button-SNvuFDC9.esm.js";import"../chunks/responsive-2lC2Wrtg.esm.js";var A=Object.defineProperty,B=Object.getOwnPropertyDescriptor,e=(d,r,i,n)=>{for(var a=n>1?void 0:n?B(r,i):r,p=d.length-1,c;p>=0;p--)(c=d[p])&&(a=(n?c(r,i,a):c(a))||a);return n&&a&&A(r,i,a),a};const m=["baseUrl","apiKey","orderId","cartTotal","currencyCode","lang"],D=["storeId","postalCode","countryCode","cart","discountCodes","email","partnerUserId"];class t extends I{constructor(){super(...arguments),this.baseUrl=U,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.draftConfig=!1,this.localStorage=v(this),this.handlePostTransaction=async()=>{this.enforceConfig();const r=await w({baseUrl:this.baseUrl,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,discountCodes:this.discountCodes}});return this.transactionId=r.transactionId,this.userId=r.beamUserId,this.nonprofitId=r.nonprofitId,this.activeWidget=this.nonprofitId?"impact-overview":"redeem-transaction",this.localStorage.removeItem("transaction"),this.localStorage.removeItem("cart"),g({name:h,path:"/",domain:this.domain}),r.transactionCreated&&dispatchEvent(new T({transactionId:r.transactionId,orderId:this.orderId,cartTotal:this.cartTotal,currencyCode:this.currencyCode})),r},this.transactionDataController=new $(this,this.handlePostTransaction)}firstUpdated(r){this.enforceConfig(),this.nonprofitId=Number(this.localStorage.getItem("nonprofit"))||null,this.selectionId=this.localStorage.getItem("transaction")||null,this.cartId=this.localStorage.getItemJson("cart")?.cartId||null,this.beamCartId=u(h)||null,this.selectionId&&this.nonprofitId&&(this.activeWidget="impact-overview")}async updated(r){[...m,...D].some(i=>r.has(i))&&await this.transactionDataController.exec()}enforceConfig(){if(b(m,this),!this.email&&!this.partnerUserId)throw new y([this.email?"":"email",this.partnerUserId?"":"partnerUserId"].filter(Boolean),"At least one of [email, partnerUserId] must be provided")}render(){return this.transactionDataController.loading?"":this.transactionDataController.error?this.debug?S({error:this.transactionDataController.error}):"":this.activeWidget==="redeem-transaction"?l` <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}" ?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"?l` <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=[f],e([o({type:String})],t.prototype,"baseUrl",2),e([o({type:String})],t.prototype,"apiKey",2),e([o({type:Number,reflect:!0})],t.prototype,"storeId",2),e([o({type:String})],t.prototype,"postalCode",2),e([o({type:String})],t.prototype,"countryCode",2),e([o({type:String})],t.prototype,"orderId",2),e([o({type:String})],t.prototype,"email",2),e([o({type:String})],t.prototype,"partnerUserId",2),e([o({type:Number})],t.prototype,"cartTotal",2),e([o({type:String})],t.prototype,"currencyCode",2),e([o({type:String})],t.prototype,"lang",2),e([o({type:Object})],t.prototype,"cart",2),e([o({type:Array})],t.prototype,"discountCodes",2),e([o({type:String})],t.prototype,"domain",2),e([o({type:Boolean})],t.prototype,"debug",2),e([s()],t.prototype,"activeWidget",2),e([s()],t.prototype,"nonprofitId",2),e([s()],t.prototype,"selectionId",2),e([s()],t.prototype,"cartId",2),e([s()],t.prototype,"beamCartId",2),e([s()],t.prototype,"userId",2),e([s()],t.prototype,"transactionId",2),e([o({type:Boolean})],t.prototype,"draftConfig",2),C(t);export{t as BeamPostPurchase}; //# sourceMappingURL=post-purchase.js.map