Loading invitation...

let inviteData = null; // Get invite token from URL const urlParams = new URLSearchParams(window.location.search); const token = urlParams.get('token'); document.addEventListener('DOMContentLoaded', async () => { if (!token) { showExpired(); return; } // Fetch invite details try { const response = await fetch(`/api/invite/${token}`); const data = await response.json(); if (data.success && data.invite) { inviteData = data.invite; showInvite(inviteData); checkDevices(); } else { showExpired(); } } catch (error) { console.error('Error loading invite:', error); showExpired(); } }); function showInvite(invite) { document.getElementById('loading-state').style.display = 'none'; document.getElementById('invite-content').style.display = 'block'; // Host info document.getElementById('host-name').textContent = invite.host_name || 'Your host'; if (invite.host_name) { document.getElementById('host-avatar').innerHTML = invite.host_name.charAt(0).toUpperCase(); } // Podcast/room name document.getElementById('podcast-name').textContent = invite.room_name || invite.podcast_name || 'Podcast Recording'; // Scheduled date if (invite.scheduled_date) { document.getElementById('schedule-row').style.display = 'flex'; const date = new Date(invite.scheduled_date); document.getElementById('scheduled-date').textContent = date.toLocaleString(); } // Expiration if (invite.expires_at) { const expires = new Date(invite.expires_at); document.getElementById('expires-at').textContent = expires.toLocaleString(); } // Personal message if (invite.message) { document.getElementById('guest-message').style.display = 'block'; document.getElementById('message-text').textContent = `"${invite.message}"`; } // Pre-fill guest name if provided if (invite.guest_name) { document.getElementById('guest-name').value = invite.guest_name; } } function showExpired() { document.getElementById('loading-state').style.display = 'none'; document.getElementById('expired-state').style.display = 'block'; } async function checkDevices() { // Check camera try { const videoStream = await navigator.mediaDevices.getUserMedia({ video: true }); videoStream.getTracks().forEach(t => t.stop()); setDeviceStatus('camera-status', 'ready'); } catch (e) { setDeviceStatus('camera-status', 'error'); } // Check microphone try { const audioStream = await navigator.mediaDevices.getUserMedia({ audio: true }); audioStream.getTracks().forEach(t => t.stop()); setDeviceStatus('mic-status', 'ready'); } catch (e) { setDeviceStatus('mic-status', 'error'); } // Check connection setDeviceStatus('connection-status', navigator.onLine ? 'ready' : 'error'); } function setDeviceStatus(elementId, status) { const el = document.getElementById(elementId); el.classList.remove('checking', 'ready', 'error'); el.classList.add(status); } // Handle form submission document.getElementById('guest-form').addEventListener('submit', async (e) => { e.preventDefault(); const guestName = document.getElementById('guest-name').value.trim(); if (!guestName) return; const btn = e.target.querySelector('button[type="submit"]'); btn.disabled = true; btn.innerHTML = 'Joining...'; try { // Mark invite as joined const response = await fetch(`/api/invite/${token}/join`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ guest_name: guestName }) }); const data = await response.json(); if (data.success) { // Store guest session info sessionStorage.setItem('guest_session', JSON.stringify({ token: token, name: guestName, room_id: inviteData.room_id })); // Redirect to podcast room window.location.href = `/podcast-room.html?room=${inviteData.room_id}&guest=true&name=${encodeURIComponent(guestName)}`; } else { throw new Error(data.message || 'Failed to join'); } } catch (error) { alert('Failed to join. Please try again.'); btn.disabled = false; btn.innerHTML = 'Join Recording'; } });