@salla.sa/twilight-components
Version:
Salla Web Component
4 lines • 4.56 kB
JavaScript
/*!
* Crafted with ❤ by Salla
*/
import{r as s,h as t}from"./p-BHYtfMwX.js";import{T as o}from"./p-CJUR4sUM.js";import{i as n}from"./p-DqDX5na4.js";const e=class{constructor(t){s(this,t),this.coupon=null,this.isLoading=!0,this.titlePercentTpl="",this.titleFixedTpl="",this.badgeLabel="",this.minSpendTpl="",this.expiresTpl="",this.shopNowLabel="",this.copyLabel="",this.copiedLabel=""}componentWillLoad(){Salla.onReady().then((()=>Salla.lang.onLoaded())).then((()=>(this.loadTranslations(),this.loadCoupon()))).catch((()=>{this.isLoading=!1}))}loadTranslations(){this.titlePercentTpl=Salla.lang.get("pages.thank_you.next_order_coupon_title_percent")||"",this.titleFixedTpl=Salla.lang.get("pages.thank_you.next_order_coupon_title_fixed")||"",this.badgeLabel=Salla.lang.get("pages.thank_you.next_order_coupon_badge")||"",this.minSpendTpl=Salla.lang.get("pages.thank_you.next_order_coupon_min_spend")||"",this.expiresTpl=Salla.lang.get("pages.thank_you.next_order_coupon_expires")||"",this.shopNowLabel=Salla.lang.get("pages.thank_you.next_order_coupon_shop_now")||"",this.copyLabel=Salla.lang.get("common.elements.copy")||"",this.copiedLabel=Salla.lang.get("pages.thank_you.next_order_coupon_copied")||""}async loadCoupon(){try{const s=await salla.api.request("coupons",{params:{type:"thankyou"}}),t=s?.data,o=Array.isArray(t)?t:Array.isArray(t?.data)?t.data:[];this.coupon=o[0]??null}catch{this.coupon=null}finally{this.isLoading=!1}}async copyCode(){if(this.coupon)try{await navigator.clipboard.writeText(this.coupon.code),salla.notify.success(this.copiedLabel),salla.event.dispatch("salla::next-order-coupon.code.copied",{code:this.coupon.code})}catch{}}isPercentage(){return"P"===this.coupon?.type||"percentage"===this.coupon?.type}formatAmount(){if(!this.coupon)return"";if(this.isPercentage())return`${salla.helpers.number(this.coupon.amount)}%`;const s=this.coupon.amount;return t("span","object"==typeof s&&s.amount?{innerHTML:salla.money(s.amount,s.currency)}:{innerHTML:salla.money(s)})}formatMinSpend(){const s=this.coupon?.minimum_amount;return s&&("object"!=typeof s||s.amount)?"string"==typeof s?s.replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">").replace(/"/g,"""):salla.money(s.amount,s.currency):""}formatExpiry(){const s=this.coupon?.expiry_date;return s?new Date(s).toLocaleDateString(Salla.lang.locale||"en-GB"):""}renderSkeleton(){return t("div",{class:"s-next-order-coupon-wrapper s-next-order-coupon-skeleton"},t("div",{class:"s-next-order-coupon-inner"},t("div",{class:"s-next-order-coupon-info"},t("salla-skeleton",{type:"normal",width:"48px",height:"48px"}),t("div",{class:"s-next-order-coupon-details"},t("salla-skeleton",{height:"14px",width:"220px"}),t("salla-skeleton",{height:"12px",width:"140px"}))),t("div",{class:"s-next-order-coupon-actions"},t("salla-skeleton",{height:"40px",width:"140px"}),t("salla-skeleton",{height:"40px",width:"100px"}))))}renderTitle(){const s=this.formatAmount(),o=this.isPercentage()?this.titlePercentTpl:this.titleFixedTpl,n=this.isPercentage()?":amount%":":amount",e=o.split(n);return t("p",{class:"s-next-order-coupon-title"},e[0],t("span",{class:"s-next-order-coupon-amount"},s),e[1]||"")}render(){if(this.isLoading)return this.renderSkeleton();if(!this.coupon)return null;const s=this.formatMinSpend(),e=this.formatExpiry();return t("div",{class:"s-next-order-coupon-wrapper"},t("div",{class:"s-next-order-coupon-inner"},t("div",{class:"s-next-order-coupon-info"},t("div",{class:"s-next-order-coupon-icon-wrap"},t("span",{class:"s-next-order-coupon-icon-bg"}),t("span",{class:"s-next-order-coupon-icon",innerHTML:o})),t("div",{class:"s-next-order-coupon-details"},t("div",{class:"s-next-order-coupon-title-row"},this.renderTitle(),t("span",{class:"s-next-order-coupon-badge"},t("span",{class:"s-next-order-coupon-badge-bg"}),t("span",{class:"s-next-order-coupon-badge-text"},this.badgeLabel))),(s||e)&&t("p",{class:"s-next-order-coupon-meta"},s&&t("span",{innerHTML:this.minSpendTpl.replace(":amount",s)}),e&&t("span",null,this.expiresTpl.replace(":date",e))))),t("div",{class:"s-next-order-coupon-actions"},t("div",{class:"s-next-order-coupon-code-box"},t("span",{class:"s-next-order-coupon-code-text"},this.coupon.code),t("salla-button",{shape:"link",class:"s-next-order-coupon-copy-btn",title:this.copyLabel,onClick:()=>this.copyCode()},t("span",{class:"s-next-order-coupon-copy-icon",innerHTML:n}))),t("salla-button",{href:salla.url.get(""),class:"s-next-order-coupon-shop-btn"},this.shopNowLabel))))}};e.style=":host{display:block}";export{e as salla_next_order_coupon}