diff --git a/app/missions/page.tsx b/app/missions/page.tsx index 964ac153..be50138d 100644 --- a/app/missions/page.tsx +++ b/app/missions/page.tsx @@ -1,51 +1,104 @@ "use client"; -import { useState } from "react"; +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"; -// Mock mission data until we implement the database -const mockMissions = [ - { - id: 1, - title: "Développement d'une application web", - category: "Technology", - location: "Remote", - skills: ["React", "Node.js", "MongoDB"], - duration: "1-3 months", - createdAt: "2023-05-15" - }, - { - id: 2, - title: "Conception d'identité visuelle", - category: "Design", - location: "Paris", - skills: ["Adobe Photoshop", "Illustrator", "UI/UX"], - duration: "< 1 month", - createdAt: "2023-06-02" - }, - { - id: 3, - title: "Traduction de documentation technique", - category: "Writing", - location: "Hybrid", - skills: ["Translation", "Technical Writing", "English"], - duration: "> 3 months", - createdAt: "2023-06-10" - } -]; +// 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; + 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(); - const filteredMissions = mockMissions.filter(mission => - mission.title.toLowerCase().includes(searchTerm.toLowerCase()) || - mission.category.toLowerCase().includes(searchTerm.toLowerCase()) || - mission.skills.some(skill => skill.toLowerCase().includes(searchTerm.toLowerCase())) + // 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(); + 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 + const getCategory = (mission: Mission) => { + return mission.oddScope && mission.oddScope.length > 0 + ? mission.oddScope[0].replace('odd-', 'ODD ') + : "Non catégorisé"; + }; + + // 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 (
@@ -64,46 +117,63 @@ export default function MissionsPage() {
-
- {filteredMissions.map((mission, index) => ( -
-
-
- - {mission.category} - - - {mission.duration} - -
- -
-

{mission.title}

-

Location: {mission.location}

+ {loading ? ( +
+
+
+ ) : filteredMissions.length > 0 ? ( +
+ {filteredMissions.map((mission) => ( +
+
+
+ + {getCategory(mission)} + + + {getDuration(mission.projection)} + +
-
- {mission.skills.map(skill => ( - - {skill} - - ))} +
+

{mission.name}

+

Type: {mission.missionType}

+ +
+ {mission.missionUsers.slice(0, 3).map(missionUser => ( + + {missionUser.role === 'gardien-temps' ? 'Gardien du Temps' : + missionUser.role === 'gardien-parole' ? 'Gardien de la Parole' : + missionUser.role === 'gardien-memoire' ? 'Gardien de la Mémoire' : 'Volontaire'} + + ))} +
+
+ +
+ + Créée le {formatDate(mission.createdAt)} + + + +
- -
- - Créée le {mission.createdAt.replace('2023-', '')} - - - - -
-
- ))} -
+ ))} +
+ ) : ( +
+

Aucune mission trouvée

+ + + +
+ )}
);