/** * 3D Print Demand dashboard panel for InvenTree. * * Fetches aggregated demand data from the plugin API and renders * a colour-coded table showing stock vs demand for every 3D-printed part. */ export function renderDashboardItem(context) { const target = context.target; target.innerHTML = 'Loading 3D print demand data...'; const headers = { 'Accept': 'application/json', }; // Include CSRF token if available const csrfToken = getCookie('csrftoken'); if (csrfToken) { headers['X-CSRFToken'] = csrfToken; } fetch('/plugin/print-demand/api/demand/', { headers, credentials: 'same-origin' }) .then(response => { if (!response.ok) { return response.json().then(data => { throw new Error(data.error || response.statusText); }); } return response.json(); }) .then(data => { if (!data.length) { target.innerHTML = 'No parts found in the configured category.'; return; } target.innerHTML = buildTable(data); }) .catch(err => { target.innerHTML = `Error: ${escapeHtml(err.message)}`; }); } function buildTable(parts) { const rows = parts.map(p => { const deficitStyle = p.deficit < 0 ? 'color:#c00;font-weight:bold;' : (p.deficit > 0 ? 'color:#080;' : ''); return `
| Part | In Stock | Allocated | Available | Required | Deficit |
|---|