EHDEM commited on
Commit
4ecb66c
·
verified ·
1 Parent(s): 4f5cb2a

Promote version 6bf265d to main

Browse files

Promoted commit 6bf265d7bdea014a3d7ab2d900fab727426e877f to main branch

Files changed (1) hide show
  1. admin-dashboard.html +45 -145
admin-dashboard.html CHANGED
@@ -81,10 +81,10 @@
81
  <button id="add-student-btn" class="px-4 py-2 bg-blue-600 text-white rounded-l-lg hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-blue-500">
82
  Add Student
83
  </button>
84
- <button id="remove-student-btn" class="px-4 py-2 bg-red-600 text-white rounded-r-lg hover:bg-red-700 focus:outline-none focus:ring-2 focus:ring-red-300">
85
  Remove Student
86
  </button>
87
- </div>
88
  </div>
89
 
90
  <!-- Form that changes based on selection -->
@@ -117,21 +117,21 @@
117
  <label class="block text-gray-700 mb-1">Student Name</label>
118
  <input type="text" class="w-full px-3 py-2 border rounded" id="student-name" required>
119
  </div>
120
- <div class="mb-3 hidden" id="remove-student-select">
121
- <label class="block text-gray-700 mb-1">Student</label>
122
- <select class="w-full px-3 py-2 border rounded" id="student-to-remove" required>
123
- <option value="">Select Student</option>
124
- </select>
125
- </div>
126
- <button type="submit" class="w-full bg-blue-600 text-white py-2 rounded hover:bg-blue-700">
127
  Add Student
128
  </button>
129
  </div>
130
 
131
  <!-- Remove Student Fields (hidden by default) -->
132
  <div id="remove-student-fields" class="hidden">
 
 
 
 
 
 
133
  <button type="button" id="remove-student-btn-submit" class="w-full bg-red-600 text-white py-2 rounded hover:bg-red-700">
134
- Remove Selected Student
135
  </button>
136
  </div>
137
  </form>
@@ -335,15 +335,16 @@
335
  e.preventDefault();
336
  document.getElementById('management-section').classList.toggle('hidden');
337
  });
 
338
  // Toggle between add/remove student forms
339
  const addStudentBtn = document.getElementById('add-student-btn');
340
  const removeStudentBtn = document.getElementById('remove-student-btn');
341
  const addFields = document.getElementById('add-student-fields');
342
- const removeContainer = document.getElementById('remove-student-container');
343
 
