import React, { useState, useEffect } from "react"; import axios from "axios"; import * as XLSX from "xlsx-js-style"; const CourseConsolidated = () => { const [data, setData] = useState([]); const [loading, setLoading] = useState(true); const [currentPage, setCurrentPage] = useState(1); const tablesPerPage = 5; const [expandedCourse, setExpandedCourse] = useState(null); useEffect(() => { const fetchData = async () => { try { const response = await axios.get( "http://localhost:8080/api/table/course-consolidated" ); setData(response.data); setLoading(false); } catch (error) { console.error("Error fetching table data:", error); setLoading(false); } }; fetchData(); }, []); if (loading) { return
Loading...
; } // Extract unique courses by courseCode const uniqueCourses = [...new Set(data.map((row) => row.courseCode))]; // Pagination const indexOfLastTable = currentPage * tablesPerPage; const indexOfFirstTable = indexOfLastTable - tablesPerPage; const currentCourses = uniqueCourses.slice( indexOfFirstTable, indexOfLastTable ); const totalPages = Math.ceil(uniqueCourses.length / tablesPerPage); const handleNextPage = () => { if (currentPage < totalPages) setCurrentPage((prevPage) => prevPage + 1); }; const handlePrevPage = () => { if (currentPage > 1) setCurrentPage((prevPage) => prevPage - 1); }; const createExcelFile = (courseData, courseName) => { const workbook = XLSX.utils.book_new(); const worksheet = XLSX.utils.json_to_sheet(courseData); XLSX.utils.book_append_sheet(workbook, worksheet, courseName); XLSX.writeFile(workbook, `${courseName.replace(/\s+/g, "_")}_Table.xlsx`); }; return (

Course Tables with Download Options

{currentCourses.map((courseCode, index) => { const courseData = data.filter( (row) => row.courseCode === courseCode ); const courseName = courseData[0]?.courseName; // Get course name from first item return (
setExpandedCourse( expandedCourse === courseCode ? null : courseCode ) } >

{courseName}'s Table

{expandedCourse === courseCode && ( {courseData.map((row, idx) => ( ))}
Semester Course Code Course Name Exam Type Year Oral/Practical Assessment Reassessment Paper Setting Moderation PwD Paper Setting
{row.semester} {row.courseCode} {row.courseName} {row.examType} {row.year} {row.oralPracticalTeachers && row.oralPracticalTeachers.length > 0 ? (
    {row.oralPracticalTeachers.map((teacher, idx) => (
  • {teacher}
  • ))}
) : ( "N/A" )}
{row.assesmentTeachers && row.assesmentTeachers.length > 0 ? (
    {row.assesmentTeachers.map((teacher, idx) => (
  • {teacher}
  • ))}
) : ( "N/A" )}
{row.reassessmentTeachers && row.reassessmentTeachers.length > 0 ? (
    {row.reassessmentTeachers.map((teacher, idx) => (
  • {teacher}
  • ))}
) : ( "N/A" )}
{row.paperSettingTeachers && row.paperSettingTeachers.length > 0 ? (
    {row.paperSettingTeachers.map((teacher, idx) => (
  • {teacher}
  • ))}
) : ( "N/A" )}
{row.moderationTeachers && row.moderationTeachers.length > 0 ? (
    {row.moderationTeachers.map((teacher, idx) => (
  • {teacher}
  • ))}
) : ( "N/A" )}
{row.pwdPaperSettingTeachers && row.pwdPaperSettingTeachers.length > 0 ? (
    {row.pwdPaperSettingTeachers.map( (teacher, idx) => (
  • {teacher}
  • ) )}
) : ( "N/A" )}
)}
); })}
{/* Pagination controls */}
Page {currentPage} of {totalPages}
); }; export default CourseConsolidated;