"use client" import { Pencil, Filter, Trash2, Search, Calendar, X } from "lucide-react" import { useMigrants } from "@/hooks/useMigrants" import { useState } from "react" import DeleteDialog from "./migrant/Modal/DeleteDialog" import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card" import { Button } from "@/components/ui/button" import { Input } from "@/components/ui/input" import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui/table" import { Checkbox } from "@/components/ui/checkbox" import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogTrigger, DialogDescription, } from "@/components/ui/dialog" import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select" import { RadioGroup, RadioGroupItem } from "@/components/ui/radio-group" import { Label } from "@/components/ui/label" import { useNavigate } from "react-router-dom" import { formatDate } from "@/utils/date" const MigrantsTable = () => { const navigate = useNavigate() const [deleteDialogOpen, setDeleteDialogOpen] = useState(false) const [deleteDialogIds, setDeleteDialogIds] = useState([]) const [isBulkDelete, setIsBulkDelete] = useState(false) const [isModalOpen, setIsModalOpen] = useState(false) const applyAdvancedFilters = () => { setIsModalOpen(false) handleSearch() // Trigger the search with the current filters } const handleEdit = (id: number) => { navigate(`/admin/migrants/edit/${id}?mode=edit`) } const [showAllActive, setShowAllActive] = useState(false) const { migrants, loading, currentPage, totalPages, searchFullName, searchOccupation, filters, selectedMigrants, setCurrentPage, setSearchFullName, setSearchOccupation, setFilters, handleSearch, resetFilters, toggleSelectMigrant, toggleSelectAll, isAllSelected, handleBulkDelete, refetchMigrants } = useMigrants(showAllActive ? 1000 : 10) return (
{/* Filters */} Search & Filters Advanced Filters Set sorting options and filters for the migrants list.
setFilters({ ...filters, alphabetical_order: value })} className="flex gap-4" >
{/* Arrival Date Sorting Options */} {filters.sort_by === "arrival_date" && (
setFilters((prev) => ({ ...prev, sort_by: "arrival_date", arrival_order: value === "newest" ? "desc" : "asc", })) } className="flex gap-4" >
)}
setFilters({ ...filters, arrival_from: e.target.value })} className="pl-8 bg-gray-800 border-gray-700 text-white focus:border-[#9B2335]" />
to
setFilters({ ...filters, arrival_to: e.target.value })} className="pl-8 bg-gray-800 border-gray-700 text-white focus:border-[#9B2335]" />
setSearchFullName(e.target.value)} onKeyDown={(e) => e.key === "Enter" && handleSearch()} className="pl-8 bg-gray-800 border-gray-700 text-white placeholder:text-gray-500 focus:border-[#9B2335]" />
setSearchOccupation(e.target.value)} onKeyDown={(e) => e.key === "Enter" && handleSearch()} onBlur={() => searchOccupation.trim() !== "" && handleSearch()} className="pl-8 bg-gray-800 border-gray-700 text-white placeholder:text-gray-500 focus:border-[#9B2335]" />
setFilters({ ...filters, arrival_from: e.target.value })} className="pl-8 bg-gray-800 border-gray-700 text-white focus:border-[#9B2335]" />
setFilters({ ...filters, arrival_to: e.target.value })} className="pl-8 bg-gray-800 border-gray-700 text-white focus:border-[#9B2335]" />
{/* Bulk Actions */} {selectedMigrants.length > 0 && (
{selectedMigrants.length} migrants selected
)} {/* Table */} {loading ? (
) : (
ID Full Name Date of Birth Place of Birth Occupation Date of Arrival NT Actions {migrants.map((migrant) => { const isSelected = migrant.person_id !== undefined && typeof migrant.person_id === "number" && selectedMigrants.includes(migrant.person_id) const hasValidId = migrant.person_id !== undefined && typeof migrant.person_id === "number" && !isNaN(migrant.person_id) return ( toggleSelectMigrant(migrant.person_id)} disabled={!hasValidId} className="border-gray-600 data-[state=checked]:bg-[#9B2335] data-[state=checked]:border-[#9B2335]" /> {migrant.person_id ?? "—"} {migrant.full_name ?? "—"} {formatDate(migrant.date_of_birth ?? "—")} {migrant.place_of_birth ?? "—"} {migrant.occupation ?? "—"} {formatDate(migrant.migration?.date_of_arrival_nt ?? "—")}
) })}
)}
{/* Pagination */}
{showAllActive ? "Showing all records" : `Page ${currentPage} of ${totalPages}`}
{/* Delete Confirmation Dialog */}
) } export default MigrantsTable