update fetch function

This commit is contained in:
znetsixe
2026-01-28 14:25:12 +01:00
parent 266a6ed4a3
commit a536c6ed5e

View File

@@ -91,7 +91,9 @@ class AssetMenu {
console.log('Initializing asset properties for ${nodeName}'); console.log('Initializing asset properties for ${nodeName}');
this.injectHtml(); this.injectHtml();
this.wireEvents(node); 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) { getDataInjectionCode(nodeName) {
return ` return `
// Asset data loader for ${nodeName} // 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 menuAsset = window.EVOLV.nodes.${nodeName}.menuData.asset || {};
const categories = menuAsset.categories || {}; const categories = menuAsset.categories || {};
const defaultCategory = menuAsset.defaultCategory || Object.keys(categories)[0] || null; 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 = { const elems = {
supplier: document.getElementById('node-input-supplier'), supplier: document.getElementById('node-input-supplier'),
type: document.getElementById('node-input-assetType'), type: document.getElementById('node-input-assetType'),
@@ -110,6 +119,79 @@ class AssetMenu {
unit: document.getElementById('node-input-unit') 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...') { function populate(selectEl, items = [], selectedValue, mapFn, placeholderText = 'Select...') {
const previous = selectEl.value; const previous = selectEl.value;
const mapper = typeof mapFn === 'function' 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(); const categoryKey = resolveCategoryKey();
node.category = categoryKey; const resolvedCategoryKey = categoryKey || defaultCategory;
const activeCategory = categoryKey ? categories[categoryKey] : null; 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 : []; const suppliers = activeCategory ? activeCategory.suppliers : [];
populate( populate(