2026-03-26 14:38:23 +05:30
|
|
|
import { useState, useEffect, useCallback } from "react";
|
|
|
|
|
import { AxiosError } from "axios";
|
2026-03-16 17:55:33 +05:30
|
|
|
|
|
|
|
|
import {
|
|
|
|
|
getDepartmentsApi,
|
|
|
|
|
createDepartmentApi,
|
|
|
|
|
updateDepartmentApi,
|
|
|
|
|
deleteDepartmentApi,
|
|
|
|
|
} from "@/api/department";
|
|
|
|
|
|
|
|
|
|
import {
|
|
|
|
|
Table,
|
|
|
|
|
TableBody,
|
|
|
|
|
TableCell,
|
|
|
|
|
TableHead,
|
|
|
|
|
TableHeader,
|
|
|
|
|
TableRow,
|
|
|
|
|
} from "@/components/ui/table";
|
|
|
|
|
|
2026-03-26 14:38:23 +05:30
|
|
|
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
|
|
|
|
|
import { Button } from "@/components/ui/button";
|
2026-03-16 17:55:33 +05:30
|
|
|
|
|
|
|
|
import {
|
|
|
|
|
Dialog,
|
|
|
|
|
DialogContent,
|
|
|
|
|
DialogHeader,
|
|
|
|
|
DialogTitle,
|
|
|
|
|
DialogFooter,
|
|
|
|
|
} from "@/components/ui/dialog";
|
|
|
|
|
|
2026-03-26 14:38:23 +05:30
|
|
|
import { Input } from "@/components/ui/input";
|
|
|
|
|
import { Textarea } from "@/components/ui/textarea";
|
2026-03-16 17:55:33 +05:30
|
|
|
|
2026-03-26 14:38:23 +05:30
|
|
|
import { Loader2, RefreshCw, Plus, Pencil, Trash, Eye } from "lucide-react";
|
2026-03-16 17:55:33 +05:30
|
|
|
|
|
|
|
|
interface Department {
|
|
|
|
|
departmentId: string;
|
|
|
|
|
name: string;
|
|
|
|
|
para1: string;
|
|
|
|
|
para2: string;
|
|
|
|
|
para3: string;
|
|
|
|
|
facilities: string;
|
|
|
|
|
services: string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default function DepartmentPage() {
|
|
|
|
|
const [departments, setDepartments] = useState<Department[]>([]);
|
|
|
|
|
const [loading, setLoading] = useState(true);
|
|
|
|
|
const [error, setError] = useState("");
|
|
|
|
|
|
|
|
|
|
const [openModal, setOpenModal] = useState(false);
|
|
|
|
|
const [editing, setEditing] = useState<Department | null>(null);
|
|
|
|
|
|
2026-03-26 14:38:23 +05:30
|
|
|
const [viewOpen, setViewOpen] = useState(false);
|
|
|
|
|
const [viewData, setViewData] = useState<Department | null>(null);
|
|
|
|
|
|
2026-03-17 17:28:18 +05:30
|
|
|
const [searchText, setSearchText] = useState("");
|
|
|
|
|
|
2026-03-16 17:55:33 +05:30
|
|
|
const [form, setForm] = useState<Department>({
|
|
|
|
|
departmentId: "",
|
|
|
|
|
name: "",
|
|
|
|
|
para1: "",
|
|
|
|
|
para2: "",
|
|
|
|
|
para3: "",
|
|
|
|
|
facilities: "",
|
|
|
|
|
services: "",
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const fetchDepartments = useCallback(async () => {
|
|
|
|
|
setLoading(true);
|
|
|
|
|
setError("");
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const res = await getDepartmentsApi();
|
|
|
|
|
setDepartments(res?.data || []);
|
|
|
|
|
} catch (err) {
|
|
|
|
|
if (err instanceof AxiosError) {
|
|
|
|
|
setError(err.response?.data?.message || "Failed to load departments");
|
|
|
|
|
} else {
|
|
|
|
|
setError("Something went wrong");
|
|
|
|
|
}
|
|
|
|
|
} finally {
|
|
|
|
|
setLoading(false);
|
|
|
|
|
}
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
fetchDepartments();
|
|
|
|
|
}, [fetchDepartments]);
|
|
|
|
|
|
2026-03-26 14:38:23 +05:30
|
|
|
const filteredDepartments = departments.filter((dep) =>
|
|
|
|
|
dep.name.toLowerCase().includes(searchText.toLowerCase()),
|
|
|
|
|
);
|
2026-03-17 17:28:18 +05:30
|
|
|
|
2026-03-26 14:38:23 +05:30
|
|
|
function handleChange(e: any) {
|
|
|
|
|
setForm({ ...form, [e.target.name]: e.target.value });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function truncate(text: string, limit = 60) {
|
|
|
|
|
if (!text) return "-";
|
|
|
|
|
return text.length > limit ? text.substring(0, limit) + "..." : text;
|
2026-03-16 17:55:33 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function openAdd() {
|
|
|
|
|
setEditing(null);
|
|
|
|
|
setForm({
|
|
|
|
|
departmentId: "",
|
|
|
|
|
name: "",
|
|
|
|
|
para1: "",
|
|
|
|
|
para2: "",
|
|
|
|
|
para3: "",
|
|
|
|
|
facilities: "",
|
|
|
|
|
services: "",
|
|
|
|
|
});
|
|
|
|
|
setOpenModal(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function openEdit(dep: Department) {
|
|
|
|
|
setEditing(dep);
|
|
|
|
|
setForm(dep);
|
|
|
|
|
setOpenModal(true);
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-26 14:38:23 +05:30
|
|
|
function openView(dep: Department) {
|
|
|
|
|
setViewData(dep);
|
|
|
|
|
setViewOpen(true);
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-16 17:55:33 +05:30
|
|
|
async function handleSubmit() {
|
|
|
|
|
try {
|
|
|
|
|
if (editing) {
|
2026-03-26 14:38:23 +05:30
|
|
|
const { departmentId, ...updateData } = form;
|
2026-03-17 13:11:00 +05:30
|
|
|
await updateDepartmentApi(editing.departmentId, updateData);
|
2026-03-16 17:55:33 +05:30
|
|
|
} else {
|
|
|
|
|
await createDepartmentApi(form);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setOpenModal(false);
|
|
|
|
|
fetchDepartments();
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error(error);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-26 14:38:23 +05:30
|
|
|
async function handleDelete(id: string) {
|
|
|
|
|
if (!confirm("Delete this department?")) return;
|
2026-03-16 17:55:33 +05:30
|
|
|
|
|
|
|
|
try {
|
2026-03-26 14:38:23 +05:30
|
|
|
await deleteDepartmentApi(id);
|
2026-03-16 17:55:33 +05:30
|
|
|
fetchDepartments();
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error(error);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div className="p-6 space-y-6">
|
2026-03-17 17:28:18 +05:30
|
|
|
<div className="flex flex-col md:flex-row md:justify-between md:items-center gap-3">
|
2026-03-16 17:55:33 +05:30
|
|
|
<h1 className="text-2xl font-bold">Departments</h1>
|
|
|
|
|
|
2026-03-17 17:28:18 +05:30
|
|
|
<div className="flex flex-wrap gap-3">
|
|
|
|
|
<Input
|
|
|
|
|
placeholder="Search department..."
|
|
|
|
|
value={searchText}
|
|
|
|
|
onChange={(e) => setSearchText(e.target.value)}
|
|
|
|
|
className="w-[220px]"
|
|
|
|
|
/>
|
|
|
|
|
|
2026-03-26 17:58:49 +05:30
|
|
|
<Button
|
|
|
|
|
variant="outline"
|
|
|
|
|
onClick={fetchDepartments}
|
|
|
|
|
disabled={loading}>
|
2026-03-16 17:55:33 +05:30
|
|
|
<RefreshCw className="mr-2 h-4 w-4" />
|
|
|
|
|
Refresh
|
|
|
|
|
</Button>
|
|
|
|
|
|
|
|
|
|
<Button onClick={openAdd}>
|
|
|
|
|
<Plus className="mr-2 h-4 w-4" />
|
|
|
|
|
Add Department
|
|
|
|
|
</Button>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
{error && (
|
|
|
|
|
<div className="p-4 text-red-600 bg-red-50 border rounded-md">
|
|
|
|
|
{error}
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
|
|
|
|
|
<Card>
|
|
|
|
|
<CardHeader>
|
|
|
|
|
<CardTitle>Department List</CardTitle>
|
|
|
|
|
</CardHeader>
|
|
|
|
|
|
|
|
|
|
<CardContent>
|
2026-03-26 14:38:23 +05:30
|
|
|
<div className="border rounded-md max-h-[500px] overflow-y-auto">
|
|
|
|
|
<Table className="w-full table-fixed">
|
2026-03-16 17:55:33 +05:30
|
|
|
<TableHeader>
|
|
|
|
|
<TableRow>
|
2026-03-26 14:38:23 +05:30
|
|
|
<TableHead className="w-[80px]">ID</TableHead>
|
|
|
|
|
<TableHead className="w-[180px]">Name</TableHead>
|
|
|
|
|
<TableHead className="w-[250px]">Para1</TableHead>
|
|
|
|
|
<TableHead className="w-[220px]">Facilities</TableHead>
|
|
|
|
|
<TableHead className="w-[220px]">Services</TableHead>
|
|
|
|
|
<TableHead className="w-[120px]">Actions</TableHead>
|
2026-03-16 17:55:33 +05:30
|
|
|
</TableRow>
|
|
|
|
|
</TableHeader>
|
|
|
|
|
|
|
|
|
|
<TableBody>
|
|
|
|
|
{loading ? (
|
|
|
|
|
<TableRow>
|
2026-03-26 14:38:23 +05:30
|
|
|
<TableCell colSpan={6} className="text-center">
|
2026-03-16 17:55:33 +05:30
|
|
|
<Loader2 className="h-6 w-6 animate-spin mx-auto" />
|
|
|
|
|
</TableCell>
|
|
|
|
|
</TableRow>
|
2026-03-17 17:28:18 +05:30
|
|
|
) : filteredDepartments.length === 0 ? (
|
2026-03-16 17:55:33 +05:30
|
|
|
<TableRow>
|
2026-03-26 14:38:23 +05:30
|
|
|
<TableCell colSpan={6} className="text-center">
|
2026-03-16 17:55:33 +05:30
|
|
|
No departments found
|
|
|
|
|
</TableCell>
|
|
|
|
|
</TableRow>
|
|
|
|
|
) : (
|
2026-03-17 17:28:18 +05:30
|
|
|
filteredDepartments.map((dep) => (
|
2026-03-16 17:55:33 +05:30
|
|
|
<TableRow key={dep.departmentId}>
|
|
|
|
|
<TableCell>{dep.departmentId}</TableCell>
|
|
|
|
|
|
2026-03-26 14:38:23 +05:30
|
|
|
<TableCell>
|
|
|
|
|
<div className="break-words">{dep.name}</div>
|
2026-03-16 17:55:33 +05:30
|
|
|
</TableCell>
|
|
|
|
|
|
2026-03-26 14:38:23 +05:30
|
|
|
<TableCell>
|
|
|
|
|
<div className="break-words whitespace-normal">
|
|
|
|
|
{truncate(dep.para1)}
|
|
|
|
|
</div>
|
2026-03-16 17:55:33 +05:30
|
|
|
</TableCell>
|
|
|
|
|
|
2026-03-26 14:38:23 +05:30
|
|
|
<TableCell>
|
|
|
|
|
<div className="break-words whitespace-normal">
|
|
|
|
|
{truncate(dep.facilities)}
|
|
|
|
|
</div>
|
2026-03-16 17:55:33 +05:30
|
|
|
</TableCell>
|
|
|
|
|
|
2026-03-26 14:38:23 +05:30
|
|
|
<TableCell>
|
|
|
|
|
<div className="break-words whitespace-normal">
|
|
|
|
|
{truncate(dep.services)}
|
|
|
|
|
</div>
|
2026-03-16 17:55:33 +05:30
|
|
|
</TableCell>
|
|
|
|
|
|
2026-03-26 14:38:23 +05:30
|
|
|
<TableCell className="flex gap-2 whitespace-nowrap">
|
|
|
|
|
<Button
|
|
|
|
|
size="sm"
|
|
|
|
|
variant="outline"
|
|
|
|
|
onClick={() => openView(dep)}>
|
|
|
|
|
<Eye className="h-4 w-4" />
|
|
|
|
|
</Button>
|
2026-03-16 17:55:33 +05:30
|
|
|
|
|
|
|
|
<Button
|
|
|
|
|
size="sm"
|
|
|
|
|
variant="outline"
|
2026-03-26 14:38:23 +05:30
|
|
|
onClick={() => openEdit(dep)}>
|
2026-03-16 17:55:33 +05:30
|
|
|
<Pencil className="h-4 w-4" />
|
|
|
|
|
</Button>
|
|
|
|
|
|
|
|
|
|
<Button
|
|
|
|
|
size="sm"
|
|
|
|
|
variant="destructive"
|
2026-03-26 14:38:23 +05:30
|
|
|
onClick={() => handleDelete(dep.departmentId)}>
|
2026-03-16 17:55:33 +05:30
|
|
|
<Trash className="h-4 w-4" />
|
|
|
|
|
</Button>
|
|
|
|
|
</TableCell>
|
|
|
|
|
</TableRow>
|
|
|
|
|
))
|
|
|
|
|
)}
|
|
|
|
|
</TableBody>
|
|
|
|
|
</Table>
|
|
|
|
|
</div>
|
|
|
|
|
</CardContent>
|
|
|
|
|
</Card>
|
|
|
|
|
|
|
|
|
|
<Dialog open={openModal} onOpenChange={setOpenModal}>
|
2026-03-26 14:38:23 +05:30
|
|
|
<DialogContent className="w-full !max-w-5xl max-h-[90vh] overflow-y-auto">
|
2026-03-16 17:55:33 +05:30
|
|
|
<DialogHeader>
|
|
|
|
|
<DialogTitle>
|
|
|
|
|
{editing ? "Edit Department" : "Add Department"}
|
|
|
|
|
</DialogTitle>
|
|
|
|
|
</DialogHeader>
|
|
|
|
|
|
2026-03-26 14:38:23 +05:30
|
|
|
<div className="space-y-4">
|
2026-03-16 17:55:33 +05:30
|
|
|
<Input
|
|
|
|
|
name="departmentId"
|
|
|
|
|
value={form.departmentId}
|
|
|
|
|
onChange={handleChange}
|
2026-03-17 13:11:00 +05:30
|
|
|
disabled={!!editing}
|
2026-03-26 14:38:23 +05:30
|
|
|
placeholder="Department ID"
|
2026-03-16 17:55:33 +05:30
|
|
|
/>
|
|
|
|
|
|
|
|
|
|
<Input
|
|
|
|
|
name="name"
|
|
|
|
|
value={form.name}
|
|
|
|
|
onChange={handleChange}
|
2026-03-26 14:38:23 +05:30
|
|
|
placeholder="Department Name"
|
2026-03-16 17:55:33 +05:30
|
|
|
/>
|
|
|
|
|
|
|
|
|
|
<Textarea
|
|
|
|
|
name="para1"
|
|
|
|
|
value={form.para1}
|
|
|
|
|
onChange={handleChange}
|
2026-03-26 14:38:23 +05:30
|
|
|
placeholder="Para1"
|
2026-03-16 17:55:33 +05:30
|
|
|
/>
|
|
|
|
|
<Textarea
|
|
|
|
|
name="para2"
|
|
|
|
|
value={form.para2}
|
|
|
|
|
onChange={handleChange}
|
2026-03-26 14:38:23 +05:30
|
|
|
placeholder="Para2"
|
2026-03-16 17:55:33 +05:30
|
|
|
/>
|
|
|
|
|
<Textarea
|
|
|
|
|
name="para3"
|
|
|
|
|
value={form.para3}
|
|
|
|
|
onChange={handleChange}
|
2026-03-26 14:38:23 +05:30
|
|
|
placeholder="Para3"
|
2026-03-16 17:55:33 +05:30
|
|
|
/>
|
|
|
|
|
<Textarea
|
|
|
|
|
name="facilities"
|
|
|
|
|
value={form.facilities}
|
|
|
|
|
onChange={handleChange}
|
2026-03-26 14:38:23 +05:30
|
|
|
placeholder="Facilities"
|
2026-03-16 17:55:33 +05:30
|
|
|
/>
|
|
|
|
|
<Textarea
|
|
|
|
|
name="services"
|
|
|
|
|
value={form.services}
|
|
|
|
|
onChange={handleChange}
|
2026-03-26 14:38:23 +05:30
|
|
|
placeholder="Services"
|
2026-03-16 17:55:33 +05:30
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<DialogFooter>
|
|
|
|
|
<Button variant="outline" onClick={() => setOpenModal(false)}>
|
|
|
|
|
Cancel
|
|
|
|
|
</Button>
|
|
|
|
|
<Button onClick={handleSubmit}>
|
|
|
|
|
{editing ? "Update" : "Create"}
|
|
|
|
|
</Button>
|
|
|
|
|
</DialogFooter>
|
|
|
|
|
</DialogContent>
|
|
|
|
|
</Dialog>
|
2026-03-26 14:38:23 +05:30
|
|
|
|
|
|
|
|
<Dialog open={viewOpen} onOpenChange={setViewOpen}>
|
|
|
|
|
<DialogContent className="w-full !max-w-5xl max-h-[90vh] overflow-y-auto">
|
|
|
|
|
{" "}
|
|
|
|
|
<DialogHeader>
|
|
|
|
|
<DialogTitle>Department Details</DialogTitle>
|
|
|
|
|
</DialogHeader>
|
|
|
|
|
{viewData && (
|
|
|
|
|
<div className="space-y-4 text-sm">
|
|
|
|
|
<p>
|
|
|
|
|
<b>ID:</b> {viewData.departmentId}
|
|
|
|
|
</p>
|
|
|
|
|
|
|
|
|
|
<p>
|
|
|
|
|
<b>Name:</b> {viewData.name}
|
|
|
|
|
</p>
|
|
|
|
|
|
|
|
|
|
<p>
|
|
|
|
|
<b>Para1:</b>
|
|
|
|
|
<br />
|
|
|
|
|
{viewData.para1}
|
|
|
|
|
</p>
|
|
|
|
|
|
|
|
|
|
<p>
|
|
|
|
|
<b>Para2:</b>
|
|
|
|
|
<br />
|
|
|
|
|
{viewData.para2}
|
|
|
|
|
</p>
|
|
|
|
|
|
|
|
|
|
<p>
|
|
|
|
|
<b>Para3:</b>
|
|
|
|
|
<br />
|
|
|
|
|
{viewData.para3}
|
|
|
|
|
</p>
|
|
|
|
|
|
|
|
|
|
<p>
|
|
|
|
|
<b>Facilities:</b>
|
|
|
|
|
<br />
|
|
|
|
|
{viewData.facilities}
|
|
|
|
|
</p>
|
|
|
|
|
|
|
|
|
|
<p>
|
|
|
|
|
<b>Services:</b>
|
|
|
|
|
<br />
|
|
|
|
|
{viewData.services}
|
|
|
|
|
</p>
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
<DialogFooter>
|
|
|
|
|
<Button onClick={() => setViewOpen(false)}>Close</Button>
|
|
|
|
|
</DialogFooter>
|
|
|
|
|
</DialogContent>
|
|
|
|
|
</Dialog>
|
2026-03-16 17:55:33 +05:30
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|