@salla.sa/twilight-components
Version:
Salla Web Component
216 lines (215 loc) • 13.5 kB
JavaScript
/*!
* Crafted with ❤ by Salla
*/
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
r = Reflect.decorate(decorators, target, key, desc);
else
for (var i = decorators.length - 1; i >= 0; i--)
if (d = decorators[i])
r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
import { h } from "@stencil/core";
import { brand_path, closeModals, formattedPointsJsx } from "./functions";
import SIGift from "../../assets/svg/gift.svg";
import SITag from "../../assets/svg/tag.svg";
import SICheck from "../../assets/svg/check.svg";
import SICancel from "../../assets/svg/cancel.svg";
import { TrackedPromise } from "../../Helpers/tracked-promise";
const c = (...args) => args.map(i => 's-loyalty-program-' + i).join(' ');
export class SallaRewardDetails {
constructor() {
this.refreshKey = Date.now();
}
handlePrizeChange() {
this.refreshKey = Date.now();
}
componentWillLoad() {
if (this.category.status === 'idle') {
this.category();
}
if (this.brand.status === 'idle') {
this.brand();
}
}
getTermsAndConditions() {
const minimumAmount = Number((this.prize.order_minimum_amount || '0').split('.')[0]) || 0;
const maximumAmount = Number((this.prize.coupon_maximum_amount || '0').split('.')[0]) || 0;
const termsAndConditions = [];
if (minimumAmount) {
termsAndConditions.push([
salla.lang.get('pages.loyalty_program.order_minimum_amount'),
`${minimumAmount} ${salla.lang.get(`currency.${(salla.config.get('user.currency_code') || '').toLowerCase()}`)}`,
]);
}
if (maximumAmount) {
termsAndConditions.push([
salla.lang.get('pages.loyalty_program.coupon_maximum_amount'),
`${maximumAmount} ${salla.lang.get(`currency.${(salla.config.get('user.currency_code') || '').toLowerCase()}`)}`,
]);
}
return termsAndConditions;
}
findItems(ids) {
if (!ids?.length)
return [];
const brands = this.brand.data;
const categories = this.category.data;
// id_ is the numeric ID from the API (e.g. 740744759); id is a string hash (e.g. "zEVZGx"). included_category_ids/included_brand_ids are number[], so id_ is correct here.
return brands.concat(categories).filter(i => ids.includes(i.id_));
}
getIncludedItems(items, ids) {
if (items?.length) {
return items;
}
if (ids?.length) {
return this.findItems(ids);
}
return [];
}
getProductIds(ids) {
return ids || [];
}
getItemsMarkers(key) {
if (key === 'both') {
return this.getItemsMarkers('included') + this.getItemsMarkers('excluded');
}
return [
this.prize[`${key}_product_ids`]?.length || 0,
this.prize[`${key}_brand_ids`]?.length || 0,
this.prize[`${key}_category_ids`]?.length || 0,
].map(Boolean).map(Number).join('');
}
getItemsMarkersText() {
// Check if prize type is free_shipping
if (this.prize.key === 'FREE_SHIPPING') {
return salla.lang.get('pages.loyalty_program.redeem_for_free_shipping');
}
switch (this.getItemsMarkers('included')) {
case '001':
return salla.lang.get('pages.loyalty_program.redeem_on_specific_products');
case '010':
return salla.lang.get('pages.loyalty_program.redeem_on_specific_brands');
case '100':
return salla.lang.get('pages.loyalty_program.redeem_on_specific_categories');
case '000':
return salla.lang.get('pages.loyalty_program.redeem_on_all_products');
default:
return salla.lang.get('pages.loyalty_program.redeem_on_specific_products');
}
}
getCategoriesJsx(items, title) {
return !items.length ? '' : h("div", { class: c('reward-details-categories-wrapper') }, h("div", { class: c('reward-details-categories') }, h("h2", { class: c('reward-details-categories-heading') }, title), h("ul", { class: c('reward-details-categories-list') }, items.map((category) => {
return (h("li", null, h("a", { href: category.urls?.customer || category.url || category.lingual_urls?.[0], class: c('reward-details-categories-item') }, h("span", { innerHTML: SITag }), h("span", { class: c('reward-details-categories-item-content') }, category.name))));
}))));
}
getBrandsJsx(items, title) {
return !items.length ? '' : h("div", { class: c('reward-details-items-brands', 'slider-arrows-edges') }, h("div", null, h("h2", null, title), h("salla-slider", { id: `brands-${items.map(({ id }) => id).join('-')}`, "slider-config": '{"loop":false,"slidesPerView":"auto","spaceBetween":10}' }, h("div", { slot: "items" }, items.map((brand) => (h("a", { href: salla.url.get(brand_path(brand)), class: c('reward-brand-item') }, h("img", { src: brand.logo || salla.url.cdn('images/s-empty.png'), alt: "" }))))))));
}
getProductsJsx(items, title) {
return !items?.length ? '' : h("div", { class: "s-loyalty-program-reward-details-items-products s-loyalty-program-slider-arrows-edges" }, h("h2", null, title), h("salla-products-slider", { source: "selected", sourceValue: JSON.stringify(items), "slider-config": '{"loop":false,"slidesPerView":"auto","freeMode":true,"spaceBetween":0,"breakpoints":{"768":{"slidesPerView":3,"freeMode":false,"speed":300}}}' }));
}
render() {
const termsAndConditions = this.getTermsAndConditions();
return (h("div", { key: '280d646eff094c16596ca31ee57f4bd104958f8c', class: "s-loyalty-program-reward-modal" }, h("div", { key: 'a39d376f170684f754c4357dc64d02b5ffbb1ed2', class: "s-loyalty-program-points-modal-header" }, h("h1", { key: '654e061c8852461a5248248ed2d95ef02a8a4711', class: "s-loyalty-program-points-modal-info" }, !salla.url.is_page('cart') ? ''
: h("button", { class: "s-loyalty-program-back-btn", onClick: () => closeModals(() => salla.event.dispatch('modal::open', 'loyalty-rewards-modal')) }, h("span", { class: {
'sicon-arrow-right': document.documentElement.getAttribute('dir') === 'rtl',
'sicon-arrow-left': document.documentElement.getAttribute('dir') === 'ltr',
} })), this.prize.name)), h("div", { key: '8dce07ce81b0d6879efc0cba88523b6d3daf42bb', class: "s-loyalty-program-modal-scrollable-content" }, h("div", { key: 'f288d7483911af0bbc17a824ee4397ad0b3ef367', class: "s-loyalty-program-reward-details-header" }, h("div", { key: '87686174a3a8c17558d0a97180cb4756a1edbefe', class: "s-loyalty-program-aspect-container" }, h("img", { key: '31461e1503c50715f160e75cf7ffad59939fb93c', src: this.prize.image, alt: this.prize.name })), h("salla-badge", { key: 'ed1fc08260885fa865279a9d2fc6af35c01e2bdc', size: "sm", bg: "light" }, h("span", { key: '26eec953f9c5f3fa5aa118828faec50a4477e013', innerHTML: SIGift }), h("span", { key: 'db3bf908be0f28534e121da9e692d801d2ee6cf9', class: "s-loyalty-program-points-badge-points" }, formattedPointsJsx(this.prize.cost_points)))), !termsAndConditions.length ? '' : h("div", { class: "s-loyalty-program-terms-and-conditions" }, h("h1", { class: "s-loyalty-program-terms-and-conditions-heading" }, salla.lang.get('pages.loyalty_program.terms_and_conditions')), h("ul", { class: "s-loyalty-program-terms-and-conditions-list" }, termsAndConditions.map(([title, points]) => (h("li", { class: "s-loyalty-program-terms-and-conditions-item" }, h("span", { class: "s-loyalty-program-terms-and-conditions-content" }, title), h("span", { class: "s-loyalty-program-terms-and-conditions-content" }, points)))))), h("div", { key: 'bfdc4d22117ae86f3bb2140546b4b5a8ecd35f8e', class: "s-loyalty-program-reward-info-message" }, h("h1", { key: 'f03b30c0372fd83e4fa7815bbd76d307c6fbe3c1', class: "s-loyalty-program-reward-info-message-heading" }, salla.lang.get('pages.loyalty_program.how_to_benefit_from_this_reward')), h("p", { key: '245acd0b8438230ff5d503e5819abb581f0ba14a', class: "s-loyalty-program-reward-info-message-content" }, this.getItemsMarkersText())), (this.prize.key === 'FREE_SHIPPING' || this.getItemsMarkers('both') === '000000') ? '' : h("div", { class: "s-loyalty-program-reward-details" }, h("salla-tabs", { key: this.refreshKey }, this.getItemsMarkers('included') === '000' ? '' : [
h("salla-tab-header", { slot: "header", name: "included", style: {
display: this.getItemsMarkers('excluded') === '000' ? 'none' : 'block',
} }, h("span", { class: "s-loyalty-program-reward-details-tab-icon", innerHTML: SICheck }), h("span", null, salla.lang.get(`pages.loyalty_program.included`))),
h("salla-tab-content", { slot: "content", name: "included" }, h("div", { class: "s-loyalty-program-reward-details-items-wrapper" }, this.getCategoriesJsx(this.getIncludedItems(this.prize.categories, this.prize.included_category_ids), salla.lang.get('pages.loyalty_program.included_categories')), this.getBrandsJsx(this.getIncludedItems(this.prize.brands, this.prize.included_brand_ids), salla.lang.get('pages.loyalty_program.included_brands')), this.getProductsJsx(this.getProductIds(this.prize.included_product_ids), salla.lang.get('pages.loyalty_program.included_products')))),
], this.getItemsMarkers('excluded') === '000' ? '' : [
h("salla-tab-header", { slot: "header", name: "excluded", style: {
display: this.getItemsMarkers('included') === '000' ? 'none' : 'block',
} }, h("span", { class: "s-loyalty-program-reward-details-tab-icon", innerHTML: SICancel }), h("span", null, salla.lang.get(`pages.loyalty_program.excluded`))),
h("salla-tab-content", { slot: "content", name: "excluded" }, h("div", { class: "s-loyalty-program-reward-details-items-wrapper" }, this.getCategoriesJsx(this.getIncludedItems(this.prize.excludedCategories, this.prize.excluded_category_ids), salla.lang.get('pages.loyalty_program.excluded_categories')), this.getBrandsJsx(this.getIncludedItems(this.prize.excludedBrands, this.prize.excluded_brand_ids), salla.lang.get('pages.loyalty_program.excluded_brands')), this.getProductsJsx(this.getProductIds(this.prize.excluded_product_ids), salla.lang.get('pages.loyalty_program.excluded_products')))),
]))), h("salla-reward-action", { key: 'e129546afde7da7fe66e250ce4de67d4523727be', onActionClick: e => this.actionClick.emit(e.detail), prize: this.prize })));
}
static get is() { return "salla-reward-details"; }
static get originalStyleUrls() {
return {
"$": ["salla-loyalty-program.scss"]
};
}
static get styleUrls() {
return {
"$": ["salla-loyalty-program.css"]
};
}
static get properties() {
return {
"prize": {
"type": "unknown",
"attribute": "prize",
"mutable": false,
"complexType": {
"original": "Prize",
"resolved": "Prize",
"references": {
"Prize": {
"location": "import",
"path": "./interfaces",
"id": "src/components/salla-loyalty-program/interfaces.ts::Prize"
}
}
},
"required": false,
"optional": false,
"docs": {
"tags": [],
"text": ""
},
"getter": false,
"setter": false
}
};
}
static get states() {
return {
"refreshKey": {}
};
}
static get events() {
return [{
"method": "actionClick",
"name": "actionClick",
"bubbles": true,
"cancelable": true,
"composed": true,
"docs": {
"tags": [],
"text": ""
},
"complexType": {
"original": "MouseEvent",
"resolved": "MouseEvent",
"references": {
"MouseEvent": {
"location": "global",
"id": "global::MouseEvent"
}
}
}
}];
}
static get watchers() {
return [{
"propName": "prize",
"methodName": "handlePrizeChange"
}];
}
}
__decorate([
TrackedPromise(async () => {
return (await salla.product.api.categories()).data;
}, { initialData: [] })
], SallaRewardDetails.prototype, "category", void 0);
__decorate([
TrackedPromise(async () => {
return (await salla.api.request('/brands')).data;
}, { initialData: [] })
], SallaRewardDetails.prototype, "brand", void 0);