ot / frontend /src /data /templateData.js
jashdoshi77's picture
OT NoteBuilder - Production deployment
ba95018
/**
* Template Data Loader
* ====================
* Fetches the template structure from the protected backend API.
* This file replaces the original templateData.js — the actual data
* lives on the server and is only accessible to authenticated users.
*/
const API_BASE = import.meta.env.VITE_API_URL || '';
// In-memory cache so we only fetch once per session
let _cachedData = null;
/**
* Fetches the complete template data from the backend.
* Must be called after authentication (needs Bearer token).
*/
export async function fetchTemplateData(token) {
if (_cachedData) return _cachedData;
const res = await fetch(`${API_BASE}/api/template`, {
headers: { Authorization: `Bearer ${token}` },
});
if (!res.ok) {
throw new Error('Failed to load template data');
}
_cachedData = await res.json();
return _cachedData;
}
/**
* Returns cached template data (must call fetchTemplateData first).
*/
export function getTemplateData() {
return _cachedData;
}
// Provide empty defaults for imports that happen before fetch completes.
// These get replaced once fetchTemplateData() resolves.
export let ABBREVIATIONS = [];
export let SMART_PHRASES = [];
export let HEADER_FIELDS = [];
// Default export — the sections array
let TEMPLATE_SECTIONS = [];
export default TEMPLATE_SECTIONS;
/**
* Called after fetchTemplateData resolves to populate the module-level exports.
*/
export function hydrateTemplateData(data) {
TEMPLATE_SECTIONS = data.sections || [];
ABBREVIATIONS = data.abbreviations || [];
SMART_PHRASES = data.smartPhrases || [];
HEADER_FIELDS = data.headerFields || [];
_cachedData = data;
// Return for convenience
return { TEMPLATE_SECTIONS, ABBREVIATIONS, SMART_PHRASES, HEADER_FIELDS };
}