import React, { useState, useEffect } from 'react'; import { useNavigate } from 'react-router-dom'; import { useAuth } from '../contexts/AuthContext'; import './TestList.css'; const TestList = () => { const [tests, setTests] = useState([]); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); const [showPasswordModal, setShowPasswordModal] = useState(false); const [selectedTest, setSelectedTest] = useState(null); const [password, setPassword] = useState(''); const [filterStatus, setFilterStatus] = useState('all'); const navigate = useNavigate(); const { token } = useAuth(); useEffect(() => { fetchTests(); }, []); const fetchTests = async () => { try { console.log('Fetching tests with token:', token?.substring(0, 50) + '...'); const response = await fetch('http://localhost:5000/api/students/tests', { headers: { 'Authorization': `Bearer ${token}` } }); if (!response.ok) { if (response.status === 401) { localStorage.removeItem('monaco_user'); localStorage.removeItem('monaco_token'); window.location.href = '/login'; return; } throw new Error(`HTTP error! status: ${response.status}`); } const data = await response.json(); if (data.success) { console.log('Tests received:', data.tests); data.tests.forEach(test => { console.log(`Test: ${test.title}, Status: ${test.status}, Start: ${test.start_time}, End: ${test.end_time}`); }); setTests(data.tests); } else { setError(data.message || 'Failed to fetch tests'); } } catch (error) { setError('Failed to fetch tests'); console.error('Error:', error); } finally { setLoading(false); } }; const handleStartTest = async (test) => { try { const response = await fetch(`http://localhost:5000/api/students/tests/${test.id}/questions`, { headers: { 'Authorization': `Bearer ${token}` } }); const data = await response.json(); if (data.success) { localStorage.setItem('currentTest', JSON.stringify({ id: test.id, questions: data.questions, currentQuestionIndex: 0 })); navigate('/editor'); } else { setError(data.message); } } catch (error) { setError('Failed to start test'); console.error('Error:', error); } }; const handlePasswordSubmit = async () => { if (!selectedTest || !password) return; try { const response = await fetch(`http://localhost:5000/api/students/tests/${selectedTest.id}/verify-password`, { method: 'POST', headers: { 'Authorization': `Bearer ${token}`, 'Content-Type': 'application/json' }, body: JSON.stringify({ password }) }); const data = await response.json(); if (data.success) { setShowPasswordModal(false); setPassword(''); handleStartTest(selectedTest); } else { setError('Invalid password'); } } catch (error) { setError('Failed to verify password'); console.error('Error:', error); } }; const handleTestClick = (test) => { if (test.password_required) { setSelectedTest(test); setShowPasswordModal(true); } else { handleStartTest(test); } }; const filteredTests = tests.filter(test => { if (filterStatus === 'all') return true; return test.status.toLowerCase() === filterStatus.toLowerCase(); }); if (loading) { return (

Loading tests...

); } if (error) { return (

Error

{error}

); } return (
{/* Header Section */}

📝 Available Tests

Select a test to start your coding challenge

{/* Filter Tabs */}
{/* Tests Grid */}
{filteredTests.length === 0 ? (

No tests available

Check back later for new tests

) : (
{filteredTests.map(test => (
{/* Status Badge */}
{/* Header */}

{test.title}

{test.status}
{/* Description */}

{test.description || 'No description available'}

{/* Test Details */}
{test.duration_minutes} minutes
{test.total_questions && (
{test.total_questions} questions
)} {test.password_required && (
Password required
)}
{/* Action Button */}
))}
)}
{/* Password Modal */} {showPasswordModal && (
{/* Modal Header */}

Protected Test

Enter password to continue

{/* Modal Body */}
setPassword(e.target.value)} onKeyPress={(e) => e.key === 'Enter' && handlePasswordSubmit()} className="modal-input" placeholder="Enter password" autoFocus />
{/* Modal Footer */}
)}
); }; export default TestList;