pediatric-ai-scribe-v3/public/js/drugs-loader.js

29 lines
1.2 KiB
JavaScript

// ============================================================
// drugs-loader.js
// Fetches /data/drugs.json at page load and exposes:
// window._DRUGS — the parsed JSON (after fetch resolves)
// window._DRUGS_READY — a Promise that resolves to the JSON
//
// Calculator functions in calculators.js look up drugs via a small
// helper (pickDrug) and fall back to hardcoded inline data if the
// JSON isn't loaded yet. This means the app keeps working even if
// the fetch fails — the JSON is an authoritative data source but
// not a hard dependency at runtime.
// ============================================================
// Default to empty shape so lookups don't throw before fetch resolves.
window._DRUGS = window._DRUGS || { version: null, sections: {} };
window._DRUGS_READY = fetch('/data/drugs.json', { credentials: 'same-origin' })
.then(function(r) {
if (!r.ok) throw new Error('drugs.json HTTP ' + r.status);
return r.json();
})
.then(function(j) {
window._DRUGS = j;
return j;
})
.catch(function(err) {
// Non-fatal — calc functions have inline fallbacks.
console.warn('[drugs-loader] could not load /data/drugs.json:', err);
return window._DRUGS;
});