'use client' import React, { useState } from 'react' import { useRouter } from 'next/navigation' import { Users, Lock, Globe, Search, Play } from 'lucide-react' interface PublicRoom { room_id: string room_code: string title: string host_name: string participants_count: number max_participants: number questions_count: number status: string } export default function QuizJoinPage() { const [joinMode, setJoinMode] = useState<'code' | 'public'>('public') const [roomCode, setRoomCode] = useState('') const [username, setUsername] = useState('') const [publicRooms, setPublicRooms] = useState([]) const [loading, setLoading] = useState(false) const router = useRouter() React.useEffect(() => { if (joinMode === 'public') { fetchPublicRooms() } }, [joinMode]) const fetchPublicRooms = async () => { try { const response = await fetch('http://127.0.0.1:5000/api/quizzes/public-rooms') const data = await response.json() if (data.success) { setPublicRooms(data.public_rooms) } } catch (error) { console.error('Failed to fetch public rooms:', error) } } const joinRoom = async (code: string) => { if (!username.trim()) { alert('Please enter your username') return } setLoading(true) try { const token = localStorage.getItem("openlearnx_jwt_token") const storedUserRaw = localStorage.getItem("openlearnx_user") const storedUser = storedUserRaw ? JSON.parse(storedUserRaw) : null const headers: Record = { "Content-Type": "application/json" } if (token) { headers.Authorization = `Bearer ${token}` } const response = await fetch('http://127.0.0.1:5000/api/quizzes/join-room', { method: 'POST', headers, body: JSON.stringify({ room_code: code, username: username.trim(), wallet_address: storedUser?.wallet_address, user_id: storedUser?.id }) }) const data = await response.json() if (data.success) { // Store session info and redirect to quiz localStorage.setItem('quiz_session', JSON.stringify(data.session)) router.push(`/quiz-play/${data.session.session_id}`) } else { alert(`Error: ${data.error}`) } } catch (error) { alert('Network error: Could not join room') } finally { setLoading(false) } } const joinWithCode = () => { if (!roomCode.trim()) { alert('Please enter room code') return } joinRoom(roomCode.trim().toUpperCase()) } return (

🎯 Join Quiz

Join an adaptive quiz and test your knowledge!

{/* Username Input */}

👤 Enter Your Name

setUsername(e.target.value)} className="w-full p-3 bg-white dark:bg-gray-700 text-gray-900 dark:text-white placeholder-gray-500 dark:placeholder-gray-400 rounded border border-gray-300 dark:border-gray-600 focus:ring-2 focus:ring-blue-500 focus:outline-none" maxLength={20} />
{/* Join Mode Toggle */}
{/* Join with Code */} {joinMode === 'code' && (

🔐 Join with Room Code

setRoomCode(e.target.value.toUpperCase())} className="flex-1 p-3 bg-white dark:bg-gray-700 text-gray-900 dark:text-white placeholder-gray-500 dark:placeholder-gray-400 rounded border border-gray-300 dark:border-gray-600 focus:ring-2 focus:ring-blue-500 focus:outline-none" maxLength={6} />
)} {/* Public Rooms */} {joinMode === 'public' && (

🌍 Public Quiz Rooms

{publicRooms.length === 0 ? (

No public rooms available

Create your own room or join with a private code

) : (
{publicRooms.map((room) => (

{room.title}

Host: {room.host_name}

{room.status.toUpperCase()}
👥 {room.participants_count}/{room.max_participants} ❓ {room.questions_count} questions 🔢 {room.room_code}
))}
)}
)}
) }