@ikas/storefront 6.1.0-beta.341 → 6.1.0-beta.343

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- import{__awaiter as t,__generator as e,__assign as n}from'./../../../ext/tslib/tslib.es6.js';import*as o from"react";import{reaction as r}from"mobx";import{listStorefrontPopup as i,SortByDirectionEnum as a}from"@ikas/storefront-api";import{IkasStorefrontConfig as u}from"@ikas/storefront-config";import{Analytics as s}from"../../../analytics/analytics.js";import"../../../analytics/head/index.js";import{IkasBaseStore as c}from"../../../store/base.js";import"../../../store/customer/index.js";import"../../../store/cart/index.js";import d from"../../../store/product/index.js";import{POPUP_CONTEXT_UPDATE_EVENT as l}from'./../../../ext/@ikas/popup-script-injector/dist/popup-script-injector.es.js';import{resolveStoreUrl as p,resolveCartUrl as f}from"./common.js";var m=["scroll","keydown","pointerdown","touchstart","mousemove"],v=function(n){return t(void 0,void 0,void 0,(function(){var o,r,i,a,l,m,v,I,S,y,P,C,k,b,E,L;return e(this,(function(j){switch(j.label){case 0:return o={popups:n,cartUrl:""},[4,function(){return new Promise((function(t){var e=setInterval((function(){var n=u.getSessionId();n&&(clearInterval(e),t(n))}),100)}))}()];case 1:return j.sent(),(r=u.getSessionId())&&(o.sessionId=r),(i=u.getCdnUrl())&&(o.cdnUrl=i),(a=null===(b=u.getMerchantSettings())||void 0===b?void 0:b.merchantId)&&(o.merchantId=a),(l=p())&&(o.storeUrl=l),(m=f())&&(o.cartUrl=m),(v=u.getCustomerToken())&&(o.customerToken=v),(I=u.getCurrentLocale())&&(o.locale=I),(S=null===(L=null===(E=u.getCurrentRouting())||void 0===E?void 0:E.countryCodes)||void 0===L?void 0:L[0])&&(o.countryCode=S),(y=u.getPriceListId())&&(o.priceListId=y),(P=u.getSalesChannelId())&&(o.salesChannelId=P),D=c.getInstance().customerStore.customer,(C=D?h(D):null)&&(o.customer=C),T=c.getInstance().cartStore.cart,(k=T?w(T):null)&&(o.cart=k),o.services=function(){var n=this,o=c.getInstance();return{searchProducts:function(o){return t(n,void 0,void 0,(function(){var t,n,r;return e(this,(function(e){switch(e.label){case 0:return[4,d.searchProducts(g(o))];case 1:return(null==(t=e.sent())?void 0:t.isSuccess)&&(null===(r=null===(n=t.data)||void 0===n?void 0:n.data)||void 0===r?void 0:r.length)?[2,t.data.data]:[2,[]]}}))}))},addItemToCart:function(r){var i=r.product,a=r.variant,u=r.quantity;return t(n,void 0,void 0,(function(){var t;return e(this,(function(e){switch(e.label){case 0:return[4,o.cartStore.addItem(a,i,null!=u?u:1)];case 1:return[2,{success:!!(null==(t=e.sent())?void 0:t.success)}]}}))}))},saveCustomerFormData:function(r){return t(n,void 0,void 0,(function(){return e(this,(function(t){switch(t.label){case 0:return[4,o.customerStore.saveCustomerFormData({email:r.email,firstName:r.firstName,lastName:r.lastName,birthDate:r.birthDate||null,phone:r.phone||"",storefrontPopupId:r.popupId,subscriptions:r.subscriptions})];case 1:return t.sent(),[2]}}))}))},getLastViewedProducts:function(){return t(n,void 0,void 0,(function(){var t;return e(this,(function(e){return"function"==typeof(t=o.customerStore).getLastViewedProducts?[2,t.getLastViewedProducts()]:[2,[]]}))}))},formatVariantSellPrice:function(t){return t.formattedSellPrice||""},formatVariantDiscountPrice:function(t){return t.formattedDiscountPrice||null},hasVariantDiscount:function(t){return!!t.hasDiscount},getVariantDiscountPercentage:function(t){var e=t.discountPercentage;return null==e?null:"number"==typeof e?e.toString():e},redirectToProduct:function(t){return!!t.url&&(window.location.href=t.url,!0)},viewPopup:function(t){s.viewStorefrontPopup(t)}}}(),[2,o]}var T,D}))}))};function g(t){var e,n,o,r,i,s=null===(e=t.order)||void 0===e?void 0:e.map((function(t){return{direction:"ASC"===t.direction?a.ASC:a.DESC,type:t.type}}));return{input:{page:null!==(n=t.page)&&void 0!==n?n:1,perPage:null!==(o=t.perPage)&&void 0!==o?o:10,order:s,productIdList:t.productIdList,categoryIdList:t.categoryIdList,priceListId:null!==(r=t.priceListId)&&void 0!==r?r:u.getPriceListId(),salesChannelId:null!==(i=t.salesChannelId)&&void 0!==i?i:u.getSalesChannelId()}}}function h(t){return{email:t.email,firstName:t.firstName,lastName:t.lastName,birthDate:t.birthDate,phone:t.phone,gender:t.gender?String(t.gender):null,customerGroupIds:t.customerGroupIds||[],customerSegmentIds:t.customerSegmentIds||[],subscriptionStatus:t.subscriptionStatus?String(t.subscriptionStatus):null}}function w(t){var e,n,o=[],r=new Set,i=new Set,a=new Set,u=new Set,s=new Set,c=new Set;return t.items.forEach((function(t){var e,n,s,d=t.variant,l=(null==d?void 0:d.productId)||null,p=(null==d?void 0:d.id)||null,f=(null===(e=null==d?void 0:d.brand)||void 0===e?void 0:e.id)||null,m=(null===(n=null==d?void 0:d.categories)||void 0===n?void 0:n.map((function(t){return t.id})).filter(Boolean))||[];o.push({productId:l,variantId:p,brandId:f,categoryIds:m,quantity:t.quantity,finalPrice:t.finalPrice}),l&&r.add(l),p&&i.add(p),f&&a.add(f),m.forEach((function(t){return t&&u.add(t)})),(null===(s=t.discount)||void 0===s?void 0:s.campaignOfferId)&&c.add(t.discount.campaignOfferId)})),null===(e=t.campaignOffers)||void 0===e||e.forEach((function(t){t.campaignOfferId&&c.add(t.campaignOfferId)})),null===(n=t.orderAdjustments)||void 0===n||n.forEach((function(t){t.campaignId&&s.add(t.campaignId)})),{totalPrice:t.totalPrice,totalFinalPrice:t.totalFinalPrice,itemCount:t.itemCount,itemQuantity:t.itemQuantity,currencyCode:t.currencyCode||null,lines:o,productIds:Array.from(r),variantIds:Array.from(i),brandIds:Array.from(a),categoryIds:Array.from(u),appliedCampaignIds:Array.from(s),appliedCampaignOfferIds:Array.from(c),couponCode:t.couponCode||null}}function I(t){"undefined"!=typeof window&&(window.ikasPopupConfig&&(void 0!==t.customer&&(window.ikasPopupConfig.customer=t.customer),void 0!==t.cart&&(window.ikasPopupConfig.cart=t.cart),void 0!==t.customerToken&&(window.ikasPopupConfig.customerToken=t.customerToken||void 0)),"function"!=typeof window.updateIkasPopupContext?window.dispatchEvent(new CustomEvent(l,{detail:t})):window.updateIkasPopupContext(t))}function S(a){var s=this;a.router;var d=a.pageSpecificDataId,l=a.pageType;o.useEffect((function(){if("undefined"!=typeof window&&"undefined"!=typeof document){var o,a,d=!1,l=[],p=function(t,e){if(function(t){t.setAttribute("data-ikas-popup-widget","true"),!t.dataset.ikasPopupWidgetUrl&&t.src&&(t.dataset.ikasPopupWidgetUrl=t.src)}(t),"true"===t.dataset.ikasPopupWidgetLoaded)return e(),function(){};var n=function(){t.dataset.ikasPopupWidgetLoaded="true",e()},o=function(e){console.error("[@ikas/storefront] Failed to load popup widget script",t.src,e)};return t.addEventListener("load",n,{once:!0}),t.addEventListener("error",o,{once:!0}),function(){t.removeEventListener("load",n),t.removeEventListener("error",o)}},f=function(t){var e=window.location.href.includes("http://localhost:3333/")?"http://localhost:9000/popup-script-injector.iife.js":"/sf/assets/popup/popup-script-injector.iife.js";if(e){var n=function(){d||function(t){if("function"==typeof window.startIkasPopupWidget)try{window.startIkasPopupWidget(t)}catch(t){console.error("[@ikas/storefront] Failed to start popup widget",t)}}(t)},r=document.querySelector('script[data-ikas-popup-widget="true"]');if(r)o=p(r,n);else{var i=document.createElement("script");i.src=e,i.defer=!0,o=p(i,n),document.body.appendChild(i)}}},g=function(t){var e=window.location.href.includes("http://localhost:3333/")?"http://localhost:9000/style.css":"/sf/assets/popup/style.css";if(e&&!document.querySelector('link[href="'.concat(e,'"]'))){var n=document.createElement("link");n.rel="stylesheet",n.href=e,n.setAttribute("data-ikas-popup-widget","true");n.addEventListener("error",(function(t){console.error("[@ikas/storefront] Failed to load popup widget CSS",e,t)}),{once:!0}),document.head.appendChild(n)}};return t(s,void 0,void 0,(function(){var o,p,S;return e(this,(function(y){switch(y.label){case 0:return(o=u.getStorefrontPopups()||[]).length?[4,(F=o,t(s,void 0,void 0,(function(){var t,o,r,a,u;return e(this,(function(e){switch(e.label){case 0:if(!(t=F.map((function(t){return t.id})).filter(Boolean)).length)return[2,F];e.label=1;case 1:return e.trys.push([1,3,,4]),[4,i({idList:t})];case 2:return(null==(o=e.sent())?void 0:o.isSuccess)&&(null===(u=o.data)||void 0===u?void 0:u.length)?(r=o.data,[2,F.map((function(t){var e=r.find((function(e){return e.id===t.id}));return e?n(n({},e),{isFilteredOnServerSide:t.isFilteredOnServerSide}):t}))]):[3,4];case 3:return a=e.sent(),console.error("[@ikas/storefront] Failed to fetch popup details",a),[3,4];case 4:return[2,F]}}))})))]:[2];case 1:return p=y.sent(),d?[2]:[4,v(p)];case 2:return S=y.sent(),window.ikasPopupConfig=S,b=function(){d||(f(S),g())},j=window,T=!1,D=function(){m.forEach((function(t){return window.removeEventListener(t,A)})),void 0!==E&&"function"==typeof j.cancelIdleCallback&&j.cancelIdleCallback(E),void 0!==L&&window.clearTimeout(L)},A=function(){T||(T=!0,D(),b())},m.forEach((function(t){return window.addEventListener(t,A,{once:!0,passive:!0})})),"function"==typeof j.requestIdleCallback?E=j.requestIdleCallback(A,{timeout:3e3}):L=window.setTimeout(A,1500),a=D,P=c.getInstance(),C=r((function(){var t=P.customerStore.customer;return t?{email:t.email,gender:t.gender,groupIds:(t.customerGroupIds||[]).join(","),segmentIds:(t.customerSegmentIds||[]).join(","),subscriptionStatus:t.subscriptionStatus}:null}),(function(){var t=P.customerStore.customer;I({customer:t?h(t):null,customerToken:u.getCustomerToken()||null})})),k=r((function(){var t=P.cartStore.cart;return t?{total:t.totalFinalPrice,itemCount:t.itemCount,quantity:t.itemQuantity,productSignature:t.items.map((function(t){var e,n;return"".concat((null===(e=t.variant)||void 0===e?void 0:e.productId)||"",":").concat((null===(n=t.variant)||void 0===n?void 0:n.id)||"",":").concat(t.quantity)})).join("|"),campaignSignature:(t.orderAdjustments||[]).map((function(t){return t.campaignId||""})).join("|"),couponCode:t.couponCode}:null}),(function(){var t=P.cartStore.cart;I({cart:t?w(t):null})})),l.push(C,k),[2]}var P,C,k,b,E,L,j,T,D,A,F}))})),function(){d=!0,null==o||o(),null==a||a(),l.forEach((function(t){return t()})),l=[],"function"==typeof window.destroyIkasPopupWidget&&window.destroyIkasPopupWidget()}}}),[d,l])}export{S as usePopupWidget};
1
+ import{__awaiter as t,__generator as e,__assign as n}from'./../../../ext/tslib/tslib.es6.js';import*as o from"react";import{reaction as r}from"mobx";import{listStorefrontPopup as i,SortByDirectionEnum as a}from"@ikas/storefront-api";import{IkasStorefrontConfig as u}from"@ikas/storefront-config";import{Analytics as s}from"../../../analytics/analytics.js";import"../../../analytics/head/index.js";import{IkasBaseStore as c}from"../../../store/base.js";import"../../../store/customer/index.js";import"../../../store/cart/index.js";import d from"../../../store/product/index.js";import{resolveStoreUrl as l,resolveCartUrl as p}from"./common.js";var f=["scroll","keydown","pointerdown","touchstart","mousemove"],m=function(n){return t(void 0,void 0,void 0,(function(){var o,r,i,a,f,m,w,I,S,y,C,P,k,b,L,E;return e(this,(function(j){switch(j.label){case 0:return o={popups:n,cartUrl:""},[4,function(){return new Promise((function(t){var e=setInterval((function(){var n=u.getSessionId();n&&(clearInterval(e),t(n))}),100)}))}()];case 1:return j.sent(),(r=u.getSessionId())&&(o.sessionId=r),(i=u.getCdnUrl())&&(o.cdnUrl=i),(a=null===(b=u.getMerchantSettings())||void 0===b?void 0:b.merchantId)&&(o.merchantId=a),(f=l())&&(o.storeUrl=f),(m=p())&&(o.cartUrl=m),(w=u.getCustomerToken())&&(o.customerToken=w),(I=u.getCurrentLocale())&&(o.locale=I),(S=null===(E=null===(L=u.getCurrentRouting())||void 0===L?void 0:L.countryCodes)||void 0===E?void 0:E[0])&&(o.countryCode=S),(y=u.getPriceListId())&&(o.priceListId=y),(C=u.getSalesChannelId())&&(o.salesChannelId=C),A=c.getInstance().customerStore.customer,(P=A?g(A):null)&&(o.customer=P),D=c.getInstance().cartStore.cart,(k=D?h(D):null)&&(o.cart=k),o.services=function(){var n=this,o=c.getInstance();return{searchProducts:function(o){return t(n,void 0,void 0,(function(){var t,n,r;return e(this,(function(e){switch(e.label){case 0:return[4,d.searchProducts(v(o))];case 1:return(null==(t=e.sent())?void 0:t.isSuccess)&&(null===(r=null===(n=t.data)||void 0===n?void 0:n.data)||void 0===r?void 0:r.length)?[2,t.data.data]:[2,[]]}}))}))},addItemToCart:function(r){var i=r.product,a=r.variant,u=r.quantity;return t(n,void 0,void 0,(function(){var t;return e(this,(function(e){switch(e.label){case 0:return[4,o.cartStore.addItem(a,i,null!=u?u:1)];case 1:return[2,{success:!!(null==(t=e.sent())?void 0:t.success)}]}}))}))},saveCustomerFormData:function(r){return t(n,void 0,void 0,(function(){return e(this,(function(t){switch(t.label){case 0:return[4,o.customerStore.saveCustomerFormData({email:r.email,firstName:r.firstName,lastName:r.lastName,birthDate:r.birthDate||null,phone:r.phone||"",storefrontPopupId:r.popupId,subscriptions:r.subscriptions})];case 1:return t.sent(),[2]}}))}))},getLastViewedProducts:function(){return t(n,void 0,void 0,(function(){var t;return e(this,(function(e){return"function"==typeof(t=o.customerStore).getLastViewedProducts?[2,t.getLastViewedProducts()]:[2,[]]}))}))},formatVariantSellPrice:function(t){return t.formattedSellPrice||""},formatVariantDiscountPrice:function(t){return t.formattedDiscountPrice||null},hasVariantDiscount:function(t){return!!t.hasDiscount},getVariantDiscountPercentage:function(t){var e=t.discountPercentage;return null==e?null:"number"==typeof e?e.toString():e},redirectToProduct:function(t){return!!t.url&&(window.location.href=t.url,!0)},viewPopup:function(t){s.viewStorefrontPopup(t)}}}(),[2,o]}var D,A}))}))};function v(t){var e,n,o,r,i,s=null===(e=t.order)||void 0===e?void 0:e.map((function(t){return{direction:"ASC"===t.direction?a.ASC:a.DESC,type:t.type}}));return{input:{page:null!==(n=t.page)&&void 0!==n?n:1,perPage:null!==(o=t.perPage)&&void 0!==o?o:10,order:s,productIdList:t.productIdList,categoryIdList:t.categoryIdList,priceListId:null!==(r=t.priceListId)&&void 0!==r?r:u.getPriceListId(),salesChannelId:null!==(i=t.salesChannelId)&&void 0!==i?i:u.getSalesChannelId()}}}function g(t){return{email:t.email,firstName:t.firstName,lastName:t.lastName,birthDate:t.birthDate,phone:t.phone,gender:t.gender?String(t.gender):null,customerGroupIds:t.customerGroupIds||[],customerSegmentIds:t.customerSegmentIds||[],subscriptionStatus:t.subscriptionStatus?String(t.subscriptionStatus):null}}function h(t){var e,n,o=[],r=new Set,i=new Set,a=new Set,u=new Set,s=new Set,c=new Set;return t.items.forEach((function(t){var e,n,s,d=t.variant,l=(null==d?void 0:d.productId)||null,p=(null==d?void 0:d.id)||null,f=(null===(e=null==d?void 0:d.brand)||void 0===e?void 0:e.id)||null,m=(null===(n=null==d?void 0:d.categories)||void 0===n?void 0:n.map((function(t){return t.id})).filter(Boolean))||[];o.push({productId:l,variantId:p,brandId:f,categoryIds:m,quantity:t.quantity,finalPrice:t.finalPrice}),l&&r.add(l),p&&i.add(p),f&&a.add(f),m.forEach((function(t){return t&&u.add(t)})),(null===(s=t.discount)||void 0===s?void 0:s.campaignOfferId)&&c.add(t.discount.campaignOfferId)})),null===(e=t.campaignOffers)||void 0===e||e.forEach((function(t){t.campaignOfferId&&c.add(t.campaignOfferId)})),null===(n=t.orderAdjustments)||void 0===n||n.forEach((function(t){t.campaignId&&s.add(t.campaignId)})),{totalPrice:t.totalPrice,totalFinalPrice:t.totalFinalPrice,itemCount:t.itemCount,itemQuantity:t.itemQuantity,currencyCode:t.currencyCode||null,lines:o,productIds:Array.from(r),variantIds:Array.from(i),brandIds:Array.from(a),categoryIds:Array.from(u),appliedCampaignIds:Array.from(s),appliedCampaignOfferIds:Array.from(c),couponCode:t.couponCode||null}}function w(t){"undefined"!=typeof window&&(window.ikasPopupConfig&&(void 0!==t.customer&&(window.ikasPopupConfig.customer=t.customer),void 0!==t.cart&&(window.ikasPopupConfig.cart=t.cart),void 0!==t.customerToken&&(window.ikasPopupConfig.customerToken=t.customerToken||void 0)),"function"!=typeof window.updateIkasPopupContext?window.dispatchEvent(new CustomEvent("ikas:popup-context-update",{detail:t})):window.updateIkasPopupContext(t))}function I(a){var s=this;a.router;var d=a.pageSpecificDataId,l=a.pageType;o.useEffect((function(){if("undefined"!=typeof window&&"undefined"!=typeof document){var o,a,d=!1,l=[],p=function(t,e){if(function(t){t.setAttribute("data-ikas-popup-widget","true"),!t.dataset.ikasPopupWidgetUrl&&t.src&&(t.dataset.ikasPopupWidgetUrl=t.src)}(t),"true"===t.dataset.ikasPopupWidgetLoaded)return e(),function(){};var n=function(){t.dataset.ikasPopupWidgetLoaded="true",e()},o=function(e){console.error("[@ikas/storefront] Failed to load popup widget script",t.src,e)};return t.addEventListener("load",n,{once:!0}),t.addEventListener("error",o,{once:!0}),function(){t.removeEventListener("load",n),t.removeEventListener("error",o)}},v=function(t){var e=window.location.href.includes("http://localhost:3333/")?"http://localhost:9000/popup-script-injector.iife.js":"/sf/assets/popup/popup-script-injector.iife.js";if(e){var n=function(){d||function(t){if("function"==typeof window.startIkasPopupWidget)try{window.startIkasPopupWidget(t)}catch(t){console.error("[@ikas/storefront] Failed to start popup widget",t)}}(t)},r=document.querySelector('script[data-ikas-popup-widget="true"]');if(r)o=p(r,n);else{var i=document.createElement("script");i.src=e,i.defer=!0,o=p(i,n),document.body.appendChild(i)}}},I=function(t){var e=window.location.href.includes("http://localhost:3333/")?"http://localhost:9000/style.css":"/sf/assets/popup/style.css";if(e&&!document.querySelector('link[href="'.concat(e,'"]'))){var n=document.createElement("link");n.rel="stylesheet",n.href=e,n.setAttribute("data-ikas-popup-widget","true");n.addEventListener("error",(function(t){console.error("[@ikas/storefront] Failed to load popup widget CSS",e,t)}),{once:!0}),document.head.appendChild(n)}};return t(s,void 0,void 0,(function(){var o,p,S;return e(this,(function(y){switch(y.label){case 0:return(o=u.getStorefrontPopups()||[]).length?[4,(F=o,t(s,void 0,void 0,(function(){var t,o,r,a,u;return e(this,(function(e){switch(e.label){case 0:if(!(t=F.map((function(t){return t.id})).filter(Boolean)).length)return[2,F];e.label=1;case 1:return e.trys.push([1,3,,4]),[4,i({idList:t})];case 2:return(null==(o=e.sent())?void 0:o.isSuccess)&&(null===(u=o.data)||void 0===u?void 0:u.length)?(r=o.data,[2,F.map((function(t){var e=r.find((function(e){return e.id===t.id}));return e?n(n({},e),{isFilteredOnServerSide:t.isFilteredOnServerSide}):t}))]):[3,4];case 3:return a=e.sent(),console.error("[@ikas/storefront] Failed to fetch popup details",a),[3,4];case 4:return[2,F]}}))})))]:[2];case 1:return p=y.sent(),d?[2]:[4,m(p)];case 2:return S=y.sent(),window.ikasPopupConfig=S,b=function(){d||(v(S),I())},j=window,D=!1,A=function(){f.forEach((function(t){return window.removeEventListener(t,T)})),void 0!==L&&"function"==typeof j.cancelIdleCallback&&j.cancelIdleCallback(L),void 0!==E&&window.clearTimeout(E)},T=function(){D||(D=!0,A(),b())},f.forEach((function(t){return window.addEventListener(t,T,{once:!0,passive:!0})})),"function"==typeof j.requestIdleCallback?L=j.requestIdleCallback(T,{timeout:3e3}):E=window.setTimeout(T,1500),a=A,C=c.getInstance(),P=r((function(){var t=C.customerStore.customer;return t?{email:t.email,gender:t.gender,groupIds:(t.customerGroupIds||[]).join(","),segmentIds:(t.customerSegmentIds||[]).join(","),subscriptionStatus:t.subscriptionStatus}:null}),(function(){var t=C.customerStore.customer;w({customer:t?g(t):null,customerToken:u.getCustomerToken()||null})})),k=r((function(){var t=C.cartStore.cart;return t?{total:t.totalFinalPrice,itemCount:t.itemCount,quantity:t.itemQuantity,productSignature:t.items.map((function(t){var e,n;return"".concat((null===(e=t.variant)||void 0===e?void 0:e.productId)||"",":").concat((null===(n=t.variant)||void 0===n?void 0:n.id)||"",":").concat(t.quantity)})).join("|"),campaignSignature:(t.orderAdjustments||[]).map((function(t){return t.campaignId||""})).join("|"),couponCode:t.couponCode}:null}),(function(){var t=C.cartStore.cart;w({cart:t?h(t):null})})),l.push(P,k),[2]}var C,P,k,b,L,E,j,D,A,T,F}))})),function(){d=!0,null==o||o(),null==a||a(),l.forEach((function(t){return t()})),l=[],"function"==typeof window.destroyIkasPopupWidget&&window.destroyIkasPopupWidget()}}}),[d,l])}export{I as usePopupWidget};
@@ -46,6 +46,7 @@ export declare class IkasCartStore {
46
46
  private beforeCartOperationCheck;
47
47
  private createCartLineOptions;
48
48
  private getOptionValues;
49
+ private checkBundleProductsStock;
49
50
  private _windowAddToCart;
50
51
  }
