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

3 lines (2 loc) 5.28 kB
import{i as O}from"./lodash-P8OIs-at.esm.js";import{c as D,b as R,g as k,f as P,d as x}from"./events-6Z5MkCRr.esm.js";import{logger as I}from"../utils/logger.esm.js";import{initNetworkListeners as K}from"../utils/network-listeners.esm.js";import{createScopedLocalStorage as J}from"../utils/local-storage.esm.js";import{memoizeLast as j}from"../utils/memoize-last.esm.js";import{B as g}from"./cart-contents-h60geKWa.esm.js";import{J as v}from"./update-cart-FwtFnnGe.esm.js";import{W as F,D as G}from"./routes-Dmf2ZNnZ.esm.js";import{getExternalCartId as E,getBeamCartId as h}from"../utils/cart.esm.js";import"../components/post-purchase.esm.js";import{waitForElement as M}from"../utils/wait-for-element.esm.js";import{B as W}from"./beam-errors-P-Lu07Ce.esm.js";import{getCookieValue as q}from"../utils/cookies.esm.js";const S=window.Shopify?.routes?.root||"/";let A=!1;const N="cart";async function z(e){if(A)return!1;K(),window.addEventListener(D.eventName,async r=>{const t=r,n=/cart\/(add|change|update|clear)/,a=t.detail.type==="xhr"?t.detail.xhr.responseURL:t.detail.response.url,c=new URL(a);if(!n.test(c.pathname))return;const o=await L(e);o.changed&&v(e,o.cart)});let s,d;window.addEventListener(R.eventName,async r=>{const t=r;I.debug("[cart-page][BeamNonprofitSelectEvent]",t.detail);const{source:n,selectedNonprofitId:a,selectionId:c}=t.detail;if(n===F.select_nonprofit){const o=E(N,e),l=h(g,e),{chainId:p,storeId:u}=e;if(s===a&&d===c)return;await y({selectedNonprofitId:a,selectionId:c,beamCartId:l||void 0,cartId:o||void 0,chainId:p,storeId:u}),d=c,s=a}}),window.addEventListener(k.eventName,async r=>{const t=r;I.debug("[cart-page][BeamNonprofitSelectionRemovedEvent]",t.detail);const{newNonprofitId:n,selectionId:a}=t.detail,c=E(N,e),o=h(g,e),{chainId:l,storeId:p}=e;s===n&&d===a||(await y({selectedNonprofitId:n,beamCartId:o||void 0,selectionId:a,cartId:c||void 0,chainId:l,storeId:p}),d=a,s=n)}),window.addEventListener(P.eventName,r=>{const t=r;I.debug("[cart-page][BeamCartCreatedEvent]",t.detail),B(e)}),window.addEventListener(x.eventName,async r=>{const t=r;I.debug("[cart-page][BeamCartChangeEvent]",t.detail),B(e)});const m=await L(e);return await v(e,m.cart,!0),A=!0,!0}async function B(e){let s,d;try{if(window.statsig){const m=window.statsig;s=m.getLayer("beam_trial_layer").get("show_beam",!1),d=m.getStableID();const r=E(N,e),t=h(g,e),{chainId:n,storeId:a}=e;y({showBeam:s,remoteSessionId:d,beamCartId:t||void 0,cartId:r||void 0,chainId:n,storeId:a})}}catch(m){I.error(m)}}async function L(e){const{token:s,total_price:d,item_count:m,currency:r,items:t,cart_level_discount_applications:n}=await T();let a=!1;const c=J(e).getItemJson("cart"),o=h(g,e),l={cartId:s,beamCartId:o,subtotal:d/100,itemCount:m,currencyCode:r,schema:{source:"generic"},content:{items:t.map(({sku:p,final_line_price:u,product_id:b,line_level_discount_allocations:C})=>{const f=p||String(b),w=u/100,i=C?.map(({discount_application:_})=>({code:_?.title,applicable:!0}));return{remoteProductIdentifier:f,localAmount:w,discounts:i}}),discounts:n.map(({title:p})=>({code:p,applicable:!0}))}};return O(l,{...c,beamCartId:c?.beamCartId})||(a=!0),{changed:a,cart:l}}const H=v,T=async function(){return await window.fetch(S+"cart.js",{method:"GET",headers:{"Content-Type":"application/json"}}).then(e=>e.json())},y=j(async function({selectedNonprofitId:e,selectionId:s,beamCartId:d,cartId:m,chainId:r,storeId:t,showBeam:n,remoteSessionId:a}){const{attributes:c}=await T(),o=c?.beam;let l={};try{o&&(l=JSON.parse(o))}catch(u){I.error(u)}const p={beam:JSON.stringify({...l,nonprofit_id:e,selection_id:s,beam_cart_id:d,shopify_cart_id:m,chain_id:r,store_id:t,show_beam:n,remote_session_id:a})};if(!O(p.beam,l))try{await window.fetch(S+"cart/update.js",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({attributes:p})})}catch(u){I.error(u)}}),V=j(async function({selectedNonprofitId:e,selectionId:s,beamCartId:d,cartId:m,chainId:r,storeId:t}){let n,a;try{if(window.statsig){const o=window.statsig;n=o.getLayer("beam_trial_layer").get("show_beam",!1),a=o.getStableID()}}catch(o){I.error(o)}const c={beam:JSON.stringify({nonprofit_id:e,selection_id:s,beam_cart_id:d,shopify_cart_id:m,chain_id:r,store_id:t,show_beam:n,remote_session_id:a})};try{await window.fetch(S+"cart/update.js",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({attributes:c})})}catch(o){I.error(o)}});async function $({parentSelector:e=".step__sections",apiKey:s,baseUrl:d=G,storeId:m,postalCode:r,countryCode:t,orderId:n,email:a,cartTotal:c,cart:o,discountCodes:l,currencyCode:p="USD",domain:u,lang:b="en",debug:C=!1}){const f=document.querySelector(e)||await M(e);if(!f)throw new W(`Timed out waiting for selector '${e}'. Could not render Beam post-purchase widget.`);const w=await q("cart");if(w){const _=J({apiKey:s}),U=_.getItemJson("cart")||{};_.setItemJson("cart",{...U,cartId:w})}const i=document.createElement("beam-post-purchase");i.apiKey=s,i.baseUrl=d,i.storeId=m,i.postalCode=r,i.countryCode=t,i.orderId=String(n),i.email=a,i.cartTotal=c,i.cart=o,i.discountCodes=l,i.currencyCode=p,i.domain=u,i.lang=b,i.debug=C,f.appendChild(i)}export{z as F,H as G,V as H,B as S,L as _,y as g,$ as s,T as v}; //# sourceMappingURL=order-page-rrfJ4w9F.esm.js.map