// ===== Data Upload & Schema ===== let selectedFile = null; // Drag & Drop - initialize safely on DOMContentLoaded document.addEventListener('DOMContentLoaded', () => { const zone = document.getElementById('uploadZone'); const fileInput = document.getElementById('fileInput'); if (zone && fileInput) { zone.addEventListener('click', (e) => { // Don't trigger file picker if clicking inside the source selector if (e.target.closest('.source-selector')) return; fileInput.click(); }); zone.addEventListener('dragover', e => { e.preventDefault(); zone.classList.add('dragover'); }); zone.addEventListener('dragleave', () => zone.classList.remove('dragover')); zone.addEventListener('drop', e => { e.preventDefault(); zone.classList.remove('dragover'); if (e.dataTransfer.files.length > 0) selectFile(e.dataTransfer.files[0]); }); fileInput.addEventListener('change', e => { if (e.target.files.length > 0) selectFile(e.target.files[0]); }); } // Load schema (Data Dictionary) loadSchema(); }); function selectFile(file) { selectedFile = file; const selector = document.getElementById('sourceSelector'); if (selector) selector.style.display = 'block'; const nameEl = document.getElementById('selectedFileName'); if (nameEl) nameEl.textContent = `${file.name} (${(file.size / 1024).toFixed(1)} KB)`; } function cancelUpload() { selectedFile = null; const selector = document.getElementById('sourceSelector'); if (selector) selector.style.display = 'none'; const vr = document.getElementById('validationResults'); if (vr) vr.classList.remove('visible'); const fi = document.getElementById('fileInput'); if (fi) fi.value = ''; } function uploadFile() { if (!selectedFile) return; const sourceType = document.getElementById('sourceType').value; if (!sourceType) { alert('Please select a data source type'); return; } const btn = document.querySelector('.btn-upload'); if (btn) { btn.disabled = true; btn.textContent = 'Uploading...'; } const formData = new FormData(); formData.append('file', selectedFile); formData.append('source_type', sourceType); fetch('/data/api/data/upload-raw', { method: 'POST', body: formData }) .then(r => r.json()) .then(data => { if (btn) { btn.disabled = false; btn.textContent = 'Upload to Raw'; } if (data.error) { alert(data.error); return; } showValidationResults(data); // Refresh the pipeline status to show the new file if (typeof refreshPipelineStatus === 'function') refreshPipelineStatus(); }) .catch(err => { if (btn) { btn.disabled = false; btn.textContent = 'Upload to Raw'; } console.error('Upload error:', err); alert('Upload failed: ' + err.message); }); } function showValidationResults(data) { const container = document.getElementById('validationResults'); const checks = document.getElementById('validationChecks'); if (!container || !checks) return; container.classList.add('visible'); let html = ''; // Show uploaded file info if (data.saved_as) { html += `
Could not load schema data.
'; console.error('Schema load error:', err); }); }