"use client" import { useEffect, useMemo, useState } from "react" import { Button } from "@/components/ui/button" import { Input } from "@/components/ui/input" import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card" import { Badge } from "@/components/ui/badge" import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui/table" import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, DialogTrigger, } from "@/components/ui/dialog" import { Label } from "@/components/ui/label" import { Textarea } from "@/components/ui/textarea" import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select" import { Plus, Search, Edit, Trash2, Eye } from "lucide-react" import { adminJobsApi, transformApiJobToFrontend, type AdminJob } from "@/lib/api" type AdminJobRow = ReturnType & { status?: string applicationsCount?: number } export default function AdminJobsPage() { const [searchTerm, setSearchTerm] = useState("") const [jobs, setJobs] = useState([]) const [isCreateDialogOpen, setIsCreateDialogOpen] = useState(false) const [isViewDialogOpen, setIsViewDialogOpen] = useState(false) const [isEditDialogOpen, setIsEditDialogOpen] = useState(false) const [selectedJob, setSelectedJob] = useState(null) const [editForm, setEditForm] = useState>({}) const [isLoading, setIsLoading] = useState(true) const [errorMessage, setErrorMessage] = useState(null) useEffect(() => { const loadJobs = async () => { try { setIsLoading(true) setErrorMessage(null) const jobsData = await adminJobsApi.list({ limit: 100 }) setJobs(jobsData.data ?? []) } catch (error) { console.error("Failed to load jobs:", error) setErrorMessage("Unable to load jobs right now.") setJobs([]) } finally { setIsLoading(false) } } loadJobs() }, []) const jobRows = useMemo( () => jobs.map((job) => { const mapped = transformApiJobToFrontend(job) const applicationsCount = typeof (job as { applicationsCount?: number }).applicationsCount === "number" ? (job as { applicationsCount?: number }).applicationsCount : 0 return { ...mapped, status: job.status, applicationsCount, } }), [jobs], ) const companyOptions = useMemo( () => Array.from(new Set(jobRows.map((job) => job.company))).sort(), [jobRows], ) const filteredJobs = useMemo( () => jobRows.filter( (job) => job.title.toLowerCase().includes(searchTerm.toLowerCase()) || job.company.toLowerCase().includes(searchTerm.toLowerCase()), ), [jobRows, searchTerm], ) const activeJobs = useMemo( () => jobs.filter((job) => ["published", "open", "active"].includes(job.status?.toLowerCase() ?? ""), ).length, [jobs], ) const totalApplications = useMemo( () => jobRows.reduce((sum, job) => sum + (job.applicationsCount ?? 0), 0), [jobRows], ) const handleViewJob = (job: AdminJobRow) => { setSelectedJob(job) setIsViewDialogOpen(true) } const handleEditJob = (job: AdminJobRow) => { setSelectedJob(job) // Find original admin job to populate edit form correctly if needed, or just use row data // Converting row data back to partial AdminJob for editing setEditForm({ title: job.title, description: job.description, // Add other fields as necessary }) setIsEditDialogOpen(true) } const handleDeleteJob = async (idStr: string) => { if (!confirm("Are you sure you want to delete this job?")) return const id = parseInt(idStr) if (isNaN(id)) return try { await adminJobsApi.delete(id) setJobs((prevJobs) => prevJobs.filter((job) => job.id !== id)) } catch (error) { console.error("Failed to delete job:", error) alert("Failed to delete job") } } const handleSaveEdit = async () => { if (!selectedJob) return const id = parseInt(selectedJob.id) if (isNaN(id)) return try { setIsLoading(true) const updated = await adminJobsApi.update(id, editForm) setJobs((prev) => prev.map((j) => (j.id === id ? updated : j))) setIsEditDialogOpen(false) } catch (error) { console.error("Failed to update job:", error) alert("Failed to update job") } finally { setIsLoading(false) } } return (
{/* Header */}

Job management

Manage all jobs posted on the platform

Create new job Fill in the details for the new job opening