From a50bfbe97558d332786035fb52842871becf881f Mon Sep 17 00:00:00 2001 From: Clayzxr Date: Mon, 27 Jan 2025 13:38:06 +0100 Subject: [PATCH] Select promotion in ConsultMobility --- .../mobility/(_islands)/ConsultMobility.tsx | 152 +++++++++++------- routes/(apps)/mobility/types.d.ts | 18 --- 2 files changed, 93 insertions(+), 77 deletions(-) diff --git a/routes/(apps)/mobility/(_islands)/ConsultMobility.tsx b/routes/(apps)/mobility/(_islands)/ConsultMobility.tsx index a27c2c6..74da520 100644 --- a/routes/(apps)/mobility/(_islands)/ConsultMobility.tsx +++ b/routes/(apps)/mobility/(_islands)/ConsultMobility.tsx @@ -1,90 +1,124 @@ import { useEffect, useState } from "preact/hooks"; export default function ConsultMobility() { - const [data, setData] = useState< - | { - promotions?: Promotion[]; - students?: Student[]; - mobilities?: Mobility[]; - } - | null - >(null); + const [mobilityData, setMobilityData] = useState([]); + const [promotions, setPromotions] = useState([]); + const [selectedPromotion, setSelectedPromotion] = useState("all"); const [error, setError] = useState(null); useEffect(() => { const fetchData = async () => { - console.log("ConsultMobility: Fetching data from API..."); try { + console.log("ConsultMobility: Fetching data from API..."); const response = await fetch("/mobility/api/insert-mobility"); - console.log("ConsultMobility: API response status:", response.status); - if (!response.ok) { throw new Error(`Error fetching data: ${response.statusText}`); } const result = await response.json(); console.log("ConsultMobility: Data fetched successfully:", result); - setData(result); + + setPromotions(result.promotions); + + const mergedData = result.students.map((student: any) => { + const existingMobility = result.mobilities.find( + (mobility: any) => mobility.studentId === student.id + ); + return { + id: existingMobility ? existingMobility.id : null, + studentId: student.id, + firstName: student.firstName, + lastName: student.lastName, + startDate: existingMobility?.startDate || null, + endDate: existingMobility?.endDate || null, + weeksCount: existingMobility?.weeksCount || null, + destinationCountry: existingMobility?.destinationCountry || null, + destinationName: existingMobility?.destinationName || null, + mobilityStatus: existingMobility?.mobilityStatus || "N/A", + promotionId: student.promotionId, + promotionName: student.promotionName, + }; + }); + + setMobilityData(mergedData); } catch (err) { console.error("ConsultMobility: Error fetching data:", err); - setError("Failed to load mobility data. Please try again later."); + setError("Failed to load data. Please try again later."); } }; fetchData(); }, []); - if (error) { - return

{error}

; - } - - if (!data?.promotions) { - return

No promotions found.

; - } + const filteredData = + selectedPromotion === "all" + ? mobilityData + : mobilityData.filter((entry) => entry.promotionId === selectedPromotion); return (

Consult Mobility

- {data.promotions.map((promo) => ( + {error &&

{error}

} + +
+ + +
+ + {promotions.map((promo) => (
-

Promotion: {promo.name}

- - - - - - - - - - - - - - - - {data.students - ?.filter((student) => student.promotionId === promo.id) - .map((student) => { - const mobility = data.mobilities?.find((mob) => - mob.studentId === student.id - ); - return ( - - - - - - - - - - - - ); - })} - -
IDFirst NameLast NameStart DateEnd DateWeeks CountDestination CountryDestination NameStatus
{student.id}{student.firstName}{student.lastName}{mobility?.startDate || "N/A"}{mobility?.endDate || "N/A"}{mobility?.weeksCount ?? "0"}{mobility?.destinationCountry || "N/A"}{mobility?.destinationName || "N/A"}{mobility?.mobilityStatus || "N/A"}
+ {selectedPromotion === "all" || selectedPromotion === promo.id ? ( + <> +

Promotion: {promo.name}

+ + + + + + + + + + + + + + + + {filteredData + .filter((entry) => entry.promotionId === promo.id) + .map((entry) => ( + + + + + + + + + + + + ))} + +
IDFirst NameLast NameStart DateEnd DateWeeks CountDestination CountryDestination NameStatus
{entry.studentId}{entry.firstName}{entry.lastName}{entry.startDate || "N/A"}{entry.endDate || "N/A"}{entry.weeksCount || "N/A"}{entry.destinationCountry || "N/A"}{entry.destinationName || "N/A"}{entry.mobilityStatus}
+ + ) : null}
))}
diff --git a/routes/(apps)/mobility/types.d.ts b/routes/(apps)/mobility/types.d.ts index 89451a7..7227ba5 100644 --- a/routes/(apps)/mobility/types.d.ts +++ b/routes/(apps)/mobility/types.d.ts @@ -2,24 +2,6 @@ interface Promotion { id: number; name: string; } - -interface Student { - id: string; - firstName: string; - lastName: string; - promotionId: number; -} - -interface Mobility { - id: number; - studentId: string; - startDate: string | null; - endDate: string | null; - weeksCount: number | null; - destinationCountry: string | null; - destinationName: string | null; - mobilityStatus: string; -} interface MobilityData { id: number | null;