@salla.sa/twilight-components
Version:
Salla Web Component
54 lines (53 loc) • 1.84 kB
JavaScript
/*!
* Crafted with ❤ by Salla
*/
import { h } from "@stencil/core";
export function nearestLowerMultiple(number, multipleOf = window.matchMedia('(min-width: 768px)').matches ? 3 : 2) {
return number < multipleOf ? number : number - (number % multipleOf);
}
export function closeModals(cb = null) {
for (const element of document.querySelectorAll('salla-modal')) {
element.close();
}
cb && requestAnimationFrame(cb);
}
export function normalizeLoyaltyData(data) {
const groups = (data.prizes || []).map((group) => {
return {
...group,
items: (group.items || []).map((item) => ({ ...item, group_title: group.title, group_type: group.type })),
};
});
const all = {
type: 'all',
title: salla.lang.get('pages.loyalty_program.all'),
items: groups.reduce((acc, group) => acc.concat(group.items), []),
};
groups.unshift(all);
return {
...data,
prizes: groups,
prizes_count: all.items.length,
points: data.points ?? [],
};
}
export function brand_path(brand) {
return [brand.name.replace(/\s/g, '-'), `brand-${brand.id}`].join('/');
}
export function isUnitOrder(point) {
for (const condition of (point.conditions ?? [])) {
if (condition.key?.endsWith('_FOR_UNIT')) {
return true;
}
}
return false;
}
export function formattedPointsJsx(value) {
if (typeof value === 'number') {
return h("span", null, value, " ", salla.lang.get('pages.loyalty_program.point'));
}
if (isUnitOrder(value)) {
return h("span", null, "1 ", salla.config.currency().symbol, " = ", value.points, " ", salla.lang.get('pages.loyalty_program.point'));
}
return h("span", null, value.points, " ", salla.lang.get('pages.loyalty_program.point'));
}