UNPKG

@salla.sa/twilight-components

Version:
217 lines (216 loc) 11.3 kB
/*! * Crafted with ❤ by Salla */ import { h } from "@stencil/core"; import SpecialDiscountIcon from "../../assets/svg/special-discount.svg"; import Tag from "../../assets/svg/tag.svg"; import Cart2 from "../../assets/svg/cart2.svg"; /** * @slot header - The top of the popup, has replaceable props `{name}`, `{message}`. * @slot product - Replaces product card, has replaceable props `{name}`, `{url}`, `{image}`, `{price}`. * @slot category - Replaces Category badge, has replaceable props `{name}`, `{url}`. */ export class SallaOfferModal { constructor() { var _a, _b; this.offer = null; this.offer_name = undefined; this.offer_message = undefined; this.hasError = false; this.errorMessage = undefined; this.productID = undefined; this.offer_type = undefined; this.translationLoaded = false; this.addToCartLabel = salla.lang.get("pages.cart.add_to_cart"); salla.event.on('offer-modal::open', product_id => this.open(product_id)); salla.lang.onLoaded(() => { this.addToCartLabel = salla.lang.get("pages.cart.add_to_cart"); this.translationLoaded = true; }); this.categorySlot = ((_a = this.host.querySelector('[slot="category"]')) === null || _a === void 0 ? void 0 : _a.innerHTML) || `<span class="s-offer-modal-badge-icon">{tagIcon}</span><span class="s-offer-modal-badge-text">{name}</span>`; this.productSlot = ((_b = this.host.querySelector('[slot="product"]')) === null || _b === void 0 ? void 0 : _b.innerHTML) || this.defaultProductSlot(); salla.event.on('offer-modal::open', product_id => this.open(product_id)); salla.product.event.onOfferExisted(offer => { if (salla.storage.get('remember-offer-' + offer.id)) { salla.log('User selected to don\'t show this offer again.'); return; } this.open(offer.product_id); }); } /** * Show the available offers for the product * @param product_id */ async open(product_id) { this.productID = product_id; //TODO:: make sure there is only one offer this.hasError = false; this.modal.open(); return await salla.api.withoutNotifier(() => salla.product.offers(product_id)) .then(response => this.showOffer(response.data[0])) .catch(e => { var _a, _b, _c, _d; this.hasError = true; this.errorMessage = ((_c = (_b = (_a = e.response) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.error) === null || _c === void 0 ? void 0 : _c.message) || ((_d = e.response) === null || _d === void 0 ? void 0 : _d.data); }) .finally(() => setTimeout(() => this.modal.stopLoading(), 1000)); } /** * Show offer details * @param {Offer} offer */ async showOffer(offer) { var _a, _b; this.offer = offer; this.offer_name = offer.name; this.offer_message = offer.message; if (this.offer.get.discounts_table) { this.offer_type = 'discounts-table'; } else if ((_a = this.offer.get.products) === null || _a === void 0 ? void 0 : _a.length) { this.offer_type = 'products'; } else if ((_b = this.offer.get.categories) === null || _b === void 0 ? void 0 : _b.length) { this.offer_type = 'categories'; } this.modal.setTitle(this.offer_name); } rememberMe(event) { salla.storage.set('remember-offer-' + this.offer.id, event.target.checked); } addToCart(qty) { //todo:: add enhancement, to cover the previous quantity, because if the discount to add two, user already added one before. salla.api.withoutNotifier(() => salla.cart.quickAdd(this.productID, qty, this.offer_type)).then(() => this.modal.close()); } getOfferContent() { var _a, _b, _c, _d, _e, _f, _g, _h; if (this.offer.get.discounts_table) { return h("div", { class: "s-offer-modal-discount-table" }, h("table", null, h("tbody", null, (_a = this.offer.get.discounts_table) === null || _a === void 0 ? void 0 : _a.map(discount => h("tr", null, h("td", null, discount.text), h("td", { class: "s-offer-modal-discount-table-cell" }, h("salla-button", { fill: "outline", shape: "btn", color: "primary", size: "medium", width: "normal", onClick: () => this.addToCart(discount.quantity) }, this.addToCartLabel))))))); } else if ((_b = this.offer.get.products) === null || _b === void 0 ? void 0 : _b.length) { return h("salla-slider", { type: "carousel", class: { "s-offer-modal-slider-centered": ((_c = this.offer.get.products) === null || _c === void 0 ? void 0 : _c.length) <= 2, "s-offer-modal-slider": true }, id: "offer-modal-slider", "controls-outer": true, "show-controls": ((_d = this.offer.get.products) === null || _d === void 0 ? void 0 : _d.length) <= 2 ? 'false' : 'true' }, h("div", { slot: 'items' }, (_e = this.offer.get.products) === null || _e === void 0 ? void 0 : _e.map(product => h("div", { class: { "s-offer-modal-product": true, "s-offer-modal-slider-item": true, "s-offer-modal-not-available": !product.is_available }, id: 'product_' + product.id, innerHTML: this.productSlot .replace(/\{name\}/g, product.name) .replace(/\{url\}/g, product.url) .replace(/\{image\}/g, product.thumbnail) .replace(/\{price\}/g, product.has_special_price ? '<span class="s-offer-modal-product-sale-price">' + salla.money(product.price) + '</span><span class="s-offer-modal-product-old-price">' + salla.money(product.regular_price) + '</span>' : salla.money(product.price)) }, h("div", { class: "s-offer-modal-btn-wrap" }, h("salla-button", { width: "wide", fill: 'outline', "data-id": product.id, disabled: !product.is_available, "loader-position": "center", onClick: this.addItem }, product.is_available ? salla.lang.get('pages.cart.add_to_cart') : salla.lang.get('pages.products.out_of_stock'))))))); } else if ((_f = this.offer.get.categories) === null || _f === void 0 ? void 0 : _f.length) { return h("salla-slider", { type: "carousel", class: { "s-offer-modal-slider-centered": ((_g = this.offer.get.categories) === null || _g === void 0 ? void 0 : _g.length) <= 2, "s-offer-modal-slider": true }, id: "offer-modal-slider", "controls-outer": true, "show-controls": ((_h = this.offer.get.categories) === null || _h === void 0 ? void 0 : _h.length) <= 2 ? 'false' : 'true' }, h("div", { slot: 'items' }, this.offer.get.categories.map(category => h("a", { href: category.urls.customer, class: "s-offer-modal-badge s-offer-modal-slider-item s-offer-modal-cat-item", innerHTML: this.categorySlot .replace(/\{tagIcon\}/g, Tag) .replace(/\{name\}/g, category.name) .replace(/\{url\}/g, category.urls.customer) })))); } } //todo:: pass event then use sallaButton from it addItem() { // this here, is sallaButton this['load'](); return salla.cart.api .quickAdd(this['dataset'].id) .finally(() => this['stop']()); } defaultProductSlot() { return '<a href={url} class="s-offer-modal-product-image-wrap"><img class="s-offer-modal-product-image" src="{image}" /></a>' + '<div class="s-offer-modal-product-info">' + ' <a href={url} class="s-offer-modal-product-name">{name}</a>' + ' <div class="s-offer-modal-product-price">{price}</div>' + '</div>'; } render() { return h("salla-modal", { "has-skeleton": true, "sub-title": this.offer_message, ref: modal => this.modal = modal, isLoading: true, class: `s-offer-modal-type-${this.offer_type ? this.offer_type : ''}` }, h("div", { slot: 'loading' }, h("div", { class: "s-offer-modal-skeleton" }, h("div", { class: "s-offer-modal-skeleton-header" }, h("salla-skeleton", { type: 'circle', height: '80px', width: '80px' }), h("salla-skeleton", { height: '15px', width: '50%' }), h("salla-skeleton", { height: '10px', width: '30%' })), h("div", { class: "s-offer-modal-skeleton-items" }, [...Array(3)].map(() => h("div", { class: "s-offer-modal-skeleton-item" }, h("salla-skeleton", { height: '9rem' }), h("div", { class: "s-offer-modal-skeleton-item-title" }, h("salla-skeleton", { height: '15px', width: '100%' })), h("div", { class: "s-offer-modal-skeleton-item-subtitle" }, h("salla-skeleton", { height: '9px', width: '50%' }), h("div", { innerHTML: Cart2 }))))), h("div", { class: "s-offer-modal-skeleton-footer" }, h("salla-skeleton", { height: '15px', width: '50%' }), h("salla-skeleton", { height: '15px', width: '30%' })))), !this.hasError && this.offer !== null ? [h("span", { slot: 'icon', class: "s-offer-modal-header-icon", innerHTML: SpecialDiscountIcon }), this.getOfferContent(), h("div", { class: "s-offer-modal-footer", slot: "footer" }, this.offer.formatted_date ? h("p", { class: "s-offer-modal-expiry" }, salla.lang.get('pages.products.offer_expires_in'), " ", this.offer.formatted_date) : '', h("label", { class: "s-offer-modal-remember-label" }, h("input", { type: "checkbox", onChange: e => this.rememberMe(e), class: "s-offer-modal-remember-input" }), "\u00A0 ", salla.lang.get('common.elements.remember_my_choice'))), ] : h("salla-placeholder", { class: "s-loyalty-placeholder", alignment: "center" }, !!this.errorMessage ? h("span", { slot: "description" }, this.errorMessage) : '')); } static get is() { return "salla-offer-modal"; } static get originalStyleUrls() { return { "$": ["salla-offer-modal.scss"] }; } static get styleUrls() { return { "$": ["salla-offer-modal.css"] }; } static get states() { return { "offer": {}, "offer_name": {}, "offer_message": {}, "hasError": {}, "errorMessage": {}, "productID": {}, "offer_type": {}, "translationLoaded": {}, "addToCartLabel": {} }; } static get methods() { return { "open": { "complexType": { "signature": "(product_id: number) => Promise<any>", "parameters": [{ "tags": [{ "name": "param", "text": "product_id" }], "text": "" }], "references": { "Promise": { "location": "global", "id": "global::Promise" } }, "return": "Promise<any>" }, "docs": { "text": "Show the available offers for the product", "tags": [{ "name": "param", "text": "product_id" }] } }, "showOffer": { "complexType": { "signature": "(offer: any) => Promise<void>", "parameters": [{ "tags": [{ "name": "param", "text": "offer" }], "text": "" }], "references": { "Promise": { "location": "global", "id": "global::Promise" } }, "return": "Promise<void>" }, "docs": { "text": "Show offer details", "tags": [{ "name": "param", "text": "offer" }] } } }; } static get elementRef() { return "host"; } } //# sourceMappingURL=salla-offer-modal.js.map