@@ -45,28 +34,6 @@ export function DailyMenu({
);
}
- const [isLoading, setLoading] = useState(false);
- const credentials = getSavedCredentials();
-
- const handleRegister = async (id: number) => {
- try {
- if (!credentials) {
- toast.error(
- "Veuillez d'abord enregistrer vos identifiants dans l'onglet BJRepas."
- );
- return;
- }
- await registerForMeal(id, credentials.username, credentials.password);
- await refetchMeals();
- 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 (
@@ -118,7 +85,7 @@ export function DailyMenu({
Vous êtes inscrit(e) à ce repas
)}
- {credentials &&
+ {/* {credentials &&
!meal.isRegistered &&
meal.submittable &&
(isLoading ? (
@@ -135,7 +102,7 @@ export function DailyMenu({
S'inscrire à ce repas
- ))}
+ ))} */}
))}
diff --git a/app/components/repas/menus.tsx b/app/components/repas/menus.tsx
index 805d8f3..0c80d79 100644
--- a/app/components/repas/menus.tsx
+++ b/app/components/repas/menus.tsx
@@ -5,13 +5,13 @@ import { useState } from "react";
import DayNavigation from "./day-navigation";
export default function Menus() {
- const { isLoading, error, meals, refetch } = useMeals();
+ const { isLoading, error, meals } = useMeals();
const [date, setDate] = useState(DateTime.now().startOf("day"));
const findMenuForDate = (date: DateTime) => {
return meals?.filter((meal) => {
- const mealDate = DateTime.fromISO(meal.date).startOf("day");
- return mealDate.equals(date);
+ const mealDate = DateTime.fromISO(meal.date);
+ return mealDate.hasSame(date.toLocal(), "day");
});
};
@@ -25,10 +25,7 @@ export default function Menus() {
{/* Menus */}
-
+
);
}
diff --git a/app/components/repas/registration.tsx b/app/components/repas/registration.tsx
new file mode 100644
index 0000000..86d9141
--- /dev/null
+++ b/app/components/repas/registration.tsx
@@ -0,0 +1,118 @@
+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.isRegistered || 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 menu 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 &&
}
+ >
+ ))}
+
+
+
+
+ );
+}