UNPKG

@salla.sa/twilight-components

Version:
5 lines 6.44 kB
/*! * Crafted with ❤ by Salla */ System.register(["./p-f422bbf9.system.js"],(function(e){"use strict";var t,n,i;return{setters:[function(e){t=e.r;n=e.h;i=e.H}],execute:function(){var o;(function(e){e["PERCENTAGE"]="percentage";e["FIXED"]="fixed";e["FREE_PRODUCT"]="free_product"})(o||(o={}));var r;(function(e){e["PRODUCT_COUNT"]="products_count";e["PRODUCT_PURCHASE"]="order_amount"})(r||(r={}));var a=":host{display:block}";var s=e("salla_conditional_offer",function(){function e(e){t(this,e);this.offer=null;this.products={};this.isLoading=true;this.canRender=true}e.prototype.componentWillLoad=function(){var e=this;salla.onReady().then((function(){var e,t,n;if(!((e=salla.config.get("store.features"))===null||e===void 0?void 0:e.includes("conditional-offer"))||salla.storage.get("cart")!==""&&!((n=(t=salla.storage.get("cart"))===null||t===void 0?void 0:t.summary)===null||n===void 0?void 0:n.count)){throw new Error("feature or cart object does not existed")}})).then((function(){return salla.api.cart.offers()})).then((function(t){var n=t.data;e.offer=n.find((function(e){return e.type==="conditional"}));if(!e.offer){throw new Error("No conditional offer has been activated in the merchant dashboard")}e.offer.details.discounts.unshift({value:0,min_spend:0});return e.updateInitialOfferValue()})).then((function(){return e.getProducts()})).then((function(){return salla.event.on("cart::updated",(function(t){return e.updateOfferValues(t)}))})).catch((function(t){e.canRender=false;salla.logger.warn("salla-conditional-offer:: ",t)})).finally((function(){e.isLoading=false}))};e.prototype.getProducts=function(){var e=this;var t=this.offer.details.discounts.filter((function(e){var t=e.type;return t===o.FREE_PRODUCT})).map((function(e){var t=e.value;return t}));if(t.length>0){return salla.product.fetch({source:"selected",source_value:t}).then((function(t){var n=t.data;n.forEach((function(t){var n=t.id,i=t.url,o=t.image;e.products[n]={url:i,image:o}}))}))}};e.prototype.updateInitialOfferValue=function(){var e=this;return salla.api.cart.details().then((function(t){var n=t.data.cart;return e.updateOfferValues(n)}))};e.prototype.updateOfferValues=function(e){var t=e.items,n=e.total;var i;var o=this.offer.details.based_on===r.PRODUCT_COUNT?t.reduce((function(e,t){var n=t.quantity;return e+n}),0):n;this.offer=Object.assign(Object.assign({},this.offer),{details:Object.assign(Object.assign({},(i=this.offer)===null||i===void 0?void 0:i.details),{current_value:o})});return this.offer};e.prototype.getCheckpointContent=function(e){var t,i;if(e.type===o.PERCENTAGE)return"".concat(e.value,"%");if(e.type===o.FIXED)return salla.money(e.value);if(e.type!==o.FREE_PRODUCT){salla.logger.error("salla-conditional-offer:: unexpected type (".concat(e.type,")!"));return""}var r=this.products[e.value];if(!r){salla.logger.error("salla-conditional-offer:: there is no product with id (".concat(e.value,")!"));return""}return n("a",{class:"s-conditional-offer-product-link",href:r.url},n("img",{class:"s-conditional-offer-checkpoint-image-content",loading:"lazy",decoding:"async",alt:((t=r===null||r===void 0?void 0:r.image)===null||t===void 0?void 0:t.alt)||"",src:(i=r===null||r===void 0?void 0:r.image)===null||i===void 0?void 0:i.url}))};e.prototype.getOfferType=function(e){var t;var n=(t=this.offer.details)===null||t===void 0?void 0:t.based_on;if(n===r.PRODUCT_COUNT)return salla.lang.choice("blocks.header.products_count",e.min_spend);if(n===r.PRODUCT_PURCHASE)return salla.money(e.min_spend);salla.logger.warn("salla-conditional-offer:: Unexpected offer detail's based_on value: ".concat(n));return"".concat(e.min_spend)};e.prototype.clamp=function(e,t,n){return Math.max(t,Math.min(e,n))};e.prototype.mapValueRanges=function(e,t,n,i,o){var r=(e-t)*(o-i)/(n-t)+i;if(r===Number.POSITIVE_INFINITY)return 100;if(r===Number.NEGATIVE_INFINITY)return 0;return this.clamp(r,0,100)};e.prototype.getCheckPointView=function(e,t){var i,o;var r=this.offer.details.discounts.findIndex((function(t){var n=t.min_spend;return n===e.min_spend}));var a=(o=(i=this.offer.details.discounts[r-1])===null||i===void 0?void 0:i.min_spend)!==null&&o!==void 0?o:0;var s=this.mapValueRanges(this.offer.details.current_value,a,e.min_spend,0,100);var l=e.min_spend<=this.offer.details.current_value;return n("div",{class:"s-conditional-offer-checkpoint-container"},t>0?[n("div",{key:"progress-line",class:"s-conditional-offer-progress-line-container"},n("div",{class:"s-conditional-offer-progress-line-inactive"}),n("div",{class:"s-conditional-offer-progress-line-active",style:{width:"".concat(s,"%")}})),n("div",{key:"checkpoint",class:"s-conditional-offer-checkpoint ".concat(l?"s-conditional-offer-active-checkpoint":"")},n("div",{class:"s-conditional-offer-item-avatar-content ".concat(l?"active":""),innerHTML:this.products&&this.getCheckpointContent(e)}),n("div",{class:"s-conditional-offer-checkpoint-label ".concat(l?"active":""),innerHTML:this.getOfferType(e)}))]:n("div",{key:"label",class:{"s-conditional-offer-checkpoint-label":true,"first-checkpoint":t===0,active:l},innerHTML:this.getOfferType(e)}))};e.prototype.getLoadingSkeletonView=function(){return n(i,{class:"s-conditional-offer-container"},n("div",{class:"s-conditional-offer-skeleton-inner-container"},n("div",{class:"s-conditional-offer-skeleton-subtitle"},n("salla-skeleton",{height:"16px",width:"30%"})),n("div",{class:"s-conditional-offer-skeleton-subtitle"},n("salla-skeleton",{height:"16px",width:"35%"})),n("div",{class:"s-conditional-offer-skeleton-checkpoints-wrapper"},Array(3).fill(null).map((function(){return[n("salla-skeleton",{key:"checkpoint-line",height:"8px"}),n("div",{key:"checkpoint"},n("salla-skeleton",{height:"60px",width:"60px",type:"circle"}))]})))))};e.prototype.render=function(){var e=this;if(!this.canRender)return null;if(this.isLoading)return this.getLoadingSkeletonView();return n(i,{class:"s-conditional-offer-container"},n("div",{class:"s-conditional-offer-title-wrapper"},n("div",{class:"s-conditional-offer-title"},this.offer.title),this.offer.description?n("div",{class:"s-conditional-offer-subtitle"},this.offer.description," ",n("i",{class:"sicon-information"})):null),n("div",{class:"s-conditional-offer-progress-container"},this.offer.details.discounts.map((function(t,i){return n("div",{class:{"flex-1":i>0},key:t.min_spend},e.getCheckPointView(t,i))}))))};return e}());s.style=a}}})); //# sourceMappingURL=p-67f80db1.system.entry.js.map