XLSX- css and xlsx-mail
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
const BASE_URL = "http://localhost:8080/api";
|
||||
const XLSX = require("xlsx-js-style");
|
||||
|
||||
// Helper function for handling fetch requests
|
||||
const fetchData = async (url, options) => {
|
||||
@@ -138,30 +139,123 @@ export const updateCourseStatus = async (courseId) => {
|
||||
};
|
||||
|
||||
// Send email
|
||||
export const sendEmail = async (emailData) => {
|
||||
console.log("Sending email with data:", emailData);
|
||||
const handleSendEmail = async (teacher, teacherData) => {
|
||||
const workbook = XLSX.utils.book_new();
|
||||
|
||||
// Validate input
|
||||
// if (!emailData.to || !emailData.subject || !emailData.message) {
|
||||
// const errorMessage = "Missing required fields: to, subject, message";
|
||||
// console.error(errorMessage);
|
||||
// throw new Error(errorMessage);
|
||||
// }
|
||||
const headerInfo = [
|
||||
["Somaiya Vidyavihar University"],
|
||||
["K. J. SOMAIYA COLLEGE OF ENGINEERING"],
|
||||
[
|
||||
"Appointment of Internal Examiners for Paper Setting / OR/PR/Assessment/Reassessment",
|
||||
],
|
||||
["Class: B Tech/M Tech/Honour/Minor"],
|
||||
["Department - Computer Engineering"],
|
||||
[],
|
||||
];
|
||||
|
||||
const tableHeaders = [
|
||||
[
|
||||
"Sr No",
|
||||
"Semester",
|
||||
"Course Code",
|
||||
"Course Name",
|
||||
"Exam Type",
|
||||
"Year",
|
||||
"Marks",
|
||||
"Surname",
|
||||
"First Name",
|
||||
"Middle Name",
|
||||
"Affiliation/College",
|
||||
"Highest Qualification",
|
||||
"Career Experience",
|
||||
"Oral/Practical",
|
||||
"Assessment",
|
||||
"Reassessment",
|
||||
"Paper Setting",
|
||||
"Moderation",
|
||||
"PwD Paper Setting",
|
||||
],
|
||||
];
|
||||
|
||||
const dataRows = teacherData.map((row, index) => [
|
||||
index + 1,
|
||||
row.semester,
|
||||
row.courseCode,
|
||||
row.courseName,
|
||||
row.examType,
|
||||
row.year,
|
||||
row.marks,
|
||||
row.surname,
|
||||
row.firstName,
|
||||
row.middleName,
|
||||
row.affiliation,
|
||||
row.qualification,
|
||||
row.experience,
|
||||
row.oralPractical,
|
||||
row.assessment,
|
||||
row.reassessment,
|
||||
row.paperSetting,
|
||||
row.moderation,
|
||||
row.pwdPaperSetting,
|
||||
]);
|
||||
|
||||
const sheetData = [...headerInfo, ...tableHeaders, ...dataRows];
|
||||
const worksheet = XLSX.utils.aoa_to_sheet(sheetData);
|
||||
XLSX.utils.book_append_sheet(workbook, worksheet, 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);
|
||||
const recipientEmail = prompt(`Enter recipient email for ${teacher}:`);
|
||||
formData.append("recipientEmail", recipientEmail);
|
||||
formData.append("file", file);
|
||||
|
||||
try {
|
||||
const response = await sendEmail(formData);
|
||||
alert(`Email sent successfully to ${recipientEmail}`);
|
||||
console.log("Response from server:", response);
|
||||
} catch (error) {
|
||||
console.error("Error sending email:", error);
|
||||
alert("Failed to send email.");
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
export const sendEmail = async (formData) => {
|
||||
try {
|
||||
const url = `${BASE_URL}/send-email`;
|
||||
const response = await fetchData(url, {
|
||||
const response = await fetch(url, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify(emailData), // Pass the email data to the server
|
||||
body: formData, // Directly pass FormData
|
||||
});
|
||||
|
||||
console.log("Email sent successfully:", response);
|
||||
return response;
|
||||
if (!response.ok) {
|
||||
let errorDetails = {};
|
||||
try {
|
||||
errorDetails = await response.json();
|
||||
} catch (err) {
|
||||
console.warn("Failed to parse error details:", err);
|
||||
}
|
||||
throw new Error(
|
||||
`Error: ${response.statusText} (${response.status}) - ${
|
||||
errorDetails.message || "No details available"
|
||||
}`
|
||||
);
|
||||
}
|
||||
|
||||
return await response.json();
|
||||
} catch (error) {
|
||||
console.error("Error sending email:", error.message);
|
||||
console.error( error.message);
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user