@salla.sa/twilight-components
Version:
Salla Web Component
70 lines (65 loc) • 2.36 kB
JavaScript
/*!
* Crafted with ❤ by Salla
*/
;
var index = require('./index-CedylOEg.js');
const sallaMultipleBundleProductCss = ":host{display:block}";
const SallaMultipleBundleProduct = class {
constructor(hostRef) {
index.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 index.h("salla-multiple-bundle-product-cart", { sections: this.parsedSections });
}
renderDetailsPage() {
return index.h("salla-multiple-bundle-product-details", { sections: this.parsedSections });
}
componentWillLoad() {
this.parseProducts(this.bundleSections);
}
render() {
return (index.h(index.Host, { key: '2a4510d1ccd3dc840d25a288cb0788a7dd23672c', class: "s-multiple-bundle-product-wrapper" }, this.isCartPage() ? this.renderCartPage() : this.renderDetailsPage()));
}
static get watchers() { return {
"bundleSections": ["parseProducts"]
}; }
};
SallaMultipleBundleProduct.style = sallaMultipleBundleProductCss;
exports.salla_multiple_bundle_product = SallaMultipleBundleProduct;