51
52
  export declare type IkasCartOperationResult = {
@@ -1 +1 @@
1
- import{__awaiter as t,__generator as e,__assign as i}from'./../../ext/tslib/tslib.es6.js';import{makeAutoObservable as r}from"mobx";import n from"./api.js";import"../../models/data/blog/category/index.js";import"../../models/data/blog/content/index.js";import"../../models/data/blog/meta-data/index.js";import"../../models/data/blog/tag/index.js";import"../../models/data/blog/index.js";import"../../models/data/brand/index.js";import{IkasProductOptionType as a,IkasCustomerEmailSubscriptionStatus as o}from"@ikas/storefront-models";import"../../models/data/campaign-offer/index.js";import"../../models/data/cart/campaign-offer/index.js";import"../../models/data/cart/index.js";import"../../models/data/category/path-item/index.js";import"../../models/data/category/index.js";import"../../models/data/checkout/index.js";import"../../models/data/checkout-settings/index.js";import"../../models/data/city/index.js";import"../../models/data/country/index.js";import"../../models/data/customer/address/region/index.js";import"../../models/data/customer/address/index.js";import"../../models/data/customer/address/ikas-localized-customer-address.js";import"../../models/data/customer/attribute/index.js";import"../../models/data/customer/review/summary/index.js";import"../../models/data/customer/review/index.js";import"../../models/data/customer/index.js";import"../../models/data/customer-form-data-input/index.js";import"../../models/data/district/index.js";import"../../models/data/favorite-product/index.js";import"../../models/data/filter-category/index.js";import"../../models/data/html-meta-data/index.js";import"../../models/data/image/index.js";import"../../models/data/order/address/region/index.js";import"../../models/data/order/address/index.js";import"../../models/data/order/line-item/option/value/index.js";import"../../models/data/order/line-item/variant/value/index.js";import"../../models/data/order/line-item/variant/index.js";import"../../models/data/order/line-item/index.js";import"../../models/data/order/order-transaction-with-prices/index.js";import"../../models/data/order/package/index.js";import"../../models/data/order/transaction/index.js";import"../../models/data/order/index.js";import"../../models/data/payment-gateway/index.js";import"../../models/data/product/attribute-value/index.js";import"../../models/data/product/filter/index.js";import"../../models/data/product/campaign-offer/index.js";import"../../models/data/product/option-set/option/index.js";import"../../models/data/product/option-set/index.js";import"../../models/data/product/stock-location/index.js";import"../../models/data/product/variant/price/index.js";import"../../models/data/product/variant/index.js";import"../../models/data/variant-type/index.js";import"../../models/data/product/index.js";import"../../models/data/raffle/index.js";import"../../models/data/shipping-zone/state/city/district/region/index.js";import"../../models/data/state/index.js";import"../../models/data/storefront-popup/storefront-popup-display-settings/index.js";import"../../models/data/storefront-popup/storefront-popup-page-filter/index.js";import"../../models/data/storefront-popup/index.js";import"../../models/data/theme-json/index.js";import"../../models/data/theme-json/component/index.js";import"../../models/data/theme-json/custom-data/index.js";import"../../models/data/variant-type/variant-value/index.js";import"@ikas/localized-address";import{IkasStorefrontConfig as s}from"@ikas/storefront-config";import{Analytics as d}from"../../analytics/analytics.js";import"../../analytics/head/index.js";import{SaveCartInput as c,createPayWithIkasSessionUrl as u}from"@ikas/storefront-api";import{getCookieValue as l}from"../../utils/helper.js";var p,m="cartId",v=function(){function m(u){var m=this;this.cart=null,this._isLoadingCart=!1,this._isCartLoadFinished=!1,this.addItem=function(r,a,o,c){return void 0===o&&(o=1),t(m,void 0,void 0,(function(){var t,u,m,v,f,h,I,g,C,b,S,y,j,x,O,P,_,T,L,E;return e(this,(function(e){switch(e.label){case 0:t=null!==(y=null==c?void 0:c.isPayWithIkas)&&void 0!==y&&y,e.label=1;case 1:return e.trys.push([1,7,,8]),!t&&a.productOptionSetId&&!a.productOptionSet&&this.baseStore.router?(this.baseStore.router.push(a.productHref),[2,{success:!1,validationError:p.INVALID_PRODUCT_OPTION_VALUES}]):a.hasValidProductOptionValues?r.hasStock||r.sellIfOutOfStock?(u=void 0,t?[3,3]:[4,this.beforeCartOperationCheck()]):[2,{success:!1,validationError:p.INSUFFICIENT_STOCK}]:[2,{success:!1,validationError:p.INVALID_PRODUCT_OPTION_VALUES}];case 2:if(e.sent(),this.cart&&(u=this.findExistingItem(r,a))){if(null===(j=a.productOptionSet)||void 0===j||j.initOptionValues(),!r.bundleSettings)return[2,this.changeItemQuantity(u,u.quantity+1,a.offers,a)];if(!this.getEditLineID())return[2,this.changeItemQuantity(u,u.quantity+1,a.offers,a)]}e.label=3;case 3:return m=this.getOptionValues(a),v=o,(null==(f=null===(x=a.salesChannels)||void 0===x?void 0:x.find((function(t){return t.id===s.getSalesChannelId()})))?void 0:f.minQuantityPerCart)&&o<f.minQuantityPerCart&&(v=f.minQuantityPerCart),(null==f?void 0:f.maxQuantityPerCart)&&o>f.maxQuantityPerCart&&(v=f.maxQuantityPerCart),h=t?null:(null===(O=this.cart)||void 0===O?void 0:O.id)||localStorage.getItem("cartId"),[4,n.addItemToCart({input:i({cartId:h,item:{id:null!==(P=null==u?void 0:u.id)&&void 0!==P?P:null,quantity:null!==(_=null==u?void 0:u.quantity)&&void 0!==_?_:v,variant:{id:r.id,name:a.name,bundleProducts:null===(T=r.bundleSettings)||void 0===T?void 0:T.products.map((function(t){var e,i,r;return{id:t.id,productId:(null===(e=t.product)||void 0===e?void 0:e.id)||"",variantId:(null===(r=null===(i=t.product)||void 0===i?void 0:i.selectedVariant)||void 0===r?void 0:r.id)||"",quantity:t.quantity}}))}},acceptedOffers:this.getAcceptedOffers(a.offers),options:m.length?m:null,priceListId:s.getPriceListId()||null,salesChannelId:s.getSalesChannelId(),storefrontThemeId:s.getStorefrontThemeId(),metadata:{_fbp:l("_fbp"),_fbc:l("_fbc"),_ga:l("_ga")}},a.selectedVariant.subscriptionPlan&&a.selectedVariant.subscriptionPlan.isSelected?{subscriptionPlanId:a.selectedVariant.subscriptionPlan.subscriptionPlanId,subscriptionPlanPeriodId:null===(L=a.selectedVariant.subscriptionPlan.periods.find((function(t){return t.isSelected})))||void 0===L?void 0:L.id}:{})})];case 4:return I=e.sent(),t?[3,6]:I.isSuccess&&I.data?[4,this.setCart(I.data)]:[3,6];case 5:e.sent(),e.label=6;case 6:return(g=t?I.data:this.cart)&&(C="".concat(g.id,"-").concat(g.updatedAt),(b=g.items.find((function(t){return t.variant.id===r.id})))&&d.addToCart(b,o,C,g,a)),null===(E=a.productOptionSet)||void 0===E||E.initOptionValues(),[2,{success:I.isSuccess,response:I}];case 7:return S=e.sent(),console.log(S),[2,{success:!1}];case 8:return[2]}}))}))},this.changeItemQuantity=function(r,a,o,c){return t(m,void 0,void 0,(function(){var t,u,p,m,v,f,h,I,g;return e(this,(function(e){switch(e.label){case 0:return e.trys.push([0,5,,6]),[4,this.beforeCartOperationCheck()];case 1:return e.sent(),[4,n.addItemToCart({input:i({cartId:(null===(v=this.cart)||void 0===v?void 0:v.id)||null,item:{id:r.id,quantity:a,variant:{id:r.variant.id,name:r.variant.name,bundleProducts:null===(f=r.variant.bundleProducts)||void 0===f?void 0:f.map((function(t){return{id:t.id,productId:t.variant.productId||"",variantId:t.variant.id||"",quantity:t.quantity}}))}},acceptedOffers:this.getAcceptedOffers(o),options:this.createCartLineOptions(r),priceListId:s.getPriceListId()||null,salesChannelId:s.getSalesChannelId(),storefrontThemeId:s.getStorefrontThemeId(),metadata:{_fbp:l("_fbp"),_fbc:l("_fbc"),_ga:l("_ga")}},(null===(h=null==c?void 0:c.selectedVariant)||void 0===h?void 0:h.subscriptionPlan)&&(null===(I=null==c?void 0:c.selectedVariant)||void 0===I?void 0:I.subscriptionPlan.isSelected)?{subscriptionPlanId:null==c?void 0:c.selectedVariant.subscriptionPlan.subscriptionPlanId,subscriptionPlanPeriodId:null===(g=null==c?void 0:c.selectedVariant.subscriptionPlan.periods.find((function(t){return t.isSelected})))||void 0===g?void 0:g.id}:{})})];case 2:return(t=e.sent()).isSuccess&&t.data?[4,this.setCart(t.data)]:[3,4];case 3:e.sent(),e.label=4;case 4:return this.cart&&(u="".concat(this.cart.id,"-").concat(this.cart.updatedAt),(p=r.quantity)>a?d.removeFromCart(r,p-a,this.cart):d.addToCart(r,a-p,u,this.cart,c)),this.removeCardIfEmpty(),[2,{success:t.isSuccess,response:t}];case 5:return m=e.sent(),console.log(m),[2,{success:!1}];case 6:return[2]}}))}))},this.createCart=function(i){return t(m,void 0,void 0,(function(){var t,r=this;return e(this,(function(e){switch(e.label){case 0:return[4,n.createCart({input:{items:i.variantLines.map((function(t){return{variant:{id:t.variantId},quantity:t.quantity}})),couponCode:i.couponCode,salesChannelId:s.getSalesChannelId(),storefrontThemeId:s.getStorefrontThemeId()}})];case 1:return(t=e.sent()).isSuccess&&t.data?[4,this.setCart(t.data)]:[3,3];case 2:e.sent(),e.label=3;case 3:return i.variantLines.forEach((function(t){if(r.cart){var e="".concat(r.cart.id,"-").concat(r.cart.updatedAt),i=r.cart.items.find((function(e){return e.variant.id===t.variantId}));i&&d.addToCart(i,t.quantity,e,r.cart)}})),[2,{success:t.isSuccess,response:t}]}}))}))},this.removeItem=function(i){return t(m,void 0,void 0,(function(){return e(this,(function(t){switch(t.label){case 0:return[4,this.changeItemQuantity(i,0)];case 1:return[2,t.sent()]}}))}))},this.removeCart=function(){localStorage.removeItem("cartId"),m.cart=void 0},this.waitUntilInitialized=function(){return new Promise((function(t){var e=setInterval((function(){m.isCartLoadFinished&&(clearInterval(e),t(null))}),100)}))},this.getEditLineID=function(){if("undefined"!=typeof window)return new URLSearchParams(window.location.search).get("editLineID")||""},this.findExistingItem=function(t,e){var i,r,n,a,o,s;if(!e.productOptionSetId&&!t.bundleSettings)return null===(i=m.cart)||void 0===i?void 0:i.orderLineItems.find((function(e){return e.variant.id===t.id}));if(t.bundleSettings){var d=m.getEditLineID();if(d)if(S=null===(r=m.cart)||void 0===r?void 0:r.orderLineItems.find((function(t){return t.id===d})))return S}var c=(null===(n=m.cart)||void 0===n?void 0:n.orderLineItems.filter((function(e){return e.variant.id===t.id})))||[];if(e.productOptionSetId)for(var u=m.getOptionValues(e),l=c.filter((function(t){var e;return(null===(e=t.options)||void 0===e?void 0:e.length)===u.length}))||[],p=0,v=l;p<v.length;p++){if((S=v[p]).options){for(var f=!0,h=function(t){var i=t.values.map((function(t){return t.value})),r=null===(s=null===(o=null===(a=e.productOptionSet)||void 0===a?void 0:a.options)||void 0===o?void 0:o.find((function(e){return e.id===t.productOptionId})))||void 0===s?void 0:s.values;if(!(f=f&&i.length===(null==r?void 0:r.length)&&i.every((function(t){return null==r?void 0:r.includes(t)}))))return"break"},I=0,g=S.options;I<g.length;I++){if("break"===h(g[I]))break}if(f){if(!t.bundleSettings)return S;if(m.isSameBundleItem(t,S))return S}}}else if(t.bundleSettings)for(var C=0,b=c;C<b.length;C++){var S=b[C];if(m.isSameBundleItem(t,S))return S}},this.isSameBundleItem=function(t,e){var i,r=null===(i=t.bundleSettings)||void 0===i?void 0:i.products,n=e.variant.bundleProducts;return!!r&&((null==r?void 0:r.length)===(null==n?void 0:n.length)&&(null==r?void 0:r.every((function(t){return!!(null==n?void 0:n.find((function(e){var i;return e.id===t.id&&e.quantity===t.quantity&&e.variant.id===(null===(i=t.product)||void 0===i?void 0:i.selectedVariant.id)})))}))))},this.saveCouponCode=function(i){return t(m,void 0,void 0,(function(){var t;return e(this,(function(e){switch(e.label){case 0:return this.cart?[4,n.saveCartCouponCode({cartId:this.cart.id,couponCode:i})]:[2,{success:!1,validationError:p.EMPTY_CART}];case 1:return(t=e.sent()).isSuccess&&t.data&&(this.cart=t.data),[2,{success:t.isSuccess,response:t}]}}))}))},this.removeCouponCode=function(){return t(m,void 0,void 0,(function(){return e(this,(function(t){return[2,this.saveCouponCode(null)]}))}))},this.removeGiftCardLine=function(i){return t(m,void 0,void 0,(function(){var t;return e(this,(function(e){switch(e.label){case 0:return this.cart?[4,n.removeGiftCardFromCart({cartId:this.cart.id,giftCardId:i})]:[2,{success:!1,validationError:p.EMPTY_CART}];case 1:return(t=e.sent()).isSuccess&&t.data&&(this.cart=t.data),[2,{success:t.isSuccess,response:t}]}}))}))},this.removeLoyaltyPointsFromCart=function(i){return t(m,void 0,void 0,(function(){var t;return e(this,(function(e){switch(e.label){case 0:return this.cart?[4,n.removeLoyaltyPointsFromCart({cartId:this.cart.id,loyaltySpendingMethodId:i})]:[2,{success:!1,validationError:p.EMPTY_CART}];case 1:return(t=e.sent()).isSuccess&&t.data&&(this.cart=t.data),[2,{success:t.isSuccess,response:t}]}}))}))},this.saveCart=function(i){return t(m,void 0,void 0,(function(){var t,r;return e(this,(function(e){switch(e.label){case 0:return(t=new c(i)).customer&&this.baseStore.customerStore.customer&&(t.customer.notificationsAccepted=this.baseStore.customerStore.customer.subscriptionStatus===o.SUBSCRIBED),[4,n.saveCart({input:t})];case 1:return[2,{success:(r=e.sent()).isSuccess,response:r}]}}))}))},this.removeCardIfEmpty=function(){var t;0===(null===(t=m.cart)||void 0===t?void 0:t.itemCount)&&m.removeCart()},this.getCart=function(){return t(m,void 0,void 0,(function(){var t,i,r,a,o,s;return e(this,(function(e){switch(e.label){case 0:if(!("undefined"!=typeof localStorage))return[2];e.label=1;case 1:return e.trys.push([1,6,7,8]),this._isLoadingCart=!0,t=null===(o=this.baseStore.customerStore.customer)||void 0===o?void 0:o.id,(i=(null===(s=this.cart)||void 0===s?void 0:s.id)||localStorage.getItem("cartId"))||t?[4,n.getCartById({id:i})]:[2];case 2:return(r=e.sent()).isSuccess&&r.data?[4,this.setCart(r.data)]:[3,4];case 3:return e.sent(),[3,5];case 4:this.removeCart(),e.label=5;case 5:return[3,8];case 6:return a=e.sent(),console.log(a),[3,8];case 7:return this._isLoadingCart=!1,this._isCartLoadFinished=!0,[7];case 8:return[2]}}))}))},this.setCart=function(i){return t(m,void 0,void 0,(function(){return e(this,(function(t){return localStorage.setItem("cartId",i.id),this.cart=i,[2]}))}))},this.beforeCartOperationCheck=function(){return t(m,void 0,void 0,(function(){var t;return e(this,(function(e){switch(e.label){case 0:return t=localStorage.getItem("cartId"),!this.cart||t?[3,1]:(this.removeCart(),[3,3]);case 1:return this.cart||!t?[3,3]:[4,this.getCart()];case 2:e.sent(),e.label=3;case 3:return[2]}}))}))},this.createCartLineOptions=function(t){if(t&&t.options){for(var e=[],i=0,r=t.options;i<r.length;i++){var n=r[i],a={productOptionId:n.productOptionId,productOptionsSetId:n.productOptionsSetId,values:n.values.map((function(t){return t.value}))};e.push(a)}return e}return null},this.getOptionValues=function(t){var e,i=[];return null===(e=t.productOptionSet)||void 0===e||e.options.forEach((function(t){var e=t.values;t.type===a.DATE_PICKER&&(e=t.values.map((function(t){var e=new Date(t);return isNaN(e.getTime())?t:e.toISOString().split("T")[0]}))),t.values.length&&i.push({productOptionId:t.id,productOptionsSetId:t.productOptionSetId,values:e})})),i},this._windowAddToCart=function(i){return t(m,void 0,void 0,(function(){var t,r,a,o,c,u,p,m,v,f,h;return e(this,(function(e){switch(e.label){case 0:return e.trys.push([0,5,,6]),[4,this.beforeCartOperationCheck()];case 1:return e.sent(),t=i.itemId?null===(v=this.cart)||void 0===v?void 0:v.items.find((function(t){return t.id===i.itemId})):null,[4,n.addItemToCart({input:{cartId:(null===(f=this.cart)||void 0===f?void 0:f.id)||null,item:{id:i.itemId||null,quantity:i.quantity,variant:{id:i.variantId}},priceListId:s.getPriceListId()||null,salesChannelId:s.getSalesChannelId(),storefrontThemeId:s.getStorefrontThemeId(),metadata:{_fbp:l("_fbp"),_fbc:l("_fbc"),_ga:l("_ga")}}})];case 2:return(r=e.sent()).isSuccess&&r.data?(i.itemId||(a=(null===(h=this.cart)||void 0===h?void 0:h.items.map((function(t){return t.id})))||[],o=r.data.items.map((function(t){return t.id})),(c=o.find((function(t){return!a.includes(t)})))&&(t=r.data.items.find((function(t){return t.id===c})))),[4,this.setCart(r.data)]):[3,4];case 3:e.sent(),e.label=4;case 4:return this.cart&&t&&(u="".concat(this.cart.id,"-").concat(this.cart.updatedAt),(p=i.quantity)>i.quantity?d.removeFromCart(t,p-i.quantity,this.cart):d.addToCart(t,i.quantity-p,u,this.cart)),this.removeCardIfEmpty(),[2,{success:r.isSuccess,response:r}];case 5:return m=e.sent(),console.log(m),[2,{success:!1}];case 6:return[2]}}))}))},this.baseStore=u,r(this)}return Object.defineProperty(m.prototype,"isLoadingCart",{get:function(){return this._isLoadingCart},enumerable:!1,configurable:!0}),Object.defineProperty(m.prototype,"isCartLoadFinished",{get:function(){return this._isCartLoadFinished},enumerable:!1,configurable:!0}),Object.defineProperty(m.prototype,"checkoutId",{get:function(){var t;return null===(t=this.cart)||void 0===t?void 0:t.id},enumerable:!1,configurable:!0}),Object.defineProperty(m.prototype,"checkoutUrl",{get:function(){if(this.checkoutId)return"/checkout/".concat(this.checkoutId,"?step=info")},enumerable:!1,configurable:!0}),m.prototype.getAcceptedOffers=function(t){var e=(null==t?void 0:t.filter((function(t){return t.isSelected&&!t.isAccepted})).map((function(t){return{campaignOfferId:t.campaignOfferId,campaignOfferProductId:t.campaignOfferProductId,productId:t.product.id,quantity:t.quantity,variantId:t.product.selectedVariant.id}})))||[];return e.length?e:void 0},m.prototype.createPayWithIkasSession=function(i,r,n){var a,o,d,c,l,p,m,v;return void 0===r&&(r=1),t(this,void 0,void 0,(function(){var t,f,h,I,g,C,b;return e(this,(function(e){switch(e.label){case 0:return e.trys.push([0,4,,5]),t=n,n?[3,2]:(f=s.getSalesChannelId(),h=s.getStorefrontThemeId(),f&&h&&i?(I=i.selectedVariant)?[4,this.addItem(I,i,r,{isPayWithIkas:!0})]:[2,{success:!1,error:"No variant selected"}]:[2,{success:!1,error:"Missing storefront configuration"}]);case 1:if(!(g=e.sent()).success||!(null===(o=null===(a=g.response)||void 0===a?void 0:a.data)||void 0===o?void 0:o.id))return[2,{success:!1,error:(null===(l=null===(c=null===(d=g.response)||void 0===d?void 0:d.graphQLErrors)||void 0===c?void 0:c[0])||void 0===l?void 0:l.message)||"Failed to create cart"}];t=g.response.data.id,e.label=2;case 2:return t?[4,u({cartId:t})]:[2,{success:!1,error:"Failed to create cart"}];case 3:return!(C=e.sent()).data||(null===(p=C.graphQLErrors)||void 0===p?void 0:p.length)?[2,{success:!1,error:(null===(v=null===(m=C.graphQLErrors)||void 0===m?void 0:m[0])||void 0===v?void 0:v.message)||"Failed to create Pay with ikas session"}]:[2,{success:!0,payUrl:C.data.payUrl,expiresAt:C.data.expiresAt}];case 4:return[2,{success:!1,error:(b=e.sent())instanceof Error?b.message:"Unknown error occurred"}];case 5:return[2]}}))}))},m}();!function(t){t.INSUFFICIENT_STOCK="INSUFFICIENT_STOCK",t.INVALID_PRODUCT_OPTION_VALUES="INVALID_PRODUCT_OPTION_VALUES",t.EMPTY_CART="EMPTY_CART"}(p||(p={}));export{m as CART_LS_KEY,p as IkasCartOperationValidationError,v as IkasCartStore};
1
+ import{__awaiter as t,__generator as e,__assign as i}from'./../../ext/tslib/tslib.es6.js';import{makeAutoObservable as r}from"mobx";import n from"./api.js";import"../../models/data/blog/category/index.js";import"../../models/data/blog/content/index.js";import"../../models/data/blog/meta-data/index.js";import"../../models/data/blog/tag/index.js";import"../../models/data/blog/index.js";import"../../models/data/brand/index.js";import{IkasProductOptionType as a,IkasCustomerEmailSubscriptionStatus as o}from"@ikas/storefront-models";import"../../models/data/campaign-offer/index.js";import"../../models/data/cart/campaign-offer/index.js";import"../../models/data/cart/index.js";import"../../models/data/category/path-item/index.js";import"../../models/data/category/index.js";import"../../models/data/checkout/index.js";import"../../models/data/checkout-settings/index.js";import"../../models/data/city/index.js";import"../../models/data/country/index.js";import"../../models/data/customer/address/region/index.js";import"../../models/data/customer/address/index.js";import"../../models/data/customer/address/ikas-localized-customer-address.js";import"../../models/data/customer/attribute/index.js";import"../../models/data/customer/review/summary/index.js";import"../../models/data/customer/review/index.js";import"../../models/data/customer/index.js";import"../../models/data/customer-form-data-input/index.js";import"../../models/data/district/index.js";import"../../models/data/favorite-product/index.js";import"../../models/data/filter-category/index.js";import"../../models/data/html-meta-data/index.js";import"../../models/data/image/index.js";import"../../models/data/order/address/region/index.js";import"../../models/data/order/address/index.js";import"../../models/data/order/line-item/option/value/index.js";import"../../models/data/order/line-item/variant/value/index.js";import"../../models/data/order/line-item/variant/index.js";import"../../models/data/order/line-item/index.js";import"../../models/data/order/order-transaction-with-prices/index.js";import"../../models/data/order/package/index.js";import"../../models/data/order/transaction/index.js";import"../../models/data/order/index.js";import"../../models/data/payment-gateway/index.js";import"../../models/data/product/attribute-value/index.js";import"../../models/data/product/filter/index.js";import"../../models/data/product/campaign-offer/index.js";import"../../models/data/product/option-set/option/index.js";import"../../models/data/product/option-set/index.js";import"../../models/data/product/stock-location/index.js";import"../../models/data/product/variant/price/index.js";import"../../models/data/product/variant/index.js";import"../../models/data/variant-type/index.js";import"../../models/data/product/index.js";import"../../models/data/raffle/index.js";import"../../models/data/shipping-zone/state/city/district/region/index.js";import"../../models/data/state/index.js";import"../../models/data/storefront-popup/storefront-popup-display-settings/index.js";import"../../models/data/storefront-popup/storefront-popup-page-filter/index.js";import"../../models/data/storefront-popup/index.js";import"../../models/data/theme-json/index.js";import"../../models/data/theme-json/component/index.js";import"../../models/data/theme-json/custom-data/index.js";import"../../models/data/variant-type/variant-value/index.js";import"@ikas/localized-address";import{IkasStorefrontConfig as s}from"@ikas/storefront-config";import{Analytics as d}from"../../analytics/analytics.js";import"../../analytics/head/index.js";import{SaveCartInput as u,createPayWithIkasSessionUrl as c}from"@ikas/storefront-api";import{getCookieValue as l}from"../../utils/helper.js";var p,m="cartId",v=function(){function m(c){var m=this;this.cart=null,this._isLoadingCart=!1,this._isCartLoadFinished=!1,this.addItem=function(r,a,o,u){return void 0===o&&(o=1),t(m,void 0,void 0,(function(){var t,c,m,v,f,h,I,g,C,b,S,y,j,x,O,P,_,T,L,E;return e(this,(function(e){switch(e.label){case 0:if(t=null!==(y=null==u?void 0:u.isPayWithIkas)&&void 0!==y&&y,this.checkBundleProductsStock(r,a).length>0)return[2,{success:!1,validationError:p.INSUFFICIENT_STOCK}];e.label=1;case 1:return e.trys.push([1,7,,8]),!t&&a.productOptionSetId&&!a.productOptionSet&&this.baseStore.router?(this.baseStore.router.push(a.productHref),[2,{success:!1,validationError:p.INVALID_PRODUCT_OPTION_VALUES}]):a.hasValidProductOptionValues?r.hasStock||r.sellIfOutOfStock?(c=void 0,t?[3,3]:[4,this.beforeCartOperationCheck()]):[2,{success:!1,validationError:p.INSUFFICIENT_STOCK}]:[2,{success:!1,validationError:p.INVALID_PRODUCT_OPTION_VALUES}];case 2:if(e.sent(),this.cart&&(c=this.findExistingItem(r,a))){if(null===(j=a.productOptionSet)||void 0===j||j.initOptionValues(),!r.bundleSettings)return[2,this.changeItemQuantity(c,c.quantity+1,a.offers,a)];if(!this.getEditLineID())return[2,this.changeItemQuantity(c,c.quantity+1,a.offers,a)]}e.label=3;case 3:return m=this.getOptionValues(a),v=o,(null==(f=null===(x=a.salesChannels)||void 0===x?void 0:x.find((function(t){return t.id===s.getSalesChannelId()})))?void 0:f.minQuantityPerCart)&&o<f.minQuantityPerCart&&(v=f.minQuantityPerCart),(null==f?void 0:f.maxQuantityPerCart)&&o>f.maxQuantityPerCart&&(v=f.maxQuantityPerCart),h=t?null:(null===(O=this.cart)||void 0===O?void 0:O.id)||localStorage.getItem("cartId"),[4,n.addItemToCart({input:i({cartId:h,item:{id:null!==(P=null==c?void 0:c.id)&&void 0!==P?P:null,quantity:null!==(_=null==c?void 0:c.quantity)&&void 0!==_?_:v,variant:{id:r.id,name:a.name,bundleProducts:null===(T=r.bundleSettings)||void 0===T?void 0:T.products.map((function(t){var e,i,r;return{id:t.id,productId:(null===(e=t.product)||void 0===e?void 0:e.id)||"",variantId:(null===(r=null===(i=t.product)||void 0===i?void 0:i.selectedVariant)||void 0===r?void 0:r.id)||"",quantity:t.quantity}}))}},acceptedOffers:this.getAcceptedOffers(a.offers),options:m.length?m:null,priceListId:s.getPriceListId()||null,salesChannelId:s.getSalesChannelId(),storefrontThemeId:s.getStorefrontThemeId(),metadata:{_fbp:l("_fbp"),_fbc:l("_fbc"),_ga:l("_ga")}},a.selectedVariant.subscriptionPlan&&a.selectedVariant.subscriptionPlan.isSelected?{subscriptionPlanId:a.selectedVariant.subscriptionPlan.subscriptionPlanId,subscriptionPlanPeriodId:null===(L=a.selectedVariant.subscriptionPlan.periods.find((function(t){return t.isSelected})))||void 0===L?void 0:L.id}:{})})];case 4:return I=e.sent(),t?[3,6]:I.isSuccess&&I.data?[4,this.setCart(I.data)]:[3,6];case 5:e.sent(),e.label=6;case 6:return(g=t?I.data:this.cart)&&(C="".concat(g.id,"-").concat(g.updatedAt),(b=g.items.find((function(t){return t.variant.id===r.id})))&&d.addToCart(b,o,C,g,a)),null===(E=a.productOptionSet)||void 0===E||E.initOptionValues(),[2,{success:I.isSuccess,response:I}];case 7:return S=e.sent(),console.log(S),[2,{success:!1}];case 8:return[2]}}))}))},this.changeItemQuantity=function(r,a,o,u){return t(m,void 0,void 0,(function(){var t,c,p,m,v,f,h,I,g;return e(this,(function(e){switch(e.label){case 0:return e.trys.push([0,5,,6]),[4,this.beforeCartOperationCheck()];case 1:return e.sent(),[4,n.addItemToCart({input:i({cartId:(null===(v=this.cart)||void 0===v?void 0:v.id)||null,item:{id:r.id,quantity:a,variant:{id:r.variant.id,name:r.variant.name,bundleProducts:null===(f=r.variant.bundleProducts)||void 0===f?void 0:f.map((function(t){return{id:t.id,productId:t.variant.productId||"",variantId:t.variant.id||"",quantity:t.quantity}}))}},acceptedOffers:this.getAcceptedOffers(o),options:this.createCartLineOptions(r),priceListId:s.getPriceListId()||null,salesChannelId:s.getSalesChannelId(),storefrontThemeId:s.getStorefrontThemeId(),metadata:{_fbp:l("_fbp"),_fbc:l("_fbc"),_ga:l("_ga")}},(null===(h=null==u?void 0:u.selectedVariant)||void 0===h?void 0:h.subscriptionPlan)&&(null===(I=null==u?void 0:u.selectedVariant)||void 0===I?void 0:I.subscriptionPlan.isSelected)?{subscriptionPlanId:null==u?void 0:u.selectedVariant.subscriptionPlan.subscriptionPlanId,subscriptionPlanPeriodId:null===(g=null==u?void 0:u.selectedVariant.subscriptionPlan.periods.find((function(t){return t.isSelected})))||void 0===g?void 0:g.id}:{})})];case 2:return(t=e.sent()).isSuccess&&t.data?[4,this.setCart(t.data)]:[3,4];case 3:e.sent(),e.label=4;case 4:return this.cart&&(c="".concat(this.cart.id,"-").concat(this.cart.updatedAt),(p=r.quantity)>a?d.removeFromCart(r,p-a,this.cart):d.addToCart(r,a-p,c,this.cart,u)),this.removeCardIfEmpty(),[2,{success:t.isSuccess,response:t}];case 5:return m=e.sent(),console.log(m),[2,{success:!1}];case 6:return[2]}}))}))},this.createCart=function(i){return t(m,void 0,void 0,(function(){var t,r=this;return e(this,(function(e){switch(e.label){case 0:return[4,n.createCart({input:{items:i.variantLines.map((function(t){return{variant:{id:t.variantId},quantity:t.quantity}})),couponCode:i.couponCode,salesChannelId:s.getSalesChannelId(),storefrontThemeId:s.getStorefrontThemeId()}})];case 1:return(t=e.sent()).isSuccess&&t.data?[4,this.setCart(t.data)]:[3,3];case 2:e.sent(),e.label=3;case 3:return i.variantLines.forEach((function(t){if(r.cart){var e="".concat(r.cart.id,"-").concat(r.cart.updatedAt),i=r.cart.items.find((function(e){return e.variant.id===t.variantId}));i&&d.addToCart(i,t.quantity,e,r.cart)}})),[2,{success:t.isSuccess,response:t}]}}))}))},this.removeItem=function(i){return t(m,void 0,void 0,(function(){return e(this,(function(t){switch(t.label){case 0:return[4,this.changeItemQuantity(i,0)];case 1:return[2,t.sent()]}}))}))},this.removeCart=function(){localStorage.removeItem("cartId"),m.cart=void 0},this.waitUntilInitialized=function(){return new Promise((function(t){var e=setInterval((function(){m.isCartLoadFinished&&(clearInterval(e),t(null))}),100)}))},this.getEditLineID=function(){if("undefined"!=typeof window)return new URLSearchParams(window.location.search).get("editLineID")||""},this.findExistingItem=function(t,e){var i,r,n,a,o,s;if(!e.productOptionSetId&&!t.bundleSettings)return null===(i=m.cart)||void 0===i?void 0:i.orderLineItems.find((function(e){return e.variant.id===t.id}));if(t.bundleSettings){var d=m.getEditLineID();if(d)if(S=null===(r=m.cart)||void 0===r?void 0:r.orderLineItems.find((function(t){return t.id===d})))return S}var u=(null===(n=m.cart)||void 0===n?void 0:n.orderLineItems.filter((function(e){return e.variant.id===t.id})))||[];if(e.productOptionSetId)for(var c=m.getOptionValues(e),l=u.filter((function(t){var e;return(null===(e=t.options)||void 0===e?void 0:e.length)===c.length}))||[],p=0,v=l;p<v.length;p++){if((S=v[p]).options){for(var f=!0,h=function(t){var i=t.values.map((function(t){return t.value})),r=null===(s=null===(o=null===(a=e.productOptionSet)||void 0===a?void 0:a.options)||void 0===o?void 0:o.find((function(e){return e.id===t.productOptionId})))||void 0===s?void 0:s.values;if(!(f=f&&i.length===(null==r?void 0:r.length)&&i.every((function(t){return null==r?void 0:r.includes(t)}))))return"break"},I=0,g=S.options;I<g.length;I++){if("break"===h(g[I]))break}if(f){if(!t.bundleSettings)return S;if(m.isSameBundleItem(t,S))return S}}}else if(t.bundleSettings)for(var C=0,b=u;C<b.length;C++){var S=b[C];if(m.isSameBundleItem(t,S))return S}},this.isSameBundleItem=function(t,e){var i,r=null===(i=t.bundleSettings)||void 0===i?void 0:i.products,n=e.variant.bundleProducts;return!!r&&((null==r?void 0:r.length)===(null==n?void 0:n.length)&&(null==r?void 0:r.every((function(t){return!!(null==n?void 0:n.find((function(e){var i;return e.id===t.id&&e.quantity===t.quantity&&e.variant.id===(null===(i=t.product)||void 0===i?void 0:i.selectedVariant.id)})))}))))},this.saveCouponCode=function(i){return t(m,void 0,void 0,(function(){var t;return e(this,(function(e){switch(e.label){case 0:return this.cart?[4,n.saveCartCouponCode({cartId:this.cart.id,couponCode:i})]:[2,{success:!1,validationError:p.EMPTY_CART}];case 1:return(t=e.sent()).isSuccess&&t.data&&(this.cart=t.data),[2,{success:t.isSuccess,response:t}]}}))}))},this.removeCouponCode=function(){return t(m,void 0,void 0,(function(){return e(this,(function(t){return[2,this.saveCouponCode(null)]}))}))},this.removeGiftCardLine=function(i){return t(m,void 0,void 0,(function(){var t;return e(this,(function(e){switch(e.label){case 0:return this.cart?[4,n.removeGiftCardFromCart({cartId:this.cart.id,giftCardId:i})]:[2,{success:!1,validationError:p.EMPTY_CART}];case 1:return(t=e.sent()).isSuccess&&t.data&&(this.cart=t.data),[2,{success:t.isSuccess,response:t}]}}))}))},this.removeLoyaltyPointsFromCart=function(i){return t(m,void 0,void 0,(function(){var t;return e(this,(function(e){switch(e.label){case 0:return this.cart?[4,n.removeLoyaltyPointsFromCart({cartId:this.cart.id,loyaltySpendingMethodId:i})]:[2,{success:!1,validationError:p.EMPTY_CART}];case 1:return(t=e.sent()).isSuccess&&t.data&&(this.cart=t.data),[2,{success:t.isSuccess,response:t}]}}))}))},this.saveCart=function(i){return t(m,void 0,void 0,(function(){var t,r;return e(this,(function(e){switch(e.label){case 0:return(t=new u(i)).customer&&this.baseStore.customerStore.customer&&(t.customer.notificationsAccepted=this.baseStore.customerStore.customer.subscriptionStatus===o.SUBSCRIBED),[4,n.saveCart({input:t})];case 1:return[2,{success:(r=e.sent()).isSuccess,response:r}]}}))}))},this.removeCardIfEmpty=function(){var t;0===(null===(t=m.cart)||void 0===t?void 0:t.itemCount)&&m.removeCart()},this.getCart=function(){return t(m,void 0,void 0,(function(){var t,i,r,a,o,s;return e(this,(function(e){switch(e.label){case 0:if(!("undefined"!=typeof localStorage))return[2];e.label=1;case 1:return e.trys.push([1,6,7,8]),this._isLoadingCart=!0,t=null===(o=this.baseStore.customerStore.customer)||void 0===o?void 0:o.id,(i=(null===(s=this.cart)||void 0===s?void 0:s.id)||localStorage.getItem("cartId"))||t?[4,n.getCartById({id:i})]:[2];case 2:return(r=e.sent()).isSuccess&&r.data?[4,this.setCart(r.data)]:[3,4];case 3:return e.sent(),[3,5];case 4:this.removeCart(),e.label=5;case 5:return[3,8];case 6:return a=e.sent(),console.log(a),[3,8];case 7:return this._isLoadingCart=!1,this._isCartLoadFinished=!0,[7];case 8:return[2]}}))}))},this.setCart=function(i){return t(m,void 0,void 0,(function(){return e(this,(function(t){return localStorage.setItem("cartId",i.id),this.cart=i,[2]}))}))},this.beforeCartOperationCheck=function(){return t(m,void 0,void 0,(function(){var t;return e(this,(function(e){switch(e.label){case 0:return t=localStorage.getItem("cartId"),!this.cart||t?[3,1]:(this.removeCart(),[3,3]);case 1:return this.cart||!t?[3,3]:[4,this.getCart()];case 2:e.sent(),e.label=3;case 3:return[2]}}))}))},this.createCartLineOptions=function(t){if(t&&t.options){for(var e=[],i=0,r=t.options;i<r.length;i++){var n=r[i],a={productOptionId:n.productOptionId,productOptionsSetId:n.productOptionsSetId,values:n.values.map((function(t){return t.value}))};e.push(a)}return e}return null},this.getOptionValues=function(t){var e,i=[];return null===(e=t.productOptionSet)||void 0===e||e.options.forEach((function(t){var e=t.values;t.type===a.DATE_PICKER&&(e=t.values.map((function(t){var e=new Date(t);return isNaN(e.getTime())?t:e.toISOString().split("T")[0]}))),t.values.length&&i.push({productOptionId:t.id,productOptionsSetId:t.productOptionSetId,values:e})})),i},this.checkBundleProductsStock=function(t,e){var i,r=null===(i=t.bundleSettings)||void 0===i?void 0:i.products,n=null==r?void 0:r.reduce((function(t,e){var i,r=null===(i=e.product)||void 0===i?void 0:i.selectedVariant;return null==(null==r?void 0:r.id)||r.sellIfOutOfStock||(t[r.id]=r.stock),t}),{}),a=null==r?void 0:r.reduce((function(t,e){var i,r,n=null===(i=e.product)||void 0===i?void 0:i.selectedVariant;return null==(null==n?void 0:n.id)||n.sellIfOutOfStock||(t[n.id]=(null!==(r=t[n.id])&&void 0!==r?r:0)+e.quantity),t}),{}),o=Object.entries(null!=a?a:{}).filter((function(t){var e,i=t[0];return t[1]>(null!==(e=null==n?void 0:n[i])&&void 0!==e?e:0)})).map((function(t){var e,i=t[0];return{variantId:i,neededQuantity:t[1],availableStock:null!==(e=null==n?void 0:n[i])&&void 0!==e?e:0}}));return o},this._windowAddToCart=function(i){return t(m,void 0,void 0,(function(){var t,r,a,o,u,c,p,m,v,f,h;return e(this,(function(e){switch(e.label){case 0:return e.trys.push([0,5,,6]),[4,this.beforeCartOperationCheck()];case 1:return e.sent(),t=i.itemId?null===(v=this.cart)||void 0===v?void 0:v.items.find((function(t){return t.id===i.itemId})):null,[4,n.addItemToCart({input:{cartId:(null===(f=this.cart)||void 0===f?void 0:f.id)||null,item:{id:i.itemId||null,quantity:i.quantity,variant:{id:i.variantId}},priceListId:s.getPriceListId()||null,salesChannelId:s.getSalesChannelId(),storefrontThemeId:s.getStorefrontThemeId(),metadata:{_fbp:l("_fbp"),_fbc:l("_fbc"),_ga:l("_ga")}}})];case 2:return(r=e.sent()).isSuccess&&r.data?(i.itemId||(a=(null===(h=this.cart)||void 0===h?void 0:h.items.map((function(t){return t.id})))||[],o=r.data.items.map((function(t){return t.id})),(u=o.find((function(t){return!a.includes(t)})))&&(t=r.data.items.find((function(t){return t.id===u})))),[4,this.setCart(r.data)]):[3,4];case 3:e.sent(),e.label=4;case 4:return this.cart&&t&&(c="".concat(this.cart.id,"-").concat(this.cart.updatedAt),(p=i.quantity)>i.quantity?d.removeFromCart(t,p-i.quantity,this.cart):d.addToCart(t,i.quantity-p,c,this.cart)),this.removeCardIfEmpty(),[2,{success:r.isSuccess,response:r}];case 5:return m=e.sent(),console.log(m),[2,{success:!1}];case 6:return[2]}}))}))},this.baseStore=c,r(this)}return Object.defineProperty(m.prototype,"isLoadingCart",{get:function(){return this._isLoadingCart},enumerable:!1,configurable:!0}),Object.defineProperty(m.prototype,"isCartLoadFinished",{get:function(){return this._isCartLoadFinished},enumerable:!1,configurable:!0}),Object.defineProperty(m.prototype,"checkoutId",{get:function(){var t;return null===(t=this.cart)||void 0===t?void 0:t.id},enumerable:!1,configurable:!0}),Object.defineProperty(m.prototype,"checkoutUrl",{get:function(){if(this.checkoutId)return"/checkout/".concat(this.checkoutId,"?step=info")},enumerable:!1,configurable:!0}),m.prototype.getAcceptedOffers=function(t){var e=(null==t?void 0:t.filter((function(t){return t.isSelected&&!t.isAccepted})).map((function(t){return{campaignOfferId:t.campaignOfferId,campaignOfferProductId:t.campaignOfferProductId,productId:t.product.id,quantity:t.quantity,variantId:t.product.selectedVariant.id}})))||[];return e.length?e:void 0},m.prototype.createPayWithIkasSession=function(i,r,n){var a,o,d,u,l,p,m,v;return void 0===r&&(r=1),t(this,void 0,void 0,(function(){var t,f,h,I,g,C,b;return e(this,(function(e){switch(e.label){case 0:return e.trys.push([0,4,,5]),t=n,n?[3,2]:(f=s.getSalesChannelId(),h=s.getStorefrontThemeId(),f&&h&&i?(I=i.selectedVariant)?[4,this.addItem(I,i,r,{isPayWithIkas:!0})]:[2,{success:!1,error:"No variant selected"}]:[2,{success:!1,error:"Missing storefront configuration"}]);case 1:if(!(g=e.sent()).success||!(null===(o=null===(a=g.response)||void 0===a?void 0:a.data)||void 0===o?void 0:o.id))return[2,{success:!1,error:(null===(l=null===(u=null===(d=g.response)||void 0===d?void 0:d.graphQLErrors)||void 0===u?void 0:u[0])||void 0===l?void 0:l.message)||"Failed to create cart"}];t=g.response.data.id,e.label=2;case 2:return t?[4,c({cartId:t})]:[2,{success:!1,error:"Failed to create cart"}];case 3:return!(C=e.sent()).data||(null===(p=C.graphQLErrors)||void 0===p?void 0:p.length)?[2,{success:!1,error:(null===(v=null===(m=C.graphQLErrors)||void 0===m?void 0:m[0])||void 0===v?void 0:v.message)||"Failed to create Pay with ikas session"}]:[2,{success:!0,payUrl:C.data.payUrl,expiresAt:C.data.expiresAt}];case 4:return[2,{success:!1,error:(b=e.sent())instanceof Error?b.message:"Unknown error occurred"}];case 5:return[2]}}))}))},m}();!function(t){t.INSUFFICIENT_STOCK="INSUFFICIENT_STOCK",t.INVALID_PRODUCT_OPTION_VALUES="INVALID_PRODUCT_OPTION_VALUES",t.EMPTY_CART="EMPTY_CART"}(p||(p={}));export{m as CART_LS_KEY,p as IkasCartOperationValidationError,v as IkasCartStore};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ikas/storefront",
3
- "version": "6.1.0-beta.341",
3
+ "version": "6.1.0-beta.343",
4
4
  "description": "Storefront functionality for ikas storefront themes.",
5
5
  "author": "ikas",
6
6
  "license": "ISC",
@@ -23,11 +23,11 @@
23
23
  "swiper": "11.0.6"
24
24
  },
