@salla.sa/twilight-components
Version:
Salla Web Component
69 lines (66 loc) • 2.07 kB
JavaScript
/*!
* Crafted with ❤ by Salla
*/
import { h } from '@stencil/core/internal/client';
function nearestLowerMultiple(number, multipleOf = window.matchMedia('(min-width: 768px)').matches ? 3 : 2) {
return number < multipleOf ? number : number - (number % multipleOf);
}
function closeModals(cb = null) {
for (const element of document.querySelectorAll('salla-modal')) {
element.close();
}
cb && requestAnimationFrame(cb);
}
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 ?? [],
};
}
function brand_path(brand) {
return [brand.name.replace(/\s/g, '-'), `brand-${brand.id}`].join('/');
}
function isUnitOrder(point) {
for (const condition of (point.conditions ?? [])) {
if (condition.key?.endsWith('_FOR_UNIT')) {
return true;
}
}
return false;
}
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'));
}
export { nearestLowerMultiple as a, brand_path as b, closeModals as c, formattedPointsJsx as f, normalizeLoyaltyData as n };