// Global variables let currentSession = null; // DOM elements const createPairsBtn = document.getElementById('createPairsBtn'); const namesInput = document.getElementById('namesInput'); const sessionNameInput = document.getElementById('sessionName'); const resultsSection = document.getElementById('resultsSection'); const pairsContainer = document.getElementById('pairsContainer'); const sessionTitle = document.getElementById('sessionTitle'); const sessionDate = document.getElementById('sessionDate'); const sessionsContainer = document.getElementById('sessionsContainer'); const loadingModal = document.getElementById('loadingModal'); const successModal = document.getElementById('successModal'); // Event listeners document.addEventListener('DOMContentLoaded', function() { loadPreviousSessions(); createPairsBtn.addEventListener('click', createPairs); // Add event listeners for action buttons document.addEventListener('click', function(e) { if (e.target.id === 'newSessionBtn') { resetForm(); } }); }); // API functions async function createPairs() { const names = namesInput.value.trim(); const sessionName = sessionNameInput.value.trim() || 'Prayer Session'; if (!names) { showError('Please enter at least one name.'); return; } const nameList = names.split('\n') .map(name => name.trim()) .filter(name => name.length > 0); if (nameList.length < 3) { showError('Please enter at least 3 names to create vibes.'); return; } showLoading(); try { const response = await fetch('/create-pairs', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ names: nameList, session_name: sessionName }) }); if (!response.ok) { const error = await response.json(); throw new Error(error.detail || 'Failed to create vibes'); } const result = await response.json(); currentSession = result; displayResults(result); hideLoading(); showSuccess(); loadPreviousSessions(); // Refresh the sessions list } catch (error) { hideLoading(); showError(error.message); } } async function loadPreviousSessions() { try { const response = await fetch('/sessions'); if (!response.ok) { throw new Error('Failed to load sessions'); } const sessions = await response.json(); displaySessions(sessions); } catch (error) { console.error('Error loading sessions:', error); } } async function loadSession(sessionId) { try { const response = await fetch(`/session/${sessionId}`); if (!response.ok) { throw new Error('Failed to load session'); } const session = await response.json(); currentSession = session; displayResults(session); // Scroll to results section resultsSection.scrollIntoView({ behavior: 'smooth' }); } catch (error) { showError('Failed to load session'); } } // Display functions function displayResults(session) { sessionTitle.textContent = session.session_name; sessionDate.textContent = formatDate(session.created_at); // Clear previous pairs pairsContainer.innerHTML = ''; // Display pairs session.pairs.forEach(pair => { const pairCard = document.createElement('div'); pairCard.className = 'pair-card'; pairCard.innerHTML = `
No previous sessions found.
'; return; } sessions.forEach(session => { const sessionCard = document.createElement('div'); sessionCard.className = 'session-card'; sessionCard.onclick = () => loadSession(session.id); const namesHtml = session.names.map(name => `${name}` ).join(''); sessionCard.innerHTML = `Created: ${formatDate(session.created_at)}