mailing-part 50%

This commit is contained in:
amNobodyyy
2025-01-23 22:42:41 +05:30
parent d96d27154b
commit b39cb67b0f
4 changed files with 154 additions and 193 deletions

View File

@@ -1,197 +1,7 @@
// import React, { useState, useEffect } from "react";
// import axios from "axios";
// const ConsolidatedTable = () => {
// const [data, setData] = useState([]);
// const [loading, setLoading] = useState(true);
// 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 <div>Loading...</div>;
// }
// return (
// <div>
// <h1>Consolidated Table</h1>
// <table border="1" style={{ width: "100%", textAlign: "left" }}>
// <thead>
// <tr>
// <th>Semester</th>
// <th>Course Code</th>
// <th>Course Name</th>
// <th>Exam Type</th>
// <th>Year</th>
// <th>Marks</th>
// <th>Name</th>
// <th>Affiliation/College</th>
// <th>Highest Qualification</th>
// <th>Career Experience</th>
// <th>Oral/Practical</th>
// <th>Assessment</th>
// <th>Reassessment</th>
// <th>Paper Setting</th>
// <th>Moderation</th>
// <th>PwD Paper Setting</th>
// </tr>
// </thead>
// <tbody>
// {data.map((row, index) => (
// <tr key={index}>
// <td>{row.semester}</td>
// <td>{row.courseCode}</td>
// <td>{row.courseName}</td>
// <td>{row.examType}</td>
// <td>{row.year}</td>
// <td>{row.marks}</td>
// <td>{row.Name}</td>
// <td>{row.affiliation}</td>
// <td>{row.qualification}</td>
// <td>{row.experience}</td>
// <td>{row.oralPractical}</td>
// <td>{row.assessment}</td>
// <td>{row.reassessment}</td>
// <td>{row.paperSetting}</td>
// <td>{row.moderation}</td>
// <td>{row.pwdPaperSetting}</td>
// </tr>
// ))}
// </tbody>
// </table>
// </div>
// );
// };
// export default ConsolidatedTable;
// import React, { useState, useEffect } from "react";
// import axios from "axios";
// import { CSVLink } from "react-csv";
// const ConsolidatedTable = () => {
// const [data, setData] = useState([]);
// const [loading, setLoading] = useState(true);
// 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 <div>Loading...</div>;
// }
// // Extract unique faculty names
// const uniqueTeachers = [...new Set(data.map((row) => row.Name))];
// return (
// <div>
// <h1>Faculty Tables with Download Option</h1>
// {uniqueTeachers.map((teacher, index) => {
// // Filter rows for the current teacher
// const teacherData = data.filter((row) => row.Name === teacher);
// return (
// <div key={index} style={{ marginBottom: "20px" }}>
// <h2>{teacher}'s Table</h2>
// <table border="1" style={{ width: "100%", textAlign: "left", marginBottom: "10px" }}>
// <thead>
// <tr>
// <th>Semester</th>
// <th>Course Code</th>
// <th>Course Name</th>
// <th>Exam Type</th>
// <th>Year</th>
// <th>Marks</th>
// <th>Name</th>
// <th>Affiliation/College</th>
// <th>Highest Qualification</th>
// <th>Career Experience</th>
// <th>Oral/Practical</th>
// <th>Assessment</th>
// <th>Reassessment</th>
// <th>Paper Setting</th>
// <th>Moderation</th>
// <th>PwD Paper Setting</th>
// </tr>
// </thead>
// <tbody>
// {teacherData.map((row, idx) => (
// <tr key={idx}>
// <td>{row.semester}</td>
// <td>{row.courseCode}</td>
// <td>{row.courseName}</td>
// <td>{row.examType}</td>
// <td>{row.year}</td>
// <td>{row.marks}</td>
// <td>{row.Name}</td>
// <td>{row.affiliation}</td>
// <td>{row.qualification}</td>
// <td>{row.experience}</td>
// <td>{row.oralPractical}</td>
// <td>{row.assessment}</td>
// <td>{row.reassessment}</td>
// <td>{row.paperSetting}</td>
// <td>{row.moderation}</td>
// <td>{row.pwdPaperSetting}</td>
// </tr>
// ))}
// </tbody>
// </table>
// {/* CSV Download Button */}
// <CSVLink
// data={teacherData}
// filename={`${teacher.replace(/\s+/g, "_")}_table.csv`}
// className="btn btn-primary"
// style={{
// padding: "10px 15px",
// backgroundColor: "#007bff",
// color: "white",
// textDecoration: "none",
// borderRadius: "5px",
// }}
// >
// Download CSV
// </CSVLink>
// </div>
// );
// })}
// </div>
// );
// };
// export default ConsolidatedTable;
import React, { useState, useEffect, useRef } from "react";
import axios from "axios";
import { CSVLink } from "react-csv";
import { sendEmail } from "../api";
const ConsolidatedTable = () => {
const [data, setData] = useState([]);
@@ -201,7 +11,9 @@ const ConsolidatedTable = () => {
useEffect(() => {
const fetchData = async () => {
try {
const response = await axios.get("http://localhost:8080/api/table/consolidated-table");
const response = await axios.get(
"http://localhost:8080/api/table/consolidated-table"
);
setData(response.data);
setLoading(false);
} catch (error) {
@@ -229,6 +41,46 @@ const ConsolidatedTable = () => {
});
};
const handleSendEmail = async (teacher, teacherData) => {
const csvHeaders = Object.keys(teacherData[0]);
const csvRows = teacherData.map((row) =>
csvHeaders
.map(
(header) => `"${row[header]?.toString().replace(/"/g, '""') || ""}"`
)
.join(",")
);
const csvContent = [csvHeaders.join(","), ...csvRows].join("\n");
const fileName = `${teacher.replace(/\s+/g, "_")}_table.csv`;
try {
const recipientEmail = prompt(`Enter recipient email for ${teacher}:`);
if (!recipientEmail) {
alert("Email is required!");
return;
}
// Create email data object
const emailData = {
teacher,
csvData: csvContent,
fileName,
recipientEmail,
};
// Call sendEmail from api.js
const response = await sendEmail(emailData);
// Handle success
alert(`Email sent successfully to ${recipientEmail}`);
console.log("Response from server:", response);
alert(`Email sent successfully to ${recipientEmail}`);
} catch (error) {
console.error("Error sending email:", error);
alert("Failed to send email.");
}
};
return (
<div>
<h1>Faculty Tables with Download Options</h1>
@@ -285,7 +137,14 @@ const ConsolidatedTable = () => {
return (
<div key={index} style={{ marginBottom: "20px" }}>
<h2>{teacher}'s Table</h2>
<table border="1" style={{ width: "100%", textAlign: "left", marginBottom: "10px" }}>
<table
border="1"
style={{
width: "100%",
textAlign: "left",
marginBottom: "10px",
}}
>
<thead>
<tr>
<th>Semester</th>
@@ -345,6 +204,20 @@ const ConsolidatedTable = () => {
>
Download {teacher}'s CSV
</CSVLink>
{/* Send Email Button */}
<button
onClick={() => handleSendEmail(teacher, teacherData)}
className="btn btn-secondary"
style={{
padding: "10px 15px",
backgroundColor: "#6c757d",
color: "white",
textDecoration: "none",
borderRadius: "5px",
}}
>
Send {teacher}'s CSV via Email
</button>
</div>
);
})}

View File

@@ -137,3 +137,31 @@ export const updateCourseStatus = async (courseId) => {
});
};
// Send email
export const sendEmail = async (emailData) => {
console.log("Sending email with data:", emailData);
// Validate input
// if (!emailData.to || !emailData.subject || !emailData.message) {
// const errorMessage = "Missing required fields: to, subject, message";
// console.error(errorMessage);
// throw new Error(errorMessage);
// }
try {
const url = `${BASE_URL}/send-email`;
const response = await fetchData(url, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(emailData), // Pass the email data to the server
});
console.log("Email sent successfully:", response);
return response;
} catch (error) {
console.error("Error sending email:", error.message);
throw error;
}
};