344
  addStudentBtn.addEventListener('click', function() {
345
  addFields.classList.remove('hidden');
346
- removeContainer.classList.add('hidden');
347
  addStudentBtn.classList.replace('bg-gray-200', 'bg-blue-600');
348
  addStudentBtn.classList.replace('text-gray-700', 'text-white');
349
  removeStudentBtn.classList.replace('bg-red-600', 'bg-gray-200');
@@ -352,14 +353,18 @@
352
 
353
  removeStudentBtn.addEventListener('click', function() {
354
  addFields.classList.add('hidden');
355
- removeContainer.classList.remove('hidden');
356
  removeStudentBtn.classList.replace('bg-gray-200', 'bg-red-600');
357
  removeStudentBtn.classList.replace('text-gray-700', 'text-white');
358
  addStudentBtn.classList.replace('bg-blue-600', 'bg-gray-200');
359
  addStudentBtn.classList.replace('text-white', 'text-gray-700');
360
- loadStudentsForRemoval();
361
  });
362
- // Handle form submission
 
 
 
 
 
363
  document.getElementById('student-form').addEventListener('submit', function(e) {
364
  e.preventDefault();
365
  const className = document.getElementById('student-class').value;
@@ -457,9 +462,9 @@
457
 
458
  this.reset();
459
  });
460
- // Load students for select dropdown (used in attendance forms)
461
- async function loadStudentsForManagement() {
462
- const classVal = document.getElementById('student-class').value;
463
  const formVal = document.getElementById('student-form').value;
464
  const studentSelect = document.getElementById('student-to-remove');
465
  const addStudentSelect = document.getElementById('student-name');
@@ -467,70 +472,14 @@ const classVal = document.getElementById('student-class').value;
467
  studentSelect.innerHTML = '<option value="">Select Student</option>';
468
 
469
  if (classVal && formVal) {
470
- try {
471
- // First check local student list
472
- const key = `${classVal}-${formVal}`;
473
- if (students[key] && students[key].length > 0) {
474
- students[key].forEach(student => {
475
- const option = document.createElement('option');
476
- option.value = student;
477
- option.textContent = student;
478
- studentSelect.appendChild(option);
479
- });
480
- }
481
-
482
- // Also fetch from GitHub to ensure we have all possible students
483
- const [formAResponse, formBResponse] = await Promise.all([
484
- fetch(`https://api.github.com/repos/${GITHUB_CONFIG.REPOS.ATTENDANCE}/contents/Data%20Students%20who%20want%20to%20eat`, {
485
- headers: {
486
- 'Accept': 'application/vnd.github.v3+json',
487
- 'Authorization': `token ${GITHUB_CONFIG.TOKEN}`
488
- }
489
- }),
490
- fetch(`https://api.github.com/repos/${GITHUB_CONFIG.REPOS.CONSUMED}/contents/data%20who%20has%20ate%20at%20dm`, {
491
- headers: {
492
- 'Accept': 'application/vnd.github.v3+json',
493
- 'Authorization': `token ${GITHUB_CONFIG.TOKEN}`
494
- }
495
- })
496
- ]);
497
-
498
- if (!formAResponse.ok || !formBResponse.ok) {
499
- throw new Error('Failed to fetch attendance data');
500
- }
501
-
502
- const [formAData, formBData] = await Promise.all([
503
- formAResponse.json(),
504
- formBResponse.json()
505
- ]);
506
-
507
- const formAContent = atob(formAData.content);
508
- const formBContent = atob(formBData.content);
509
-
510
- const allRecords = [
511
- ...formAContent.split('\n').filter(line => line.trim() !== '').map(line => JSON.parse(line)),
512
- ...formBContent.split('\n').filter(line => line.trim() !== '').map(line => JSON.parse(line))
513
- ];
514
-
515
- // Get unique students from attendance records matching the class and form
516
- const attendanceStudents = [...new Set(
517
- allRecords
518
- .filter(r => r.class === classVal && r.form == formVal && r.studentName !== 'N/A')
519
- .map(r => r.studentName)
520
- )];
521
-
522
- // Add any students not already in the dropdown
523
- attendanceStudents.forEach(student => {
524
- if (!Array.from(studentSelect.options).some(opt => opt.value === student)) {
525
- const option = document.createElement('option');
526
- option.value = student;
527
- option.textContent = student;
528
- studentSelect.appendChild(option);
529
- }
530
  });
531
- } catch (error) {
532
- console.error('Error loading students:', error);
533
- // Continue with local data only
534
  }
535
  }
536
  }
@@ -590,30 +539,10 @@ const classVal = document.getElementById('student-class').value;
590
  localStorage.setItem('studentData', JSON.stringify(students));
591
  }
592
  }
593
- // Function to update remove student dropdown
594
- function updateRemoveStudentDropdown() {
595
- const classVal = document.getElementById('student-class').value;
596
- const formVal = document.getElementById('student-form').value;
597
- const studentSelect = document.getElementById('student-to-remove');
598
-
599
- studentSelect.innerHTML = '<option value="">Select Student</option>';
600
-
601
- if (classVal && formVal) {
602
- const key = `${classVal}-${formVal}`;
603
- if (students[key]) {
604
- students[key].forEach(student => {
605
- const option = document.createElement('option');
606
- option.value = student;
607
- option.textContent = student;
608
- studentSelect.appendChild(option);
609
- });
610
- }
611
- }
612
- }
613
-
614
  // Initialize student data when page loads
615
  initializeStudentData();
616
- // Prevent form submission on enter key
 
617
  document.getElementById('student-form').addEventListener('keypress', function(e) {
618
  if (e.key === 'Enter') {
619
  e.preventDefault();
@@ -652,73 +581,44 @@ const classVal = document.getElementById('student-class').value;
652
  showManagementMessage('Student already exists', 'error');
653
  }
654
  });
