"use client"; import { useState, useEffect } from "react"; import { Search } from "lucide-react"; import { Input } from "@/components/ui/input"; import Link from "next/link"; import { Button } from "@/components/ui/button"; import { useToast } from "@/components/ui/use-toast"; // Define Mission interface interface User { id: string; email: string; } interface MissionUser { id: string; role: string; user: User; } interface Mission { id: string; name: string; logo?: string; logoUrl?: string; oddScope: string[]; niveau: string; missionType: string; projection: string; participation?: string; services?: string[]; intention?: string; createdAt: string; creator: User; missionUsers: MissionUser[]; } export default function MissionTabPage() { const [searchTerm, setSearchTerm] = useState(""); const [missions, setMissions] = useState([]); const [loading, setLoading] = useState(true); const { toast } = useToast(); // Fetch all missions from API useEffect(() => { const fetchAllMissions = async () => { try { setLoading(true); const response = await fetch('/api/missions/all'); // This will need a new API endpoint if (!response.ok) { throw new Error('Failed to fetch missions'); } const data = await response.json(); console.log("All missions data:", data.missions); setMissions(data.missions || []); } catch (error) { console.error('Error fetching missions:', error); toast({ title: "Erreur", description: "Impossible de charger les missions", variant: "destructive", }); } finally { setLoading(false); } }; fetchAllMissions(); }, []); // Filter missions based on search term const filteredMissions = missions.filter(mission => mission.name.toLowerCase().includes(searchTerm.toLowerCase()) || mission.niveau.toLowerCase().includes(searchTerm.toLowerCase()) || mission.missionType.toLowerCase().includes(searchTerm.toLowerCase()) || mission.oddScope.some(scope => scope.toLowerCase().includes(searchTerm.toLowerCase())) ); // Function to format date const formatDate = (dateString: string) => { const date = new Date(dateString); return date.toLocaleDateString('fr-FR', { day: '2-digit', month: '2-digit', year: 'numeric' }); }; // Function to get mission category and icon const getODDInfo = (mission: Mission) => { const oddCode = mission.oddScope && mission.oddScope.length > 0 ? mission.oddScope[0] : null; // Extract number from odd code (e.g., "odd-3" -> "3") const oddNumber = oddCode ? oddCode.replace('odd-', '') : null; return { number: oddNumber, label: oddNumber ? `ODD ${oddNumber}` : "Non catégorisé", iconPath: oddNumber ? `/F SDG Icons 2019 WEB/F-WEB-Goal-${oddNumber.padStart(2, '0')}.png` : "" }; }; // Function to get appropriate badge color based on niveau const getNiveauBadgeColor = (niveau: string) => { switch(niveau) { case 'a': return 'bg-green-100 text-green-800'; case 'b': return 'bg-blue-100 text-blue-800'; case 'c': return 'bg-purple-100 text-purple-800'; case 's': return 'bg-amber-100 text-amber-800'; default: return 'bg-gray-100 text-gray-800'; } }; // Function to get full niveau label const getNiveauLabel = (niveau: string) => { switch(niveau) { case 'a': return 'A'; case 'b': return 'B'; case 'c': return 'C'; case 's': return 'S'; default: return niveau.toUpperCase(); } }; return (
{/* Page Title */}

Le Tableau des Missions

{/* Search Header */}

Toutes les missions disponibles

setSearchTerm(e.target.value)} />
{/* Missions Grid */}
{loading ? (
) : filteredMissions.length > 0 ? (
{filteredMissions.map((mission) => { const oddInfo = getODDInfo(mission); const niveauColor = getNiveauBadgeColor(mission.niveau); return (
{/* Card Header with Name and Level */}

{mission.name}

{/* ODD scope icon */} {oddInfo.number && (
{oddInfo.label} { // Fallback if image fails to load (e.target as HTMLImageElement).style.display = 'none'; }} />
)} {getNiveauLabel(mission.niveau)}
{/* Centered Logo */}
{mission.logoUrl ? ( {mission.name} { console.error("Logo failed to load:", { missionId: mission.id, missionName: mission.name, logoUrl: mission.logoUrl, logoPath: mission.logo }); // If the image fails to load, show the fallback (e.currentTarget as HTMLImageElement).style.display = 'none'; // Show the fallback div const fallbackDiv = e.currentTarget.parentElement?.querySelector('.logo-fallback'); if (fallbackDiv) { (fallbackDiv as HTMLElement).style.display = 'flex'; } }} /> ) : null}
{mission.name.slice(0, 2).toUpperCase()}
{/* Card Content - Services and Description */}
{/* Services section */} {mission.services && mission.services.length > 0 && (
Services:
{mission.services.map(service => ( {service} ))}
)} {/* Description text */}
{mission.intention ? (mission.intention.substring(0, 100) + (mission.intention.length > 100 ? '...' : '')) : 'Pas de description disponible.'}
{/* Card Footer */}
Créée le {formatDate(mission.createdAt)}
); })}
) : (

Aucune mission trouvée

Modifiez vos critères de recherche ou réessayez plus tard.

)}
); }