From c9cb423ae222344e31d783f12ed14d28e6e75160 Mon Sep 17 00:00:00 2001 From: Clayzxr Date: Mon, 27 Jan 2025 13:41:55 +0100 Subject: [PATCH] Select promotion in EditMobility --- .../mobility/(_islands)/EditMobility.tsx | 201 ++++++++++-------- 1 file changed, 117 insertions(+), 84 deletions(-) diff --git a/routes/(apps)/mobility/(_islands)/EditMobility.tsx b/routes/(apps)/mobility/(_islands)/EditMobility.tsx index db1b80d..c0f33a8 100644 --- a/routes/(apps)/mobility/(_islands)/EditMobility.tsx +++ b/routes/(apps)/mobility/(_islands)/EditMobility.tsx @@ -3,10 +3,12 @@ import { useEffect, useState } from "preact/hooks"; export default function EditMobility() { const [mobilityData, setMobilityData] = useState([]); const [promotions, setPromotions] = useState([]); + const [selectedPromotion, setSelectedPromotion] = useState("all"); const [isSaving, setIsSaving] = useState(false); useEffect(() => { async function fetchMobilityData() { + console.log("EditMobility: Fetching data from API..."); const response = await fetch("/mobility/api/insert-mobility"); const data = await response.json(); console.log("EditMobility: Data fetched successfully:", data); @@ -18,7 +20,7 @@ export default function EditMobility() { (mobility: any) => mobility.studentId === student.id ); return { - id: existingMobility ? existingMobility.id : null, + id: existingMobility ? existingMobility.id : null, studentId: student.id, firstName: student.firstName, lastName: student.lastName, @@ -52,7 +54,9 @@ export default function EditMobility() { const handleSave = async () => { setIsSaving(true); + try { + console.log("EditMobility: Sending data to API..."); const response = await fetch("/mobility/api/insert-mobility", { method: "POST", headers: { "Content-Type": "application/json" }, @@ -74,100 +78,129 @@ export default function EditMobility() { } }; + const filteredData = + selectedPromotion === "all" + ? mobilityData + : mobilityData.filter((entry) => entry.promotionId === selectedPromotion); + const groupedData = promotions.map((promo) => ({ promotion: promo.name, - students: mobilityData.filter( - (entry) => entry.promotionId === promo.id - ), + students: filteredData.filter((entry) => entry.promotionId === promo.id), })); return (

Edit Mobility

+ +
+ + +
+ {groupedData.map((group) => (
-

Promotion: {group.promotion}

- - - - - - - - - - - - - - - - {group.students.map((entry) => ( - - - - - - - - - - - - ))} - -
IDFirst NameLast NameStart DateEnd DateWeeks CountDestination CountryDestination NameStatus
{entry.studentId}{entry.firstName}{entry.lastName} - - handleChange(entry.studentId, "startDate", e.target.value) - } - /> - - - handleChange(entry.studentId, "endDate", e.target.value) - } - /> - {entry.weeksCount || "0"} - - handleChange( - entry.studentId, - "destinationCountry", - e.target.value - ) - } - /> - - - handleChange(entry.studentId, "destinationName", e.target.value) - } - /> - - -
+ {group.students.length > 0 && ( + <> +

Promotion: {group.promotion}

+ + + + + + + + + + + + + + + + {group.students.map((entry) => ( + + + + + + + + + + + + ))} + +
IDFirst NameLast NameStart DateEnd DateWeeks CountDestination CountryDestination NameStatus
{entry.studentId}{entry.firstName}{entry.lastName} + + handleChange(entry.studentId, "startDate", e.target.value) + } + /> + + + handleChange(entry.studentId, "endDate", e.target.value) + } + /> + {entry.weeksCount || "N/A"} + + handleChange( + entry.studentId, + "destinationCountry", + e.target.value + ) + } + /> + + + handleChange(entry.studentId, "destinationName", e.target.value) + } + /> + + +
+ + )}
))} +