Files
gg-backend/frontend/src/pages/Career.tsx
T

446 lines
12 KiB
TypeScript
Raw Normal View History

2026-03-24 14:35:23 +05:30
import { useState, useEffect, useCallback } from "react";
import { getCareersApi, deleteCareerApi } from "@/api/career";
import apiClient from "@/api/client";
import {
Table,
TableBody,
TableCell,
TableHead,
TableHeader,
TableRow,
} from "@/components/ui/table";
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
import { Button } from "@/components/ui/button";
import {
Dialog,
DialogContent,
DialogHeader,
DialogTitle,
DialogFooter,
} from "@/components/ui/dialog";
import { Input } from "@/components/ui/input";
2026-04-08 16:30:50 +05:30
import { Badge } from "@/components/ui/badge";
import { Switch } from "@/components/ui/switch";
import { Label } from "@/components/ui/label";
2026-03-24 14:35:23 +05:30
2026-04-08 16:30:50 +05:30
import {
Loader2,
Plus,
Pencil,
Trash,
RefreshCw,
ChevronLeft,
ChevronRight,
} from "lucide-react";
2026-03-24 14:35:23 +05:30
export default function CareerPage() {
const [careers, setCareers] = useState<any[]>([]);
const [loading, setLoading] = useState(true);
const [openModal, setOpenModal] = useState(false);
const [editing, setEditing] = useState<any>(null);
const [searchText, setSearchText] = useState("");
2026-04-08 16:30:50 +05:30
const [currentPage, setCurrentPage] = useState(1);
const itemsPerPage = 10;
2026-03-24 14:35:23 +05:30
const [form, setForm] = useState({
post: "",
designation: "",
qualification: "",
experienceNeed: "",
email: "",
number: "",
status: "new",
isActive: true,
sortOrder: 0,
2026-03-24 14:35:23 +05:30
});
const fetchAll = useCallback(async () => {
setLoading(true);
try {
const res = await getCareersApi();
setCareers(res?.data || []);
} catch (err) {
console.error(err);
} finally {
setLoading(false);
}
}, []);
useEffect(() => {
fetchAll();
}, [fetchAll]);
2026-04-08 16:30:50 +05:30
const filteredCareers = careers.filter(
(item) =>
item.post?.toLowerCase().includes(searchText.toLowerCase()) ||
item.designation?.toLowerCase().includes(searchText.toLowerCase()),
2026-03-24 14:35:23 +05:30
);
2026-04-08 16:30:50 +05:30
useEffect(() => {
setCurrentPage(1);
}, [searchText]);
const totalPages = Math.ceil(filteredCareers.length / itemsPerPage);
const indexOfLastItem = currentPage * itemsPerPage;
const indexOfFirstItem = indexOfLastItem - itemsPerPage;
const currentItems = filteredCareers.slice(indexOfFirstItem, indexOfLastItem);
2026-03-24 14:35:23 +05:30
function handleChange(e: any) {
setForm({ ...form, [e.target.name]: e.target.value });
}
function openAdd() {
setEditing(null);
setForm({
post: "",
designation: "",
qualification: "",
experienceNeed: "",
email: "",
number: "",
status: "new",
isActive: true,
sortOrder: 0,
2026-03-24 14:35:23 +05:30
});
setOpenModal(true);
}
function openEdit(item: any) {
setEditing(item);
setForm({
post: item.post || "",
designation: item.designation || "",
qualification: item.qualification || "",
experienceNeed: item.experienceNeed || "",
email: item.email || "",
number: item.number || "",
status: item.status || "new",
isActive: item.isActive ?? true,
sortOrder: item.sortOrder ?? 0,
2026-03-24 14:35:23 +05:30
});
setOpenModal(true);
}
async function handleSubmit() {
try {
if (editing) {
await apiClient.patch(`/careers/${editing.id}`, form);
} else {
await apiClient.post("/careers", form);
}
setOpenModal(false);
fetchAll();
} catch (err) {
console.error(err);
}
}
async function handleDelete(id: number) {
if (!confirm("Delete career?")) return;
await deleteCareerApi(id);
fetchAll();
}
return (
<div className="p-6 space-y-6">
2026-04-08 16:30:50 +05:30
<div className="flex flex-col md:flex-row md:justify-between md:items-center gap-4">
<h1 className="text-3xl font-bold">Careers</h1>
2026-03-24 14:35:23 +05:30
2026-04-08 16:30:50 +05:30
<div className="flex flex-wrap gap-3">
2026-03-24 14:35:23 +05:30
<Input
2026-04-08 16:30:50 +05:30
placeholder="Search post / designation..."
2026-03-24 14:35:23 +05:30
value={searchText}
onChange={(e) => setSearchText(e.target.value)}
2026-04-08 16:30:50 +05:30
className="w-[250px] text-base"
2026-03-24 14:35:23 +05:30
/>
2026-04-08 16:30:50 +05:30
<Button
variant="outline"
onClick={fetchAll}
disabled={loading}
className="text-base"
>
<RefreshCw className="mr-2 h-5 w-5" />
2026-03-24 14:35:23 +05:30
Refresh
</Button>
2026-04-08 16:30:50 +05:30
<Button onClick={openAdd} className="text-base">
<Plus className="mr-2 h-5 w-5" />
2026-03-24 14:35:23 +05:30
Add Career
</Button>
</div>
</div>
<Card>
<CardHeader>
2026-04-08 16:30:50 +05:30
<CardTitle className="text-xl">Career Opportunities</CardTitle>
2026-03-24 14:35:23 +05:30
</CardHeader>
2026-04-08 16:30:50 +05:30
<CardContent className="p-0 sm:p-6 space-y-4">
<div className="rounded-md border overflow-x-auto overflow-y-auto max-h-[650px] relative">
<Table className="w-full min-w-[1000px] table-fixed border-separate border-spacing-0">
<TableHeader className="sticky top-0 z-20 bg-background shadow-sm">
2026-03-24 14:35:23 +05:30
<TableRow>
2026-04-08 16:30:50 +05:30
<TableHead className="w-[60px] bg-background font-bold text-sm">
Priority
2026-04-08 16:30:50 +05:30
</TableHead>
<TableHead className="w-[200px] bg-background font-bold text-sm">
Post & Designation
</TableHead>
<TableHead className="w-[200px] bg-background font-bold text-sm">
Qualification
</TableHead>
<TableHead className="w-[120px] bg-background font-bold text-sm">
Experience
</TableHead>
<TableHead className="w-[200px] bg-background font-bold text-sm">
Contact Info
</TableHead>
<TableHead className="w-[100px] bg-background font-bold text-sm">
Visibility
2026-04-08 16:30:50 +05:30
</TableHead>
<TableHead className="w-[120px] bg-background font-bold text-right text-sm">
Actions
</TableHead>
2026-03-24 14:35:23 +05:30
</TableRow>
</TableHeader>
<TableBody>
{loading ? (
<TableRow>
2026-04-08 16:30:50 +05:30
<TableCell colSpan={7} className="text-center py-10">
<Loader2 className="h-8 w-8 animate-spin mx-auto" />
2026-03-24 14:35:23 +05:30
</TableCell>
</TableRow>
2026-04-08 16:30:50 +05:30
) : currentItems.length === 0 ? (
2026-03-24 14:35:23 +05:30
<TableRow>
2026-04-08 16:30:50 +05:30
<TableCell
colSpan={7}
className="text-center text-muted-foreground py-10 text-base"
>
2026-03-24 14:35:23 +05:30
No careers found
</TableCell>
</TableRow>
) : (
2026-04-08 16:30:50 +05:30
currentItems.map((item) => (
<TableRow key={item.id} className="hover:bg-muted/50">
<TableCell className="font-mono text-xs">
{item.sortOrder}
2026-04-08 16:30:50 +05:30
</TableCell>
<TableCell>
<div className="font-semibold text-base truncate">
{item.post}
</div>
<div className="text-xs text-muted-foreground truncate">
{item.designation}
</div>
</TableCell>
<TableCell>
<div className="text-sm line-clamp-2">
{item.qualification}
</div>
</TableCell>
<TableCell className="text-sm">
{item.experienceNeed}
</TableCell>
<TableCell>
<div className="text-sm font-medium">{item.email}</div>
<div className="text-xs text-muted-foreground">
{item.number}
</div>
</TableCell>
<TableCell>
<Badge
variant={item.isActive ? "default" : "secondary"}
2026-04-08 16:30:50 +05:30
className="capitalize"
2026-03-24 14:35:23 +05:30
>
{item.isActive ? "Active" : "Hidden"}
2026-04-08 16:30:50 +05:30
</Badge>
</TableCell>
2026-03-24 14:35:23 +05:30
2026-04-08 16:30:50 +05:30
<TableCell className="text-right">
<div className="flex justify-end gap-2">
<Button
size="icon"
variant="ghost"
className="h-9 w-9"
onClick={() => openEdit(item)}
>
<Pencil className="h-4 w-4" />
</Button>
<Button
size="icon"
variant="ghost"
className="h-9 w-9 text-destructive hover:text-destructive hover:bg-destructive/10"
onClick={() => handleDelete(item.id)}
>
<Trash className="h-4 w-4" />
</Button>
</div>
2026-03-24 14:35:23 +05:30
</TableCell>
</TableRow>
))
)}
</TableBody>
</Table>
</div>
2026-04-08 16:30:50 +05:30
{!loading && filteredCareers.length > 0 && (
<div className="flex items-center justify-between px-2 py-6 border-t">
<div className="text-base text-muted-foreground">
Showing{" "}
<span className="font-semibold">{indexOfFirstItem + 1}</span> to{" "}
<span className="font-semibold">
{Math.min(indexOfLastItem, filteredCareers.length)}
</span>{" "}
of{" "}
<span className="font-semibold">{filteredCareers.length}</span>{" "}
careers
</div>
<div className="flex items-center gap-6">
<div className="text-base font-semibold">
Page {currentPage} of {totalPages}
</div>
<div className="flex gap-2">
<Button
variant="outline"
size="icon"
className="h-10 w-10"
onClick={() =>
setCurrentPage((prev) => Math.max(prev - 1, 1))
}
disabled={currentPage === 1}
>
<ChevronLeft className="h-5 w-5" />
</Button>
<Button
variant="outline"
size="icon"
className="h-10 w-10"
onClick={() =>
setCurrentPage((prev) => Math.min(prev + 1, totalPages))
}
disabled={currentPage === totalPages || totalPages === 0}
>
<ChevronRight className="h-5 w-5" />
</Button>
</div>
</div>
</div>
)}
2026-03-24 14:35:23 +05:30
</CardContent>
</Card>
<Dialog open={openModal} onOpenChange={setOpenModal}>
2026-04-08 16:30:50 +05:30
<DialogContent className="w-full max-w-lg">
2026-03-24 14:35:23 +05:30
<DialogHeader>
2026-04-08 16:30:50 +05:30
<DialogTitle className="text-2xl">
{editing ? "Edit Career" : "Add New Career"}
</DialogTitle>
2026-03-24 14:35:23 +05:30
</DialogHeader>
2026-04-08 16:30:50 +05:30
<div className="space-y-4 py-4">
<div className="grid grid-cols-1 gap-4">
<Input
name="post"
placeholder="Post (e.g. Staff Nurse)"
value={form.post}
onChange={handleChange}
className="text-base"
/>
<Input
name="designation"
placeholder="Designation"
value={form.designation}
onChange={handleChange}
className="text-base"
/>
<Input
name="qualification"
placeholder="Qualification"
value={form.qualification}
onChange={handleChange}
className="text-base"
/>
<Input
name="experienceNeed"
placeholder="Experience Needed"
value={form.experienceNeed}
onChange={handleChange}
className="text-base"
/>
<Input
name="email"
type="email"
placeholder="HR Email Address"
value={form.email}
onChange={handleChange}
className="text-base"
/>
<Input
name="number"
placeholder="Contact Number"
value={form.number}
onChange={handleChange}
className="text-base"
/>
<Input
name="status"
placeholder="Status (e.g. active / closed)"
value={form.status}
onChange={handleChange}
className="text-base"
/>
<div className="flex items-center justify-between p-2 border rounded-md">
<Label htmlFor="isActive" className="text-base">
Active
</Label>
<Switch
id="isActive"
checked={form.isActive}
onCheckedChange={(val) => setForm({ ...form, isActive: val })}
/>
</div>
<div className="space-y-1">
<Label htmlFor="sortOrder" className="text-sm">
Sort Priority (Lower numbers show first)
</Label>
<Input
id="sortOrder"
name="sortOrder"
type="number"
placeholder="Sort Order"
value={form.sortOrder}
onChange={handleChange}
className="text-base"
/>
</div>
2026-04-08 16:30:50 +05:30
</div>
2026-03-24 14:35:23 +05:30
</div>
2026-04-08 16:30:50 +05:30
<DialogFooter className="pt-4 border-t">
<Button
variant="ghost"
onClick={() => setOpenModal(false)}
className="text-base"
>
2026-03-24 14:35:23 +05:30
Cancel
</Button>
2026-04-08 16:30:50 +05:30
<Button onClick={handleSubmit} className="px-8 text-base">
{editing ? "Save Changes" : "Create Career"}
2026-03-24 14:35:23 +05:30
</Button>
</DialogFooter>
</DialogContent>
</Dialog>
</div>
);
}