Select promotion in ConsultMobility
This commit is contained in:
@@ -1,90 +1,124 @@
|
|||||||
import { useEffect, useState } from "preact/hooks";
|
import { useEffect, useState } from "preact/hooks";
|
||||||
|
|
||||||
export default function ConsultMobility() {
|
export default function ConsultMobility() {
|
||||||
const [data, setData] = useState<
|
const [mobilityData, setMobilityData] = useState<MobilityData[]>([]);
|
||||||
| {
|
const [promotions, setPromotions] = useState<Promotion[]>([]);
|
||||||
promotions?: Promotion[];
|
const [selectedPromotion, setSelectedPromotion] = useState<number | "all">("all");
|
||||||
students?: Student[];
|
|
||||||
mobilities?: Mobility[];
|
|
||||||
}
|
|
||||||
| null
|
|
||||||
>(null);
|
|
||||||
const [error, setError] = useState<string | null>(null);
|
const [error, setError] = useState<string | null>(null);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const fetchData = async () => {
|
const fetchData = async () => {
|
||||||
console.log("ConsultMobility: Fetching data from API...");
|
|
||||||
try {
|
try {
|
||||||
|
console.log("ConsultMobility: Fetching data from API...");
|
||||||
const response = await fetch("/mobility/api/insert-mobility");
|
const response = await fetch("/mobility/api/insert-mobility");
|
||||||
console.log("ConsultMobility: API response status:", response.status);
|
|
||||||
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
throw new Error(`Error fetching data: ${response.statusText}`);
|
throw new Error(`Error fetching data: ${response.statusText}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
const result = await response.json();
|
const result = await response.json();
|
||||||
console.log("ConsultMobility: Data fetched successfully:", result);
|
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) {
|
} catch (err) {
|
||||||
console.error("ConsultMobility: Error fetching data:", 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();
|
fetchData();
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
if (error) {
|
const filteredData =
|
||||||
return <p className="error">{error}</p>;
|
selectedPromotion === "all"
|
||||||
}
|
? mobilityData
|
||||||
|
: mobilityData.filter((entry) => entry.promotionId === selectedPromotion);
|
||||||
if (!data?.promotions) {
|
|
||||||
return <p>No promotions found.</p>;
|
|
||||||
}
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<section>
|
<section>
|
||||||
<h2>Consult Mobility</h2>
|
<h2>Consult Mobility</h2>
|
||||||
{data.promotions.map((promo) => (
|
{error && <p className="error">{error}</p>}
|
||||||
|
|
||||||
|
<div>
|
||||||
|
<label htmlFor="promotionSelect">Select Promotion: </label>
|
||||||
|
<select
|
||||||
|
id="promotionSelect"
|
||||||
|
value={selectedPromotion}
|
||||||
|
onChange={(e) =>
|
||||||
|
setSelectedPromotion(
|
||||||
|
e.target.value === "all" ? "all" : Number(e.target.value)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
>
|
||||||
|
<option value="all">All Promotions</option>
|
||||||
|
{promotions.map((promo) => (
|
||||||
|
<option key={promo.id} value={promo.id}>
|
||||||
|
{promo.name}
|
||||||
|
</option>
|
||||||
|
))}
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{promotions.map((promo) => (
|
||||||
<div key={promo.id}>
|
<div key={promo.id}>
|
||||||
<h3>Promotion: {promo.name}</h3>
|
{selectedPromotion === "all" || selectedPromotion === promo.id ? (
|
||||||
<table>
|
<>
|
||||||
<thead>
|
<h3>Promotion: {promo.name}</h3>
|
||||||
<tr>
|
<table>
|
||||||
<th>ID</th>
|
<thead>
|
||||||
<th>First Name</th>
|
<tr>
|
||||||
<th>Last Name</th>
|
<th>ID</th>
|
||||||
<th>Start Date</th>
|
<th>First Name</th>
|
||||||
<th>End Date</th>
|
<th>Last Name</th>
|
||||||
<th>Weeks Count</th>
|
<th>Start Date</th>
|
||||||
<th>Destination Country</th>
|
<th>End Date</th>
|
||||||
<th>Destination Name</th>
|
<th>Weeks Count</th>
|
||||||
<th>Status</th>
|
<th>Destination Country</th>
|
||||||
</tr>
|
<th>Destination Name</th>
|
||||||
</thead>
|
<th>Status</th>
|
||||||
<tbody>
|
</tr>
|
||||||
{data.students
|
</thead>
|
||||||
?.filter((student) => student.promotionId === promo.id)
|
<tbody>
|
||||||
.map((student) => {
|
{filteredData
|
||||||
const mobility = data.mobilities?.find((mob) =>
|
.filter((entry) => entry.promotionId === promo.id)
|
||||||
mob.studentId === student.id
|
.map((entry) => (
|
||||||
);
|
<tr key={entry.studentId}>
|
||||||
return (
|
<td>{entry.studentId}</td>
|
||||||
<tr key={student.id}>
|
<td>{entry.firstName}</td>
|
||||||
<td>{student.id}</td>
|
<td>{entry.lastName}</td>
|
||||||
<td>{student.firstName}</td>
|
<td>{entry.startDate || "N/A"}</td>
|
||||||
<td>{student.lastName}</td>
|
<td>{entry.endDate || "N/A"}</td>
|
||||||
<td>{mobility?.startDate || "N/A"}</td>
|
<td>{entry.weeksCount || "N/A"}</td>
|
||||||
<td>{mobility?.endDate || "N/A"}</td>
|
<td>{entry.destinationCountry || "N/A"}</td>
|
||||||
<td>{mobility?.weeksCount ?? "0"}</td>
|
<td>{entry.destinationName || "N/A"}</td>
|
||||||
<td>{mobility?.destinationCountry || "N/A"}</td>
|
<td>{entry.mobilityStatus}</td>
|
||||||
<td>{mobility?.destinationName || "N/A"}</td>
|
</tr>
|
||||||
<td>{mobility?.mobilityStatus || "N/A"}</td>
|
))}
|
||||||
</tr>
|
</tbody>
|
||||||
);
|
</table>
|
||||||
})}
|
</>
|
||||||
</tbody>
|
) : null}
|
||||||
</table>
|
|
||||||
</div>
|
</div>
|
||||||
))}
|
))}
|
||||||
</section>
|
</section>
|
||||||
|
|||||||
Vendored
-18
@@ -2,24 +2,6 @@ interface Promotion {
|
|||||||
id: number;
|
id: number;
|
||||||
name: string;
|
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 {
|
interface MobilityData {
|
||||||
id: number | null;
|
id: number | null;
|
||||||
|
|||||||
Reference in New Issue
Block a user