From a536c6ed5ec97a79f328d7a19cf71faed165a21d Mon Sep 17 00:00:00 2001 From: znetsixe <73483679+znetsixe@users.noreply.github.com> Date: Wed, 28 Jan 2026 14:25:12 +0100 Subject: [PATCH] update fetch function --- src/menu/asset.js | 114 ++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 100 insertions(+), 14 deletions(-) diff --git a/src/menu/asset.js b/src/menu/asset.js index b47dc4d..c3f17c2 100644 --- a/src/menu/asset.js +++ b/src/menu/asset.js @@ -91,7 +91,9 @@ class AssetMenu { console.log('Initializing asset properties for ${nodeName}'); this.injectHtml(); this.wireEvents(node); - this.loadData(node); + this.loadData(node).catch((error) => + console.error('Asset menu load failed:', error) + ); }; `; } @@ -99,10 +101,17 @@ class AssetMenu { getDataInjectionCode(nodeName) { return ` // Asset data loader for ${nodeName} - window.EVOLV.nodes.${nodeName}.assetMenu.loadData = function(node) { + window.EVOLV.nodes.${nodeName}.assetMenu.loadData = async function(node) { const menuAsset = window.EVOLV.nodes.${nodeName}.menuData.asset || {}; const categories = menuAsset.categories || {}; const defaultCategory = menuAsset.defaultCategory || Object.keys(categories)[0] || null; + const apiConfig = menuAsset.apiConfig || { + url: 'http://localhost:8000/apis/products/PLC/integration/', + headers: { + accept: 'application/json', + Authorization: '4a49332a-fc3e-11f0-bf0a-9457f8d645d9' + } + }; const elems = { supplier: document.getElementById('node-input-supplier'), type: document.getElementById('node-input-assetType'), @@ -110,6 +119,79 @@ class AssetMenu { unit: document.getElementById('node-input-unit') }; + function resolveCategoryKey() { + if (node.softwareType && categories[node.softwareType]) { + return node.softwareType; + } + if (node.category && categories[node.category]) { + return node.category; + } + return defaultCategory; + } + + function normalizeModel(model = {}) { + return { + id: model.id || model.name, + name: model.name, + units: model.units || [] + }; + } + + function normalizeType(type = {}) { + return { + id: type.id || type.name, + name: type.name, + models: Array.isArray(type.models) + ? type.models.map(normalizeModel) + : [] + }; + } + + function normalizeSupplier(supplier = {}) { + const types = (supplier.categories || []).reduce((acc, category) => { + const categoryTypes = Array.isArray(category.types) + ? category.types.map(normalizeType) + : []; + return acc.concat(categoryTypes); + }, []); + return { + id: supplier.id || supplier.name, + name: supplier.name, + types + }; + } + + function normalizeApiCategory(key, label, suppliers = []) { + const normalizedSuppliers = suppliers + .map(normalizeSupplier) + .filter((supplier) => supplier.types && supplier.types.length); + if (!normalizedSuppliers.length) { + return null; + } + return { + softwareType: key, + label: label || key, + suppliers: normalizedSuppliers + }; + } + + async function fetchCategoryFromApi(key) { + if (!apiConfig.url || !key) { + return null; + } + const response = await fetch(apiConfig.url, { + headers: apiConfig.headers || {} + }); + if (!response.ok) { + throw new Error('Asset API request failed: ' + response.status); + } + const payload = await response.json(); + if (!payload || payload.success === false || !Array.isArray(payload.data)) { + throw new Error(payload?.message || 'Unexpected asset API response'); + } + return normalizeApiCategory(key, node.softwareType || key, payload.data); + } + function populate(selectEl, items = [], selectedValue, mapFn, placeholderText = 'Select...') { const previous = selectEl.value; const mapper = typeof mapFn === 'function' @@ -149,19 +231,23 @@ class AssetMenu { } } - const resolveCategoryKey = () => { - if (node.softwareType && categories[node.softwareType]) { - return node.softwareType; - } - if (node.category && categories[node.category]) { - return node.category; - } - return defaultCategory; - }; - const categoryKey = resolveCategoryKey(); - node.category = categoryKey; - const activeCategory = categoryKey ? categories[categoryKey] : null; + const resolvedCategoryKey = categoryKey || defaultCategory; + let activeCategory = resolvedCategoryKey ? categories[resolvedCategoryKey] : null; + + if (resolvedCategoryKey) { + node.category = resolvedCategoryKey; + } + + try { + const apiCategory = await fetchCategoryFromApi(resolvedCategoryKey); + if (apiCategory) { + categories[resolvedCategoryKey] = apiCategory; + activeCategory = apiCategory; + } + } catch (error) { + console.warn('[AssetMenu] API lookup failed for ${nodeName}, using local asset data', error); + } const suppliers = activeCategory ? activeCategory.suppliers : []; populate(