import { CalendarCheck, CheckCheck, Loader2 } from "lucide-react"; import { Button } from "../ui/button"; import { Card, CardContent, CardHeader, CardTitle } from "../ui/card"; import { registerForMeal, useMeals, type Meal } from "~/lib/api"; import { useState } from "react"; import { toast } from "sonner"; import { formatDate } from "./date-picker"; import { DateTime } from "luxon"; export default function MealRegistration({ credentials, }: { credentials: { username: string; password: string }; }) { const [isPosting, setLoading] = useState(false); const { isLoading, error, meals: rawMeals, refetch } = useMeals(); const filterRegisteredMeals = (meals: Meal[]) => { return meals?.filter((meal) => meal.submittable); }; const meals = filterRegisteredMeals(rawMeals || []); const handleRegister = async (id: number) => { try { await registerForMeal(id, credentials.username, credentials.password); await refetch(); setLoading(false); } catch (error) { console.error("Error registering for meal:", error); toast.error( "Une erreur est survenue lors de l'inscription au repas. Veuillez réessayer." ); } }; return (
{isLoading &&

Chargement du menu...

} {error && (

Erreur lors du chargement des menus.

)} {meals && meals.length === 0 &&

Aucun repas disponible.

} {/* Menus */}

Inscription aux repas

Prochains Repas
{meals.map((meal, i) => ( <>

Repas du {formatDate(DateTime.fromISO(meal.date))}

({meal.name})

{meal.isRegistered && ( )} {isPosting && !meal.isRegistered && ( )} {!isPosting && !meal.isRegistered && meal.submittable && ( )}
{i < meals.length - 1 &&
} ))}
); }