// API base URL const API_BASE_URL = '/api'; // Selected uploads for actions let selectedUploads = new Set(); // Show toast notification function showToast(message, type = 'success') { const toast = document.createElement('div'); toast.className = `fixed bottom-4 right-4 px-6 py-3 rounded-lg shadow-lg text-white ${ type === 'success' ? 'bg-green-600' : 'bg-red-600' } transform translate-y-0 opacity-100 transition-all duration-300`; toast.textContent = message; document.body.appendChild(toast); setTimeout(() => { toast.style.transform = 'translateY(100%)'; toast.style.opacity = '0'; setTimeout(() => toast.remove(), 300); }, 3000); } // Show loading state function setLoading(isLoading) { const buttons = document.querySelectorAll('button'); buttons.forEach(button => { if (isLoading) { button.disabled = true; button.classList.add('opacity-50', 'cursor-not-allowed'); } else { button.disabled = false; button.classList.remove('opacity-50', 'cursor-not-allowed'); } }); } // Handle form submission document.getElementById('uploadForm').addEventListener('submit', async (e) => { e.preventDefault(); const formData = new FormData(); formData.append('file', document.getElementById('csvFile').files[0]); formData.append('source', document.getElementById('source').value); formData.append('description', document.getElementById('description').value); try { setLoading(true); const response = await fetch(`${API_BASE_URL}/manabox/process-csv`, { method: 'POST', body: formData }); if (!response.ok) { const errorData = await response.json(); throw new Error(errorData.detail || 'Failed to upload CSV'); } showToast('CSV uploaded successfully'); document.getElementById('uploadForm').reset(); fetchUploads(); // Refresh the uploads list } catch (error) { showToast('Error uploading CSV: ' + error.message, 'error'); } finally { setLoading(false); } }); // Fetch uploads from the API async function fetchUploads() { try { setLoading(true); const response = await fetch(`${API_BASE_URL}/manabox/manabox-file-uploads`); if (!response.ok) { throw new Error('Failed to fetch uploads'); } const uploads = await response.json(); displayUploads(uploads); } catch (error) { showToast('Error fetching uploads: ' + error.message, 'error'); } finally { setLoading(false); } } // Display uploads in the UI function displayUploads(uploads) { const uploadsList = document.getElementById('uploadsList'); uploadsList.innerHTML = ''; if (!uploads || uploads.length === 0) { uploadsList.innerHTML = '