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

363 lines
11 KiB
TypeScript
Raw Normal View History

2026-05-26 15:48:01 +05:30
import { useState, useEffect, useCallback } from 'react';
import { getCareersApi, updateCareerApi, createCareerApi } from '@/api/career';
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';
import { Badge } from '@/components/ui/badge';
import { Switch } from '@/components/ui/switch';
import { Label } from '@/components/ui/label';
import { Loader2, Plus, Pencil, 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);
2026-05-26 15:48:01 +05:30
const [searchText, setSearchText] = useState('');
2026-03-24 14:35:23 +05:30
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({
2026-05-26 15:48:01 +05:30
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()) ||
2026-05-26 15:48:01 +05:30
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 });
}
const handleToggleStatus = async (item: any) => {
try {
await updateCareerApi(item.id, {
...item,
isActive: !item.isActive,
} as any);
fetchAll();
} catch (error) {
2026-05-26 15:48:01 +05:30
console.error('Failed to toggle status', error);
}
};
2026-03-24 14:35:23 +05:30
function openAdd() {
setEditing(null);
setForm({
2026-05-26 15:48:01 +05:30
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({
2026-05-26 15:48:01 +05:30
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 updateCareerApi(editing.id, form);
2026-03-24 14:35:23 +05:30
} else {
await createCareerApi(form);
2026-03-24 14:35:23 +05:30
}
2026-03-24 14:35:23 +05:30
setOpenModal(false);
fetchAll();
} catch (err) {
console.error(err);
}
}
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-05-26 15:48:01 +05:30
<Button variant="outline" onClick={fetchAll} disabled={loading} className="text-base">
2026-04-08 16:30:50 +05:30
<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-[800px] table-fixed border-separate border-spacing-0">
2026-04-08 16:30:50 +05:30
<TableHeader className="sticky top-0 z-20 bg-background shadow-sm">
2026-03-24 14:35:23 +05:30
<TableRow>
2026-05-26 15:48:01 +05:30
<TableHead className="w-[80px] bg-background font-bold text-sm">Priority</TableHead>
<TableHead className="w-[250px] 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-[80px] bg-background font-bold text-sm">Status (Active)</TableHead>
<TableHead className="w-[80px] bg-background font-bold text-right text-sm">Actions</TableHead>
2026-03-24 14:35:23 +05:30
</TableRow>
</TableHeader>
<TableBody>
{loading ? (
<TableRow>
<TableCell colSpan={6} className="text-center py-10">
2026-04-08 16:30:50 +05:30
<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-05-26 15:48:01 +05:30
<TableCell colSpan={6} 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">
2026-05-26 15:48:01 +05:30
<TableCell className="font-mono text-xs">{item.sortOrder}</TableCell>
2026-04-08 16:30:50 +05:30
<TableCell>
2026-05-26 15:48:01 +05:30
<div className="font-semibold text-base truncate">{item.post}</div>
<div className="text-xs text-muted-foreground truncate">{item.designation}</div>
2026-04-08 16:30:50 +05:30
</TableCell>
<TableCell>
2026-05-26 15:48:01 +05:30
<div className="text-sm line-clamp-2">{item.qualification}</div>
2026-04-08 16:30:50 +05:30
</TableCell>
2026-05-26 15:48:01 +05:30
<TableCell className="text-sm">{item.experienceNeed}</TableCell>
2026-04-08 16:30:50 +05:30
<TableCell>
<div className="flex items-center gap-2">
2026-05-26 15:48:01 +05:30
<Switch checked={item.isActive} onCheckedChange={() => handleToggleStatus(item)} />
<Badge variant={item.isActive ? 'default' : 'secondary'} className="capitalize">
{item.isActive ? 'Active' : 'Hidden'}
</Badge>
2026-04-08 16:30:50 +05:30
</div>
</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">
2026-05-26 15:48:01 +05:30
<Button size="icon" variant="ghost" className="h-9 w-9" onClick={() => openEdit(item)}>
2026-04-08 16:30:50 +05:30
<Pencil 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">
2026-05-26 15:48:01 +05:30
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
2026-04-08 16:30:50 +05:30
</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"
2026-05-26 15:48:01 +05:30
onClick={() => setCurrentPage((prev) => Math.max(prev - 1, 1))}
2026-04-08 16:30:50 +05:30
disabled={currentPage === 1}
>
<ChevronLeft className="h-5 w-5" />
</Button>
<Button
variant="outline"
size="icon"
className="h-10 w-10"
2026-05-26 15:48:01 +05:30
onClick={() => setCurrentPage((prev) => Math.min(prev + 1, totalPages))}
2026-04-08 16:30:50 +05:30
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-05-26 15:48:01 +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">
2026-05-26 15:48:01 +05:30
<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">
2026-05-26 15:48:01 +05:30
{editing ? 'Save Changes' : 'Create Career'}
2026-03-24 14:35:23 +05:30
</Button>
</DialogFooter>
</DialogContent>
</Dialog>
</div>
);
}