@salla.sa/twilight-components
Version:
Salla Web Component
5 lines • 6.62 kB
JavaScript
/*!
* Crafted with ❤ by Salla
*/
import{r as registerInstance,h,H as Host}from"./index-114c614f.js";var DiscountType;(function(e){e["PERCENTAGE"]="percentage";e["FIXED"]="fixed";e["FREE_PRODUCT"]="free_product"})(DiscountType||(DiscountType={}));var OfferType;(function(e){e["PRODUCT_COUNT"]="products_count";e["PRODUCT_PURCHASE"]="order_amount"})(OfferType||(OfferType={}));var sallaConditionalOfferCss=":host{display:block}";var SallaConditionalOffer=function(){function e(e){registerInstance(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===DiscountType.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===OfferType.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,n;if(e.type===DiscountType.PERCENTAGE)return"".concat(e.value,"%");if(e.type===DiscountType.FIXED)return salla.money(e.value);if(e.type!==DiscountType.FREE_PRODUCT){salla.logger.error("salla-conditional-offer:: unexpected type (".concat(e.type,")!"));return""}var i=this.products[e.value];if(!i){salla.logger.error("salla-conditional-offer:: there is no product with id (".concat(e.value,")!"));return""}return h("a",{class:"s-conditional-offer-product-link",href:i.url},h("img",{class:"s-conditional-offer-checkpoint-image-content",loading:"lazy",decoding:"async",alt:((t=i===null||i===void 0?void 0:i.image)===null||t===void 0?void 0:t.alt)||"",src:(n=i===null||i===void 0?void 0:i.image)===null||n===void 0?void 0:n.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===OfferType.PRODUCT_COUNT)return salla.lang.choice("blocks.header.products_count",e.min_spend);if(n===OfferType.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 a=(e-t)*(o-i)/(n-t)+i;if(a===Number.POSITIVE_INFINITY)return 100;if(a===Number.NEGATIVE_INFINITY)return 0;return this.clamp(a,0,100)};e.prototype.getCheckPointView=function(e,t){var n,i;var o=this.offer.details.discounts.findIndex((function(t){var n=t.min_spend;return n===e.min_spend}));var a=(i=(n=this.offer.details.discounts[o-1])===null||n===void 0?void 0:n.min_spend)!==null&&i!==void 0?i:0;var r=this.mapValueRanges(this.offer.details.current_value,a,e.min_spend,0,100);var s=e.min_spend<=this.offer.details.current_value;return h("div",{class:"s-conditional-offer-checkpoint-container"},t>0?[h("div",{key:"progress-line",class:"s-conditional-offer-progress-line-container"},h("div",{class:"s-conditional-offer-progress-line-inactive"}),h("div",{class:"s-conditional-offer-progress-line-active",style:{width:"".concat(r,"%")}})),h("div",{key:"checkpoint",class:"s-conditional-offer-checkpoint ".concat(s?"s-conditional-offer-active-checkpoint":"")},h("div",{class:"s-conditional-offer-item-avatar-content ".concat(s?"active":""),innerHTML:this.products&&this.getCheckpointContent(e)}),h("div",{class:"s-conditional-offer-checkpoint-label ".concat(s?"active":""),innerHTML:this.getOfferType(e)}))]:h("div",{key:"label",class:{"s-conditional-offer-checkpoint-label":true,"first-checkpoint":t===0,active:s},innerHTML:this.getOfferType(e)}))};e.prototype.getLoadingSkeletonView=function(){return h(Host,{class:"s-conditional-offer-container"},h("div",{class:"s-conditional-offer-skeleton-inner-container"},h("div",{class:"s-conditional-offer-skeleton-subtitle"},h("salla-skeleton",{height:"16px",width:"30%"})),h("div",{class:"s-conditional-offer-skeleton-subtitle"},h("salla-skeleton",{height:"16px",width:"35%"})),h("div",{class:"s-conditional-offer-skeleton-checkpoints-wrapper"},Array(3).fill(null).map((function(){return[h("salla-skeleton",{key:"checkpoint-line",height:"8px"}),h("div",{key:"checkpoint"},h("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 h(Host,{class:"s-conditional-offer-container"},h("div",{class:"s-conditional-offer-title-wrapper"},h("div",{class:"s-conditional-offer-title"},this.offer.title),this.offer.description?h("div",{class:"s-conditional-offer-subtitle"},this.offer.description," ",h("i",{class:"sicon-information"})):null),h("div",{class:"s-conditional-offer-progress-container"},this.offer.details.discounts.map((function(t,n){return h("div",{class:{"flex-1":n>0},key:t.min_spend},e.getCheckPointView(t,n))}))))};return e}();SallaConditionalOffer.style=sallaConditionalOfferCss;export{SallaConditionalOffer as salla_conditional_offer};
//# sourceMappingURL=salla-conditional-offer.entry.js.map