From a4ab02fe616f292734b19a23d5f37e05b63be8d8 Mon Sep 17 00:00:00 2001 From: 5t4l1n Date: Sat, 26 Jul 2025 23:07:00 +0530 Subject: [PATCH] new --- frontend/app/coding/join/page.tsx | 225 +++++++++++++----------------- frontend/app/coding/page.tsx | 73 ++++++++-- 2 files changed, 160 insertions(+), 138 deletions(-) diff --git a/frontend/app/coding/join/page.tsx b/frontend/app/coding/join/page.tsx index 1a34907..9ceb80f 100644 --- a/frontend/app/coding/join/page.tsx +++ b/frontend/app/coding/join/page.tsx @@ -6,51 +6,50 @@ export default function JoinExam() { const [examCode, setExamCode] = useState('') const [studentName, setStudentName] = useState('') const [loading, setLoading] = useState(false) - const [result, setResult] = useState('') + const [error, setError] = useState('') const router = useRouter() - const join = async () => { - if (!examCode || !studentName) { - setResult('❌ Please fill both fields') + const handleSubmit = async (e: React.FormEvent) => { + e.preventDefault() // ✅ Prevent form from reloading page + + setError('') + + if (!examCode.trim()) { + setError('Please enter the exam code') + return + } + + if (!studentName.trim()) { + setError('Please enter your name') return } setLoading(true) - setResult('⏳ Joining exam...') try { + // ✅ CORRECT FIELD NAMES - Must match backend expectations const payload = { - exam_code: examCode.trim().toUpperCase(), - student_name: studentName.trim() + exam_code: examCode.trim().toUpperCase(), // Backend expects exam_code + student_name: studentName.trim() // Backend expects student_name } - console.log('🚀 Sending:', payload) + console.log('🚀 Sending payload:', payload) const response = await fetch('http://127.0.0.1:5000/api/exam/join-exam', { method: 'POST', - headers: { 'Content-Type': 'application/json' }, - body: JSON.stringify(payload) + headers: { + 'Content-Type': 'application/json', + 'Accept': 'application/json' + }, + body: JSON.stringify(payload) // ✅ MUST stringify the payload }) + console.log('📡 Response status:', response.status) + const data = await response.json() - console.log('📦 Response:', data) - + console.log('📦 Response data:', data) + if (data.success) { - // ✅ ENHANCED SUCCESS DISPLAY - const successMessage = `✅ Successfully joined: ${data.exam_info.title} - -📋 Exam Details: -• Status: ${data.exam_info.status} -• Duration: ${data.exam_info.duration_minutes} minutes -• Participants: ${data.exam_info.participants_count}/${data.exam_info.max_participants} -• Languages: ${data.exam_info.languages.join(', ')} -• Problem: ${data.exam_info.problem_title} - -🎯 You're now registered for the exam! -⏳ Wait for the host to start the exam.` - - setResult(successMessage) - // Store session data localStorage.setItem('exam_session', JSON.stringify({ exam_code: examCode.toUpperCase(), @@ -59,124 +58,96 @@ export default function JoinExam() { joined_at: new Date().toISOString() })) - // Show success alert - alert(`🎉 Welcome to the exam! + alert(`✅ Successfully joined: ${data.exam_info.title} -📝 Exam: ${data.exam_info.title} 👤 Joined as: ${studentName} 📊 You are participant #${data.exam_info.participants_count} -✅ Successfully registered!`) +Wait for the host to start the exam!`) - // Redirect to exam waiting page after 2 seconds - setTimeout(() => { - router.push('/coding/exam') - }, 2000) + // Redirect to exam interface + router.push('/coding/exam') } else { - setResult(`❌ Error: ${data.error}`) + setError(data.error || 'Failed to join exam') } } catch (error) { console.error('❌ Error:', error) - setResult('❌ Network error: Could not connect to server') + setError('Network error: Could not connect to backend server') } finally { setLoading(false) } } return ( -
-

🚀 Join Coding Exam

- -
-
- - setExamCode(e.target.value)} - placeholder="0C3LQ8" - style={{ - width: '100%', - padding: '12px', - background: '#333', - color: 'white', - border: '2px solid #4CAF50', - borderRadius: '4px', - fontFamily: 'monospace', - fontSize: '16px', - textTransform: 'uppercase' - }} - /> +
+
+
+

Join Coding Exam

+

Enter the exam code provided by your instructor

- -
- - setStudentName(e.target.value)} - placeholder="Your name" - style={{ - width: '100%', - padding: '12px', - background: '#333', - color: 'white', - border: '2px solid #4CAF50', - borderRadius: '4px', - fontSize: '16px' - }} - /> + + {/* Form with proper onSubmit handler */} +
+
+ + setExamCode(e.target.value.toUpperCase())} + placeholder="Enter 6-character code" + maxLength={6} + className="w-full px-4 py-3 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent text-center text-lg font-mono tracking-widest uppercase" + required + /> +
+ +
+ + setStudentName(e.target.value)} + placeholder="Enter your full name" + className="w-full px-4 py-3 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent" + required + /> +
+ + +
+ + {/* Error Display */} + {error && ( +
+ ❌ {error} +
+ )} + + {/* Debug Info */} +
+

