import { useEffect, useState } from "preact/hooks"; import Promotion from "$root/routes/(apps)/students/(_components)/Promotion.tsx"; type SingleUserResponse = { promo: Promotion; student: Student }; type ManyUsersResponse = { promos: Promotion[]; students: Student[] }; type APIResponse = SingleUserResponse | ManyUsersResponse; export default function ConsultStudents() { const [data, setData] = useState(null); const [error, setError] = useState(null); useEffect(() => { const fetchData = async () => { const response = await fetch("/students/api/students"); if (!response.ok) { setError("Failed to load data. Please try again later."); } const result: APIResponse = await response.json(); setData(result); }; fetchData(); }, []); return ( <> {error &&

{error}

} {data && ((Object.hasOwn(data, "student")) ? ( ) : (data as ManyUsersResponse).promos.map((promo) => ( )))} ); }