/** * Data Analyzer Agent - Specific JavaScript * Uses WorkflowsCore for all shared functionality */ // Initialize data analyzer functionality document.addEventListener('DOMContentLoaded', function() { console.log('Data Analyzer loaded'); // Initialize file upload const fileInput = document.getElementById('dataFile'); if (fileInput) { fileInput.addEventListener('change', handleFileChange); } // Initialize drag and drop const uploadArea = document.querySelector('.file-upload-area'); if (uploadArea && fileInput) { WorkflowsCore.setupDragAndDrop(uploadArea, fileInput); } // Set initial radio selection const firstRadio = document.querySelector('.radio-card'); if (firstRadio && !document.querySelector('.radio-card.selected')) { firstRadio.classList.add('selected'); const input = firstRadio.querySelector('input[type="radio"]'); if (input) input.checked = true; } // Handle form submission const form = document.getElementById('agentForm'); if (form) { form.addEventListener('submit', handleFormSubmission); } }); // Data Analyzer specific functions function selectRadio(value) { // Remove selected class from all cards document.querySelectorAll('.radio-card').forEach(card => { card.classList.remove('selected'); }); // Add selected class to clicked card const selectedCard = document.querySelector(`input[value="${value}"]`).closest('.radio-card'); if (selectedCard) { selectedCard.classList.add('selected'); } // Select the radio button const radioInput = document.getElementById(value); if (radioInput) { radioInput.checked = true; } } function handleFileChange(event) { const file = event.target.files[0]; const uploadArea = document.querySelector('.file-upload-area'); const uploadText = document.querySelector('.upload-text'); if (file) { uploadArea.classList.add('file-selected'); uploadText.innerHTML = `
📄
${file.name}
${WorkflowsCore.formatFileSize(file.size)}
`; WorkflowsCore.showToast(`File selected: ${file.name}`, 'success'); } else { uploadArea.classList.remove('file-selected'); uploadText.innerHTML = `
📁
Click to upload or drag and drop
PDF files only
`; } } function handleFormSubmission(e) { e.preventDefault(); // Validate form if (!isFormValid()) { return; } // Check authentication and balance using WorkflowsCore if (!WorkflowsCore.checkAuthentication()) { return; } const agentPrice = parseFloat(document.body.getAttribute('data-agent-price')); if (!WorkflowsCore.checkBalance(agentPrice)) { return; } // Show processing status WorkflowsCore.showProcessing('Analyzing Your Data...'); // Submit form with AJAX const formData = new FormData(e.target); fetch(window.location.href, { method: 'POST', body: formData, headers: { 'X-Requested-With': 'XMLHttpRequest' } }) .then(response => { if (response.headers.get('content-type')?.includes('application/json')) { return response.json(); } else { return response.text().then(html => { console.log('HTML response received'); return { success: true, processing: true }; }); } }) .then(result => { if (result.success && result.analysis_results) { // Handle JSON response with analysis data displayAnalysisResults(result.analysis_results); // Update wallet balance if provided if (result.wallet_balance !== undefined) { WorkflowsCore.updateWalletBalance(result.wallet_balance); } } else if (result.success && result.processing) { // Show processing message WorkflowsCore.showToast('🔄 Processing started successfully!', 'success'); // Show unavailable message after timeout (since N8N integration may not be active) setTimeout(() => { WorkflowsCore.hideProcessing(); WorkflowsCore.showToast('⚠️ Analysis service temporarily unavailable. Please try again later.', 'error'); }, 30000); // 30 second timeout } else { WorkflowsCore.hideProcessing(); WorkflowsCore.showToast(`❌ ${result.error || 'Processing failed'}`, 'error'); } }) .catch(error => { console.error('Form submission error:', error); WorkflowsCore.hideProcessing(); WorkflowsCore.showToast('❌ Connection error. Please try again.', 'error'); }); } function displayAnalysisResults(analysisData) { WorkflowsCore.hideProcessing(); let resultsHtml = '

✅ Analysis Complete

'; if (analysisData.sections && analysisData.sections.length > 0) { resultsHtml += '
'; analysisData.sections.forEach(section => { if (section.heading && section.content) { resultsHtml += `

📋 ${section.heading}

${section.content.replace(/\n/g, '
')}
`; } }); resultsHtml += '
'; } else { resultsHtml += '

Analysis completed successfully. Your data has been processed.

'; } if (analysisData.timestamp) { resultsHtml += `

Analysis completed: ${new Date(analysisData.timestamp).toLocaleString()}

`; } WorkflowsCore.showResults(resultsHtml, 'Analysis Results'); WorkflowsCore.showToast('✅ Data analysis completed successfully!', 'success'); } function isFormValid() { const fileInput = document.getElementById('dataFile'); const analysisType = document.querySelector('input[name="analysisType"]:checked'); // Clear previous errors WorkflowsCore.clearFieldError('dataFile'); WorkflowsCore.clearFieldError('analysisType'); let isValid = true; if (!fileInput.files || fileInput.files.length === 0) { WorkflowsCore.showFieldError('dataFile', 'Please select a data file'); WorkflowsCore.showToast('Please select a data file', 'error'); isValid = false; } if (!analysisType) { WorkflowsCore.showFieldError('analysisType', 'Please select an analysis type'); WorkflowsCore.showToast('Please select an analysis type', 'error'); isValid = false; } return isValid; }