@salla.sa/twilight-components
Version:
Salla Web Component
68 lines (64 loc) • 2.35 kB
JavaScript
/*!
* Crafted with ❤ by Salla
*/
import { r as registerInstance, h, H as Host } from './index-Dbv0I4re.js';
const sallaMultipleBundleProductCss = ":host{display:block}";
const SallaMultipleBundleProduct = class {
constructor(hostRef) {
registerInstance(this, hostRef);
this.parsedSections = [];
}
parseProducts(newValue) {
let sections = [];
if (typeof newValue === 'string') {
try {
const parsed = JSON.parse(newValue);
sections = this.extractSections(parsed);
}
catch (e) {
console.error('Invalid JSON passed to bundleSections prop:', newValue);
sections = [];
}
}
else if (newValue) {
sections = this.extractSections(newValue);
}
this.parsedSections = sections;
}
extractSections(data) {
// Handle new nested structure: data.bundle.sections
if (data.bundle && data.bundle.sections && Array.isArray(data.bundle.sections)) {
return data.bundle.sections;
}
// Handle old flat structure: data is directly an array of sections
if (Array.isArray(data)) {
return data;
}
// Handle case where data.sections exists at root level
if (data.sections && Array.isArray(data.sections)) {
return data.sections;
}
console.warn('No valid sections found in data:', data);
return [];
}
isCartPage() {
return salla.url.is_page('cart');
}
renderCartPage() {
return h("salla-multiple-bundle-product-cart", { sections: this.parsedSections });
}
renderDetailsPage() {
return h("salla-multiple-bundle-product-details", { sections: this.parsedSections });
}
componentWillLoad() {
this.parseProducts(this.bundleSections);
}
render() {
return (h(Host, { key: 'd2723aa4c40e7585adbc5e128035b03dc9920fbc', class: "s-multiple-bundle-product-wrapper" }, this.isCartPage() ? this.renderCartPage() : this.renderDetailsPage()));
}
static get watchers() { return {
"bundleSections": ["parseProducts"]
}; }
};
SallaMultipleBundleProduct.style = sallaMultipleBundleProductCss;
export { SallaMultipleBundleProduct as salla_multiple_bundle_product };