Debug Info:

+

Code: "{examCode}"

+

Name: "{studentName}"

+

✅ Sends: exam_code + student_name

- - -
- - {/* ENHANCED RESULT DISPLAY */} - {result && ( -
- {result} -
- )} - -
-

🔧 Debug Info:

-

Exam Code: "{examCode}"

-

Student Name: "{studentName}"

-

✅ Backend working correctly

) diff --git a/frontend/app/coding/page.tsx b/frontend/app/coding/page.tsx index 07c3632..a69cf4b 100644 --- a/frontend/app/coding/page.tsx +++ b/frontend/app/coding/page.tsx @@ -118,36 +118,74 @@ export default function CodingExamPlatform() { }) const data = await response.json() + console.log('📦 Create exam response:', data) + if (data.success) { - setExamId(data.exam_code) + // ✅ CORRECTED: Use exam_code, NOT exam_id + const participantCode = data.exam_code // This is the 6-character code + const databaseId = data.exam_id // This is the MongoDB ObjectId + + setExamId(participantCode) setExamInfo({ title: 'String Capitalizer Challenge', status: 'waiting' }) - alert(`Exam created! Share this code with participants: ${data.exam_code}`) + + // ✅ FIXED: Show exam_code instead of exam_id + alert(`Exam created! Share this code with participants: ${participantCode}`) + } else { + alert(`Failed to create exam: ${data.error}`) } } catch (error) { - alert('Failed to create exam') + console.error('Create exam error:', error) + alert('Failed to create exam - network error') } } + // ✅ CORRECTED JOIN FUNCTION WITH PROPER REDIRECT const joinExam = async () => { try { + console.log('🚀 Joining with:', { exam_code: examId, student_name: participantName }) + const response = await fetch('http://127.0.0.1:5000/api/exam/join-exam', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ - exam_id: examId, - name: participantName + exam_code: examId, // ✅ Correct field name + student_name: participantName // ✅ Changed from 'name' to 'student_name' }) }) const data = await response.json() + console.log('📦 Join response:', data) + if (data.success) { setExamInfo(data.exam_info) - alert('Successfully joined the exam!') + + // Store exam session data for the exam interface + localStorage.setItem('exam_session', JSON.stringify({ + exam_code: examId, + student_name: participantName, + exam_info: data.exam_info, + joined_at: new Date().toISOString() + })) + + alert(`✅ Successfully joined: ${data.exam_info.title}! + +👤 Joined as: ${participantName} +📊 Participants: ${data.exam_info.participants_count}/${data.exam_info.max_participants} +⏱️ Duration: ${data.exam_info.duration_minutes} minutes + +Redirecting to exam interface...`) + + // ✅ REDIRECT TO EXAM INTERFACE + setTimeout(() => { + router.push('/coding/exam') + }, 1500) + } else { - alert(data.error) + alert(`❌ Error: ${data.error}`) } } catch (error) { - alert('Failed to join exam') + console.error('Join error:', error) + alert('❌ Failed to join exam - network error') } } @@ -156,7 +194,7 @@ export default function CodingExamPlatform() { const response = await fetch('http://127.0.0.1:5000/api/exam/start-exam', { method: 'POST', headers: { 'Content-Type': 'application/json' }, - body: JSON.stringify({ exam_id: examId }) + body: JSON.stringify({ exam_code: examId }) }) const data = await response.json() @@ -273,6 +311,12 @@ export default function CodingExamPlatform() { Create Exam
+ + {/* Debug Info */} +
+

Will create with host_name: "{participantName}"

+

✅ Will display exam_code (6 chars), not exam_id

+
) @@ -288,10 +332,11 @@ export default function CodingExamPlatform() {
setExamId(e.target.value.toUpperCase())} - className="w-full p-3 border border-gray-300 rounded-lg" + className="w-full p-3 border border-gray-300 rounded-lg text-center font-mono text-lg tracking-widest uppercase" + maxLength={6} /> Join Exam + + {/* Debug Info */} +
+

Will send: exam_code="{examId}" student_name="{participantName}"

+

✅ After join → redirect to /coding/exam

+