Lashtw commited on
Commit
d71bda1
·
verified ·
1 Parent(s): b7f16d3

Upload 9 files

Browse files
Files changed (1) hide show
  1. src/views/InstructorView.js +96 -33
src/views/InstructorView.js CHANGED
@@ -475,6 +475,102 @@ export function setupInstructorEvents() {
475
  });
476
  }
477
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
478
  // Handle Instructor Management
479
  navInstBtn.addEventListener('click', async () => {
480
  const modal = document.getElementById('instructor-modal');
@@ -950,26 +1046,6 @@ export function setupInstructorEvents() {
950
  document.head.appendChild(style);
951
  }
952
 
953
- navAdminBtn.addEventListener('click', () => {
954
- // Save current room to return later
955
- const currentRoom = localStorage.getItem('vibecoding_instructor_room');
956
- localStorage.setItem('vibecoding_admin_referer', 'instructor'); // track entry source
957
- window.location.hash = 'admin';
958
- });
959
-
960
- // Auto-fill code
961
- const savedRoomCode = localStorage.getItem('vibecoding_instructor_room');
962
- if (savedRoomCode) {
963
- document.getElementById('rejoin-room-code').value = savedRoomCode;
964
- }
965
-
966
- const rejoinBtn = document.getElementById('rejoin-room-btn');
967
- rejoinBtn.addEventListener('click', () => {
968
- const code = document.getElementById('rejoin-room-code').value.trim();
969
- if (!code) return alert('請輸入教室代碼');
970
- enterRoom(code);
971
- });
972
-
973
  // Gallery Logic
974
  document.getElementById('btn-open-gallery').addEventListener('click', () => {
975
  window.open('monster_preview.html', '_blank');
@@ -986,19 +1062,6 @@ export function setupInstructorEvents() {
986
  }
987
  });
988
 
989
- createBtn.addEventListener('click', async () => {
990
- try {
991
- createBtn.disabled = true;
992
- createBtn.textContent = "...";
993
- const roomCode = await createRoom();
994
- enterRoom(roomCode);
995
- } catch (error) {
996
- console.error(error);
997
- alert("建立失敗");
998
- createBtn.disabled = false;
999
- }
1000
- });
1001
-
1002
  // Check Previous Session (Handled by onAuthStateChanged now)
1003
  // if (sessionStorage.getItem('vibecoding_instructor_auth') === 'true') {
1004
  // authModal.classList.add('hidden');
 
475
  });
476
  }
477
 
478
+ // Create Room
479
+ if (createBtn) {
480
+ createBtn.addEventListener('click', async () => {
481
+ const roomCode = Math.random().toString(36).substring(2, 8).toUpperCase();
482
+ try {
483
+ // Ensure roomInfo is visible
484
+ const roomInfo = document.getElementById('room-info');
485
+ const displayRoomCode = document.getElementById('display-room-code');
486
+ const createContainer = document.getElementById('create-room-container');
487
+ const dashboardContent = document.getElementById('dashboard-content');
488
+
489
+ await createRoom(roomCode, currentInstructor ? currentInstructor.name : 'Unknown');
490
+ displayRoomCode.textContent = roomCode;
491
+
492
+ // Store in LocalStorage
493
+ localStorage.setItem('vibecoding_room_code', roomCode);
494
+ localStorage.setItem('vibecoding_is_host', 'true');
495
+
496
+ // UI Updates
497
+ createContainer.classList.add('hidden');
498
+ roomInfo.classList.remove('hidden');
499
+ dashboardContent.classList.remove('hidden');
500
+
501
+ // Start Subscription
502
+ subscribeToRoom(roomCode, (data) => {
503
+ updateDashboard(data);
504
+ });
505
+
506
+ } catch (e) {
507
+ console.error(e);
508
+ alert("無法建立教室: " + e.message);
509
+ }
510
+ });
511
+ }
512
+
513
+ // Rejoin Room
514
+ const rejoinBtn = document.getElementById('rejoin-room-btn');
515
+ if (rejoinBtn) {
516
+ rejoinBtn.addEventListener('click', async () => {
517
+ const inputCode = document.getElementById('rejoin-room-code').value.trim().toUpperCase();
518
+ if (!inputCode) return alert("請輸入代碼");
519
+
520
+ try {
521
+ // Ensure roomInfo is visible
522
+ const roomInfo = document.getElementById('room-info');
523
+ const displayRoomCode = document.getElementById('display-room-code');
524
+ const createContainer = document.getElementById('create-room-container');
525
+ const dashboardContent = document.getElementById('dashboard-content');
526
+
527
+ // Check if room exists first (optional, subscribe handles it usually)
528
+ displayRoomCode.textContent = inputCode;
529
+ localStorage.setItem('vibecoding_room_code', inputCode);
530
+
531
+ // UI Updates
532
+ createContainer.classList.add('hidden');
533
+ roomInfo.classList.remove('hidden');
534
+ dashboardContent.classList.remove('hidden');
535
+
536
+ subscribeToRoom(inputCode, (data) => {
537
+ updateDashboard(data);
538
+ });
539
+ } catch (e) {
540
+ alert("重回失敗: " + e.message);
541
+ }
542
+ });
543
+ }
544
+
545
+ // Leave Room
546
+ const leaveBtn = document.getElementById('leave-room-btn');
547
+ if (leaveBtn) {
548
+ leaveBtn.addEventListener('click', () => {
549
+ const roomInfo = document.getElementById('room-info');
550
+ const createContainer = document.getElementById('create-room-container');
551
+ const dashboardContent = document.getElementById('dashboard-content');
552
+ const displayRoomCode = document.getElementById('display-room-code');
553
+
554
+ localStorage.removeItem('vibecoding_room_code');
555
+ localStorage.removeItem('vibecoding_is_host');
556
+
557
+ displayRoomCode.textContent = '';
558
+ roomInfo.classList.add('hidden');
559
+ dashboardContent.classList.add('hidden');
560
+ createContainer.classList.remove('hidden');
561
+
562
+ // Unsubscribe logic if needed, usually auto-handled by new subscription or page reload
563
+ window.location.reload();
564
+ });
565
+ }
566
+
567
+ // Nav to Admin
568
+ if (navAdminBtn) {
569
+ navAdminBtn.addEventListener('click', () => {
570
+ window.location.hash = '#admin';
571
+ });
572
+ }
573
+
574
  // Handle Instructor Management
575
  navInstBtn.addEventListener('click', async () => {
576
  const modal = document.getElementById('instructor-modal');
 
1046
  document.head.appendChild(style);
1047
  }
1048
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1049
  // Gallery Logic
1050
  document.getElementById('btn-open-gallery').addEventListener('click', () => {
1051
  window.open('monster_preview.html', '_blank');
 
1062
  }
1063
  });
1064
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1065
  // Check Previous Session (Handled by onAuthStateChanged now)
1066
  // if (sessionStorage.getItem('vibecoding_instructor_auth') === 'true') {
1067
  // authModal.classList.add('hidden');