Working DB with table promotions
This commit is contained in:
@@ -11,83 +11,63 @@ interface Student {
|
||||
lastName: string;
|
||||
email: string;
|
||||
promotionId: number;
|
||||
promotionName: string;
|
||||
}
|
||||
|
||||
export default function ConsultStudents() {
|
||||
const [promotions, setPromotions] = useState<Promotion[]>([]);
|
||||
const [studentsByPromotion, setStudentsByPromotion] = useState<Record<number, Student[]>>({});
|
||||
const [data, setData] = useState<{ promotions: Promotion[]; students: Student[] } | null>(null);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
const fetchPromotionsAndStudents = async () => {
|
||||
const fetchData = async () => {
|
||||
try {
|
||||
const promoResponse = await fetch("/mobility/api/promotions");
|
||||
if (!promoResponse.ok) {
|
||||
throw new Error(`Error fetching promotions: ${promoResponse.statusText}`);
|
||||
const response = await fetch("/mobility/api/insert_students");
|
||||
if (!response.ok) {
|
||||
throw new Error(`Error fetching data: ${response.statusText}`);
|
||||
}
|
||||
const promos: Promotion[] = await promoResponse.json();
|
||||
setPromotions(promos);
|
||||
|
||||
const studentsResponse = await fetch("/mobility/api/insert_students");
|
||||
if (!studentsResponse.ok) {
|
||||
throw new Error(`Error fetching students: ${studentsResponse.statusText}`);
|
||||
}
|
||||
const students: Student[] = await studentsResponse.json();
|
||||
|
||||
const grouped: Record<number, Student[]> = {};
|
||||
for (const student of students) {
|
||||
if (!grouped[student.promotionId]) {
|
||||
grouped[student.promotionId] = [];
|
||||
}
|
||||
grouped[student.promotionId].push(student);
|
||||
}
|
||||
setStudentsByPromotion(grouped);
|
||||
const result = await response.json();
|
||||
setData(result);
|
||||
} catch (err) {
|
||||
console.error("Error fetching promotions or students:", err);
|
||||
console.error("Error fetching data:", err);
|
||||
setError("Failed to load data. Please try again later.");
|
||||
}
|
||||
};
|
||||
|
||||
fetchPromotionsAndStudents();
|
||||
fetchData();
|
||||
}, []);
|
||||
|
||||
return (
|
||||
<section>
|
||||
<h2>Consult Students</h2>
|
||||
{error && <p className="error">{error}</p>}
|
||||
{promotions.length === 0 ? (
|
||||
<p>No promotions found.</p>
|
||||
) : (
|
||||
promotions.map((promo) => (
|
||||
<div key={promo.id}>
|
||||
<h3>Promotion: {promo.name}</h3>
|
||||
{studentsByPromotion[promo.id]?.length ? (
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>ID</th>
|
||||
<th>First Name</th>
|
||||
<th>Last Name</th>
|
||||
<th>Email</th>
|
||||
{data?.promotions.map((promo) => (
|
||||
<div key={promo.id}>
|
||||
<h3>Promotion: {promo.name}</h3>
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>ID</th>
|
||||
<th>First Name</th>
|
||||
<th>Last Name</th>
|
||||
<th>Email</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{data.students
|
||||
.filter((student) => student.promotionId === promo.id)
|
||||
.map((student) => (
|
||||
<tr key={student.id}>
|
||||
<td>{student.id}</td>
|
||||
<td>{student.firstName}</td>
|
||||
<td>{student.lastName}</td>
|
||||
<td>{student.email}</td>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{studentsByPromotion[promo.id].map((student) => (
|
||||
<tr key={student.id}>
|
||||
<td>{student.id}</td>
|
||||
<td>{student.firstName}</td>
|
||||
<td>{student.lastName}</td>
|
||||
<td>{student.email}</td>
|
||||
</tr>
|
||||
))}
|
||||
</tbody>
|
||||
</table>
|
||||
) : (
|
||||
<p>No students in this promotion.</p>
|
||||
)}
|
||||
</div>
|
||||
))
|
||||
)}
|
||||
))}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
))}
|
||||
</section>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -11,18 +11,15 @@ export default function UploadStudents() {
|
||||
if (input.files && input.files.length > 0) {
|
||||
fileData.value = input.files[0];
|
||||
statusMessage.value = "File selected: " + input.files[0].name;
|
||||
console.log("File selected:", input.files[0].name);
|
||||
} else {
|
||||
fileData.value = null;
|
||||
statusMessage.value = "No file selected";
|
||||
}
|
||||
};
|
||||
|
||||
const confirmUpload = () => {
|
||||
console.log("Confirm Upload");
|
||||
const confirmUpload = async () => {
|
||||
if (!fileData.value) {
|
||||
statusMessage.value = "Please select a file before confirming upload.";
|
||||
console.error("Error: No file selected.");
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -30,42 +27,31 @@ export default function UploadStudents() {
|
||||
const reader = new FileReader();
|
||||
|
||||
reader.onload = async (e) => {
|
||||
try {
|
||||
const arrayBuffer = e.target?.result as ArrayBuffer;
|
||||
const workbook = XLSX.read(arrayBuffer, { type: "array" });
|
||||
const arrayBuffer = e.target?.result as ArrayBuffer;
|
||||
const workbook = XLSX.read(arrayBuffer, { type: "array" });
|
||||
|
||||
for (const sheetName of workbook.SheetNames) {
|
||||
const sheet = workbook.Sheets[sheetName];
|
||||
const data = XLSX.utils.sheet_to_json(sheet, {
|
||||
header: ["Nom", "Prénom", "Mail"],
|
||||
range: 1, // Ignorer les en-têtes
|
||||
});
|
||||
for (const sheetName of workbook.SheetNames) {
|
||||
const sheet = workbook.Sheets[sheetName];
|
||||
const data = XLSX.utils.sheet_to_json(sheet, {
|
||||
header: ["Nom", "Prénom", "Mail"],
|
||||
range: 1, // Ignorer les en-têtes
|
||||
});
|
||||
|
||||
console.log(`Data from sheet ${sheetName}:`, data);
|
||||
const response = await fetch("/mobility/api/insert_students", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ promoName: sheetName, data }),
|
||||
});
|
||||
|
||||
const response = await fetch("/mobility/api/insert_students", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ promoName: sheetName, data }),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(
|
||||
`Failed to insert data for promotion ${sheetName}`,
|
||||
);
|
||||
}
|
||||
if (!response.ok) {
|
||||
throw new Error(`Failed to insert data for promotion ${sheetName}`);
|
||||
}
|
||||
|
||||
statusMessage.value = "Data uploaded and inserted successfully!";
|
||||
} catch (error) {
|
||||
console.error("Error processing the file:", error);
|
||||
statusMessage.value =
|
||||
"Error processing the file. Please check its format.";
|
||||
}
|
||||
|
||||
statusMessage.value = "Data uploaded and inserted successfully!";
|
||||
};
|
||||
|
||||
reader.onerror = (e) => {
|
||||
console.error("FileReader error:", e);
|
||||
reader.onerror = () => {
|
||||
statusMessage.value = "Error reading the file.";
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user