import { useEffect, useState } from "react"; import { Combobox } from "~/components/combobox"; import { Label } from "~/components/ui/label"; import { getStudentMatch, mergeStudent } from "~/lib/api"; type PartialStudent = { id: string; fullName: string; }; export function MatchStudent({ firstName, lastName, onSuccess, }: { firstName: string; lastName: string; onSuccess: (student: PartialStudent) => void; }) { const [pastClasses, setPastClasses] = useState< { value: string; label: string }[] >([]); useEffect(() => { fetch("/past_classes.json") .then((res) => res.json()) .then((data) => { setPastClasses(data); }); }, []); const [pastClassName, setPastClassName] = useState(""); const [isLoading, setIsLoading] = useState(false); const [matchedStudent, setMatchedStudent] = useState(null); useEffect(() => { if (pastClassName) { setIsLoading(true); getStudentMatch(firstName, lastName, pastClassName) .then((student) => { setMatchedStudent(student); setIsLoading(false); }) .catch(() => { setMatchedStudent(null); setIsLoading(false); }); } }, [pastClassName, firstName, lastName]); const handleValidate = () => { if (matchedStudent) { mergeStudent(matchedStudent.id).then(() => { onSuccess(matchedStudent); }); } }; return (
{isLoading &&

Recherche en cours...

} {matchedStudent && (

Correspondance trouvée: {matchedStudent.fullName}

)}
); }