Trying to use the DB (not working)

This commit is contained in:
Clayzxr
2025-01-18 23:38:29 +01:00
parent 30ffc90b69
commit 4057bb488c
5 changed files with 33 additions and 115 deletions
@@ -1,82 +0,0 @@
import { useSignal } from "@preact/signals";
import Papa from "https://cdn.skypack.dev/papaparse";
type Student = {
firstName: string;
lastName: string;
email: string;
};
type Promotion = {
name: string;
students: Student[];
};
export default function SaveStudents() {
const promotions = useSignal<Promotion[]>([]);
const statusMessage = useSignal<string>("");
const loadCSV = async () => {
try {
const response = await fetch("/api/students"); // Assurez-vous que l'API est appelée correctement
if (!response.ok) {
throw new Error("Failed to load CSV file");
}
const csvText = await response.text(); // Lire le contenu en texte
const parsedData = Papa.parse(csvText, {
header: true,
skipEmptyLines: true,
});
const groupedPromotions: Record<string, Student[]> = {};
parsedData.data.forEach((row: any) => {
const { promotion, firstName, lastName, email } = row;
if (!groupedPromotions[promotion]) {
groupedPromotions[promotion] = [];
}
groupedPromotions[promotion].push({ firstName, lastName, email });
});
const loadedPromotions = Object.entries(groupedPromotions).map(
([name, students]) => ({
name,
students,
})
);
promotions.value = loadedPromotions;
statusMessage.value = "Data loaded successfully!";
} catch (error) {
console.error("Error loading CSV file:", error);
statusMessage.value = "Failed to load data. Please try again.";
}
};
// Charger les données CSV dès le chargement du composant
loadCSV();
return (
<div>
<h2>Loaded Promotions</h2>
<button onClick={loadCSV}>Actualiser</button>
<p>{statusMessage.value}</p>
<ul>
{promotions.value.map((promotion) => (
<li key={promotion.name}>
<strong>{promotion.name}</strong>
<ul>
{promotion.students.map((student, index) => (
<li key={index}>
{student.firstName} {student.lastName} - {student.email}
</li>
))}
</ul>
</li>
))}
</ul>
</div>
);
}
@@ -18,7 +18,7 @@ export default function UploadStudents() {
}
};
const handleUpload = async () => {
const handleUpload = () => {
if (!fileData.value) {
statusMessage.value = "Please select a file before confirming upload.";
return;
@@ -39,13 +39,19 @@ export default function UploadStudents() {
});
console.log(`Data from sheet ${sheetName}:`, data);
await insertIntoMobility(data as Array<{ firstName: string; lastName: string; email: string }>, sheetName);
await insertIntoMobility(
data as Array<
{ firstName: string; lastName: string; email: string }
>,
sheetName,
);
}
statusMessage.value = "File uploaded and data inserted successfully!";
} catch (error) {
console.error("Error reading or inserting file:", error);
statusMessage.value = "Error processing the file. Please check its format.";
statusMessage.value =
"Error processing the file. Please check its format.";
}
};
+2 -5
View File
@@ -1,13 +1,11 @@
import { RouteConfig } from "$fresh/server.ts";
import UploadStudents from "../(_islands)/UploadStudents.tsx";
//import ConsultStudents from "../(_islands)/ConsultStudents.tsx";
//import EditStudents from "../(_islands)/EditStudents.tsx";
export const config: RouteConfig = {
skipAppWrapper: false,
skipInheritedLayouts: false,
skipAppWrapper: false,
skipInheritedLayouts: false,
};
export default function Students() {
@@ -16,7 +14,6 @@ export default function Students() {
<h1>Manage Promotions</h1>
<UploadStudents />
<hr />
</section>
);
}