655
- // Load students for removal (checkboxes)
656
- function loadStudentsForRemoval() {
657
- const classVal = document.getElementById('student-class').value;
658
- const formVal = document.getElementById('student-form').value;
659
- const container = document.getElementById('students-to-remove-list');
660
-
661
- container.innerHTML = '';
662
-
663
- if (classVal && formVal) {
664
- const key = `${classVal}-${formVal}`;
665
- if (students[key] && students[key].length > 0) {
666
- students[key].forEach(student => {
667
- const div = document.createElement('div');
668
- div.className = 'flex items-center p-2 hover:bg-gray-100 rounded';
669
- div.innerHTML = `
670
- <input type="checkbox" id="remove-${student}" name="students-to-remove" value="${student}"
671
- class="mr-2 h-4 w-4 rounded border-gray-300 text-red-600 focus:ring-red-500">
672
- <label for="remove-${student}">${student}</label>
673
- `;
674
- container.appendChild(div);
675
- });
676
- } else {
677
- container.innerHTML = '<p class="text-gray-500 text-center py-2">No students found</p>';
678
- }
679
- } else {
680
- container.innerHTML = '<p class="text-gray-500 text-center py-2">Please select class and form</p>';
681
- }
682
- }
683
-
684
- // Handle remove students button click
685
  document.getElementById('remove-student-btn-submit').addEventListener('click', function() {
686
  const className = document.getElementById('student-class').value;
687
  const form = document.getElementById('student-form').value;
 
688
 
689
  if (!className || !form) {
690
  showManagementMessage('Please select both class and form', 'error');
691
  return;
692
  }
693
 
694
- const checkboxes = document.querySelectorAll('input[name="students-to-remove"]:checked');
695
- if (checkboxes.length === 0) {
696
- showManagementMessage('Please select at least one student to remove', 'error');
697
  return;
698
  }
699
 
700
  const key = `${className}-${form}`;
 
701
  if (students[key]) {
702
- const studentsToRemove = Array.from(checkboxes).map(cb => cb.value);
703
- const updatedStudents = students[key].filter(name => !studentsToRemove.includes(name));
704
  students[key] = updatedStudents;
705
 
706
  // Save to localStorage
707
  localStorage.setItem('studentData', JSON.stringify(students));
708
 
709
  // Show success message
710
- showManagementMessage(`${studentsToRemove.length} student(s) removed successfully!`);
 
 
 
711
 
712
- // Reload students list
713
- loadStudentsForRemoval();
714
  } else {
715
  showManagementMessage('No students found for this class/form', 'error');
716
  }
717
  });
718
-
719
- // Reload students list when class/form changes
720
- document.getElementById('student-class').addEventListener('change', loadStudentsForRemoval);
721
- document.getElementById('student-form').addEventListener('change', loadStudentsForRemoval);
722
  });
723
  </script>
724
  <script>
 
81
  <button id="add-student-btn" class="px-4 py-2 bg-blue-600 text-white rounded-l-lg hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-blue-500">
82
  Add Student
83
  </button>
84
+ <button id="remove-student-btn" class="px-4 py-2 bg-gray-200 text-gray-700 rounded-r-lg hover:bg-gray-300 focus:outline-none focus:ring-2 focus:ring-gray-300">
85
  Remove Student
86
  </button>
87
+ </div>
88
  </div>
89
 
90
  <!-- Form that changes based on selection -->
 
117
  <label class="block text-gray-700 mb-1">Student Name</label>
118
  <input type="text" class="w-full px-3 py-2 border rounded" id="student-name" required>
119
  </div>
120
+ <button type="submit" class="w-full bg-blue-600 text-white py-2 rounded hover:bg-blue-700">
 
 
 
 
 
 
121
  Add Student
122
  </button>
123
  </div>
124
 
125
  <!-- Remove Student Fields (hidden by default) -->
126
  <div id="remove-student-fields" class="hidden">
127
+ <div class="mb-3">
128
+ <label class="block text-gray-700 mb-1">Student</label>
129
+ <select class="w-full px-3 py-2 border rounded" id="student-to-remove" required>
130
+ <option value="">Select Student</option>
131
+ </select>
132
+ </div>
133
  <button type="button" id="remove-student-btn-submit" class="w-full bg-red-600 text-white py-2 rounded hover:bg-red-700">
134
+ Remove Student
135
  </button>
136
  </div>
137
  </form>
 
335
  e.preventDefault();
336
  document.getElementById('management-section').classList.toggle('hidden');
337
  });
338
+
339
  // Toggle between add/remove student forms
340
  const addStudentBtn = document.getElementById('add-student-btn');
