"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"; import { getPublicUrl } from "@/lib/s3"; // 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; oddScope: string[]; niveau: string; missionType: string; projection: string; participation?: string; services?: string[]; createdAt: string; creator: User; missionUsers: MissionUser[]; } export default function MissionsPage() { const [searchTerm, setSearchTerm] = useState(""); const [missions, setMissions] = useState([]); const [loading, setLoading] = useState(true); const { toast } = useToast(); // Fetch missions from API useEffect(() => { const fetchMissions = async () => { try { setLoading(true); const response = await fetch('/api/missions'); if (!response.ok) { throw new Error('Failed to fetch missions'); } const data = await response.json(); // Debug log to check mission data structure console.log("Mission 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); } }; fetchMissions(); }, []); // 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- Apprentissage'; case 'b': return 'B- Basique'; case 'c': return 'C- Complexe'; case 's': return 'S- Spécial'; default: return niveau; } }; // Function to get mission type label const getMissionTypeLabel = (type: string) => { switch(type) { case 'remote': return 'À distance'; case 'onsite': return 'Sur site'; case 'hybrid': return 'Hybride'; default: return type; } }; // Function to get participation label const getParticipationLabel = (participation: string | null | undefined) => { console.log("Participation value:", participation); // Debug log if (!participation) return 'Non spécifié'; switch(participation) { case 'volontaire': return 'Volontaire'; case 'cooptation': return 'Cooptation'; default: return participation; } }; // Function to get mission duration const getDuration = (projection: string) => { switch(projection) { case 'short': return '< 1 mois'; case 'medium': return '1-3 mois'; case 'long': return '> 3 mois'; default: return projection; } }; return (

Gérez vos missions et opportunités de bénévolat

setSearchTerm(e.target.value)} />
{loading ? (
) : filteredMissions.length > 0 ? (
{filteredMissions.map((mission) => { const oddInfo = getODDInfo(mission); const niveauColor = getNiveauBadgeColor(mission.niveau); return (
{/* Card Header with Logo and ODD */}
{mission.logo ? ( {mission.name} { console.log("Logo failed to load:", mission.logo); console.log("Full URL attempted:", mission.logo ? getPublicUrl(mission.logo) : 'undefined 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()}
{oddInfo.number && (
{oddInfo.label} { // Fallback if image fails to load (e.target as HTMLImageElement).style.display = 'none'; }} /> {oddInfo.label}
)} {getNiveauLabel(mission.niveau)}

{mission.name}

{/* Card Content */}
Type: {getMissionTypeLabel(mission.missionType)}
Durée: {getDuration(mission.projection)}
Participation: {getParticipationLabel(mission.participation)}
{mission.services && mission.services.length > 0 && (
Services:
{mission.services.map(service => ( {service} ))}
)}
{/* Card Footer */}
Créée le {formatDate(mission.createdAt)}
); })}
) : (

Aucune mission trouvée

Créez votre première mission pour commencer à organiser vos projets et inviter des participants.

)}
); }