From 9d4183f8b33927e271631b5d570b71aa0af3560f Mon Sep 17 00:00:00 2001 From: Clayzxr Date: Tue, 21 Jan 2025 15:54:38 +0100 Subject: [PATCH] Consult students from DB --- .../mobility/(_islands)/ConsultStudents.tsx | 67 +++++++++++++++++++ routes/(apps)/mobility/partials/students.tsx | 5 +- 2 files changed, 70 insertions(+), 2 deletions(-) diff --git a/routes/(apps)/mobility/(_islands)/ConsultStudents.tsx b/routes/(apps)/mobility/(_islands)/ConsultStudents.tsx index e69de29..552b88b 100644 --- a/routes/(apps)/mobility/(_islands)/ConsultStudents.tsx +++ b/routes/(apps)/mobility/(_islands)/ConsultStudents.tsx @@ -0,0 +1,67 @@ +import { useEffect, useState } from "preact/hooks"; + +interface Student { + id: number; + firstName: string; + lastName: string; + email: string; + promotion: string; +} + +export default function ConsultStudents() { + const [students, setStudents] = useState([]); + const [error, setError] = useState(null); + + useEffect(() => { + const fetchStudents = async () => { + try { + const response = await fetch("/mobility/api/insert_students"); + + if (!response.ok) { + throw new Error(`Error fetching students: ${response.statusText}`); + } + + const data: Student[] = await response.json(); + setStudents(data); + } catch (err) { + console.error("Error fetching students:", err); + setError("Failed to load students. Please try again later."); + } + }; + + fetchStudents(); + }, []); + + return ( +
+

Consult Students

+ {error &&

{error}

} + {students.length === 0 ? ( +

No students found.

+ ) : ( + + + + + + + + + + + + {students.map((student) => ( + + + + + + + + ))} + +
IDFirst NameLast NameEmailPromotion
{student.id}{student.firstName}{student.lastName}{student.email}{student.promotion}
+ )} +
+ ); +} diff --git a/routes/(apps)/mobility/partials/students.tsx b/routes/(apps)/mobility/partials/students.tsx index bbc0d34..f3c7cb9 100644 --- a/routes/(apps)/mobility/partials/students.tsx +++ b/routes/(apps)/mobility/partials/students.tsx @@ -1,7 +1,7 @@ import { RouteConfig } from "$fresh/server.ts"; import UploadStudents from "../(_islands)/UploadStudents.tsx"; -//import ConsultStudents from "../(_components)/ConsultStudents.tsx"; -//import EditStudents from "../(_components)/EditStudents.tsx"; +import ConsultStudents from "../(_islands)/ConsultStudents.tsx"; +//import EditStudents from "../(_islands)/EditStudents.tsx"; export const config: RouteConfig = { skipAppWrapper: false, @@ -14,6 +14,7 @@ export default function Students() {

Manage Promotions


+ ); }