25
25
  "devDependencies": {
26
- "@ikas/storefront-api": "^6.1.0-beta.341",
27
- "@ikas/storefront-config": "^6.1.0-beta.341",
28
- "@ikas/storefront-model-functions": "^6.1.0-beta.341",
29
- "@ikas/storefront-models": "^6.1.0-beta.341",
30
- "@ikas/storefront-providers": "^6.1.0-beta.341",
26
+ "@ikas/storefront-api": "^6.1.0-beta.343",
27
+ "@ikas/storefront-config": "^6.1.0-beta.343",
28
+ "@ikas/storefront-model-functions": "^6.1.0-beta.343",
29
+ "@ikas/storefront-models": "^6.1.0-beta.343",
30
+ "@ikas/storefront-providers": "^6.1.0-beta.343",
31
31
  "@ikas/localized-address": "1.0.0-beta.6",
32
32
  "@adyen/adyen-web": "^5.57.0",
33
33
  "@rollup/plugin-commonjs": "^22.0.0",
@@ -62,11 +62,11 @@
62
62
  },
63
63
  "peerDependencies": {
64
64
  "@ikas/localized-address": "1.0.0-beta.6",
65
- "@ikas/storefront-api": "^6.1.0-beta.341",
66
- "@ikas/storefront-config": "^6.1.0-beta.341",
67
- "@ikas/storefront-model-functions": "^6.1.0-beta.341",
68
- "@ikas/storefront-models": "^6.1.0-beta.341",
69
- "@ikas/storefront-providers": "^6.1.0-beta.341",
65
+ "@ikas/storefront-api": "^6.1.0-beta.343",
66
+ "@ikas/storefront-config": "^6.1.0-beta.343",
67
+ "@ikas/storefront-model-functions": "^6.1.0-beta.343",
68
+ "@ikas/storefront-models": "^6.1.0-beta.343",
69
+ "@ikas/storefront-providers": "^6.1.0-beta.343",
70
70
  "mobx": "^6.1.3",
71
71
  "mobx-react-lite": "^3.1.5",
72
72
  "next": "12.2.0",