import React, { useState, useEffect } from "react";
import axios from "axios";
import * as XLSX from "xlsx-js-style";
import { sendEmail } from "../api";
import { createExcelBook } from "../api";
const ConsolidatedTable = () => {
const [data, setData] = useState([]);
const [loading, setLoading] = useState(true);
const [currentPage, setCurrentPage] = useState(1);
const tablesPerPage = 5;
const [expandedTeacher, setExpandedTeacher] = useState(null);
useEffect(() => {
const fetchData = async () => {
try {
const response = await axios.get(
"http://localhost:8080/api/table/consolidated-table"
);
setData(response.data);
setLoading(false);
} catch (error) {
console.error("Error fetching table data:", error);
setLoading(false);
}
};
fetchData();
}, []);
if (loading) {
return
Loading...
;
}
// Extract unique faculty names
const uniqueTeachers = [...new Set(data.map((row) => row.Name))];
// Pagination
const indexOfLastTable = currentPage * tablesPerPage;
const indexOfFirstTable = indexOfLastTable - tablesPerPage;
const currentTeachers = uniqueTeachers.slice(indexOfFirstTable, indexOfLastTable);
const totalPages = Math.ceil(uniqueTeachers.length / tablesPerPage);
const handleNextPage = () => {
if (currentPage < totalPages) setCurrentPage((prevPage) => prevPage + 1);
};
const handlePrevPage = () => {
if (currentPage > 1) setCurrentPage((prevPage) => prevPage - 1);
};
const createExcelFile = (teacherData, teacherName) => {
const workbook = createExcelBook(teacherData, teacherName);
XLSX.writeFile(workbook, `${teacherName.replace(/\s+/g, "_")}_Table.xlsx`);
};
const bulkDownload = () => {
uniqueTeachers.forEach((teacher) => {
const teacherData = data.filter((row) => row.Name === teacher);
createExcelFile(teacherData, teacher);
});
};
const handleSendEmail = async (teacher, teacherData) => {
const facultyId = teacherData[0].facultyId; // This assumes all rows for a teacher have the same facultyId
try {
const response = await axios.get(
`http://localhost:8080/api/faculty/${facultyId}`
);
const facultyEmail = response.data.email;
const workbook = createExcelBook(teacherData, teacher);
const fileName = `${teacher.replace(/\s+/g, "_")}_table.xlsx`;
const excelBlob = XLSX.write(workbook, {
bookType: "xlsx",
type: "array",
});
const file = new File([excelBlob], fileName, {
type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
});
const formData = new FormData();
formData.append("teacher", teacher);
formData.append("fileName", fileName);
formData.append("recipientEmail", facultyEmail);
formData.append("file", file);
try {
const response = await sendEmail(formData);
alert(`Email sent successfully to ${facultyEmail}`);
console.log("Response from server:", response);
} catch (error) {
console.error("Error sending email:", error);
alert("Failed to send email.");
}
} catch (error) {
console.error("Error fetching Faculty data:", error);
alert("Failed to fetch faculty data.");
}
};
// Send emails to all teachers
const sendEmailsToAllTeachers = async () => {
for (let teacher of uniqueTeachers) {
const teacherData = data.filter((row) => row.Name === teacher);
await handleSendEmail(teacher, teacherData); // Wait for each email to be sent before proceeding to the next
}
alert("Emails sent to all teachers.");
};
return (
Faculty Tables with Download Options
{currentTeachers.map((teacher, index) => {
const teacherData = data.filter((row) => row.Name === teacher);
return (
setExpandedTeacher(expandedTeacher === teacher ? null : teacher)}
>
{teacher}'s Table
{expandedTeacher === teacher && (
| Semester |
Course Code |
Course Name |
Exam Type |
Year |
Marks |
Name |
Affiliation/College |
Highest Qualification |
Career Experience |
Oral/Practical |
Assessment |
Reassessment |
Paper Setting |
Moderation |
PwD Paper Setting |
{teacherData.map((row, idx) => (
| {row.semester} |
{row.courseCode} |
{row.courseName} |
{row.examType} |
{row.year} |
{row.marks} |
{row.Name} |
{row.affiliation} |
{row.qualification} |
{row.experience} |
{row.oralPractical} |
{row.assessment} |
{row.reassessment} |
{row.paperSetting} |
{row.moderation} |
{row.pwdPaperSetting} |
))}
)}
);
})}
{/* Pagination controls */}
Page {currentPage} of {totalPages}
);
};
export default ConsolidatedTable;