341
  const removeStudentBtn = document.getElementById('remove-student-btn');
342
  const addFields = document.getElementById('add-student-fields');
343
+ const removeFields = document.getElementById('remove-student-fields');
344
 
345
  addStudentBtn.addEventListener('click', function() {
346
  addFields.classList.remove('hidden');
347
+ removeFields.classList.add('hidden');
348
  addStudentBtn.classList.replace('bg-gray-200', 'bg-blue-600');
349
  addStudentBtn.classList.replace('text-gray-700', 'text-white');
350
  removeStudentBtn.classList.replace('bg-red-600', 'bg-gray-200');
 
353
 
354
  removeStudentBtn.addEventListener('click', function() {
355
  addFields.classList.add('hidden');
356
+ removeFields.classList.remove('hidden');
357
  removeStudentBtn.classList.replace('bg-gray-200', 'bg-red-600');
358
  removeStudentBtn.classList.replace('text-gray-700', 'text-white');
359
  addStudentBtn.classList.replace('bg-blue-600', 'bg-gray-200');
360
  addStudentBtn.classList.replace('text-white', 'text-gray-700');
 
361
  });
362
+
363
+ // Load students when class/form changes
364
+ document.getElementById('student-class').addEventListener('change', loadStudentsForManagement);
365
+ document.getElementById('student-form').addEventListener('change', loadStudentsForManagement);
366
+
367
+ // Handle form submission
368
  document.getElementById('student-form').addEventListener('submit', function(e) {
369
  e.preventDefault();
370
  const className = document.getElementById('student-class').value;
 
462
 
463
  this.reset();
464
  });
465
+ // Load students for management dropdown
466
+ function loadStudentsForManagement() {
467
+ const classVal = document.getElementById('student-class').value;
468
  const formVal = document.getElementById('student-form').value;
469
  const studentSelect = document.getElementById('student-to-remove');
470
  const addStudentSelect = document.getElementById('student-name');
 
472
  studentSelect.innerHTML = '<option value="">Select Student</option>';
473
 
474
  if (classVal && formVal) {
475
+ const key = `${classVal}-${formVal}`;
476
+ if (students[key] && students[key].length > 0) {
477
+ students[key].forEach(student => {
478
+ const option = document.createElement('option');
479
+ option.value = student;
480
+ option.textContent = student;
481
+ studentSelect.appendChild(option);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
482
  });
 
 
 
483
  }
484
  }
485
  }
 
539
  localStorage.setItem('studentData', JSON.stringify(students));
540
  }
541
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
542
  // Initialize student data when page loads
543
  initializeStudentData();
544
+
545
+ // Prevent form submission on enter key
546
  document.getElementById('student-form').addEventListener('keypress', function(e) {
547
  if (e.key === 'Enter') {
548
  e.preventDefault();
 
581
  showManagementMessage('Student already exists', 'error');
582
  }
583
  });
584
+ // Handle remove student button click
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
585
  document.getElementById('remove-student-btn-submit').addEventListener('click', function() {
586
  const className = document.getElementById('student-class').value;
587
  const form = document.getElementById('student-form').value;
588
+ const studentName = document.getElementById('student-to-remove').value;
589
 
590
  if (!className || !form) {
591
  showManagementMessage('Please select both class and form', 'error');
592
  return;
593
  }
594
 
595
+ if (!studentName || studentName === '') {
596
+ showManagementMessage('Please select a student to remove', 'error');
 
597
  return;
598
  }
599
 
600
  const key = `${className}-${form}`;
601
+
602
  if (students[key]) {
603
+ // Filter out the selected student
604
+ const updatedStudents = students[key].filter(name => name !== studentName);
605
  students[key] = updatedStudents;
606
 
607
  // Save to localStorage
608
  localStorage.setItem('studentData', JSON.stringify(students));
609
 
610
  // Show success message
611
+ showManagementMessage(`Student ${studentName} removed successfully!`);
612
+
613
+ // Reload students in dropdown
614
+ loadStudentsForManagement();
615
 
616
+ // Reset selection
617
+ document.getElementById('student-to-remove').value = '';
618
  } else {
619
  showManagementMessage('No students found for this class/form', 'error');
620
  }
621
  });
 
 
 
 
622
  });
623
  </script>
624
  <script>