feat:add doctor page

This commit is contained in:
ARJUN S THAMPI
2026-03-17 13:11:00 +05:30
parent db8cee836a
commit 763b887d65
11 changed files with 962 additions and 34 deletions

View File

@@ -34,7 +34,6 @@ import {Textarea} from "@/components/ui/textarea";
import {Loader2, RefreshCw, Plus, Pencil, Trash} from "lucide-react";
interface Department {
id?: number;
departmentId: string;
name: string;
para1: string;
@@ -62,8 +61,6 @@ export default function DepartmentPage() {
services: "",
});
/* ---------------- FETCH ---------------- */
const fetchDepartments = useCallback(async () => {
setLoading(true);
setError("");
@@ -86,8 +83,6 @@ export default function DepartmentPage() {
fetchDepartments();
}, [fetchDepartments]);
/* ---------------- FORM ---------------- */
function handleChange(
e: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement>,
) {
@@ -122,7 +117,8 @@ export default function DepartmentPage() {
async function handleSubmit() {
try {
if (editing) {
await updateDepartmentApi(editing.id!, form);
const {departmentId, ...updateData} = form;
await updateDepartmentApi(editing.departmentId, updateData);
} else {
await createDepartmentApi(form);
}
@@ -134,26 +130,20 @@ export default function DepartmentPage() {
}
}
/* ---------------- DELETE ---------------- */
async function handleDelete(id: number) {
async function handleDelete(departmentId: string) {
const confirmDelete = confirm("Delete this department?");
if (!confirmDelete) return;
try {
await deleteDepartmentApi(id);
await deleteDepartmentApi(departmentId);
fetchDepartments();
} catch (error) {
console.error(error);
}
}
/* ---------------- UI ---------------- */
return (
<div className="p-6 space-y-6">
{/* Header */}
<div className="flex justify-between items-center">
<h1 className="text-2xl font-bold">Departments</h1>
@@ -174,16 +164,12 @@ export default function DepartmentPage() {
</div>
</div>
{/* Error */}
{error && (
<div className="p-4 text-red-600 bg-red-50 border rounded-md">
{error}
</div>
)}
{/* Table */}
<Card>
<CardHeader>
<CardTitle>Department List</CardTitle>
@@ -257,7 +243,7 @@ export default function DepartmentPage() {
<Button
size="sm"
variant="destructive"
onClick={() => handleDelete(dep.id!)}
onClick={() => handleDelete(dep.departmentId)}
>
<Trash className="h-4 w-4" />
</Button>
@@ -287,9 +273,9 @@ export default function DepartmentPage() {
placeholder="Department ID"
value={form.departmentId}
onChange={handleChange}
disabled={!!editing}
/>
{/* FIXED INPUT */}
<Input
name="name"
placeholder="Department Name"

View File

@@ -0,0 +1,441 @@
import {useState, useEffect, useCallback} from "react";
import {AxiosError} from "axios";
import {
getDoctorsApi,
createDoctorApi,
updateDoctorApi,
deleteDoctorApi,
getDoctorTimingApi,
} from "@/api/doctor";
import {getDepartmentsApi} from "@/api/department";
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 {Popover, PopoverContent, PopoverTrigger} from "@/components/ui/popover";
import {Command, CommandGroup, CommandItem} from "@/components/ui/command";
import {Check} from "lucide-react";
import {Input} from "@/components/ui/input";
import {Loader2, Plus, Pencil, Trash} from "lucide-react";
import {log} from "console";
interface Department {
departmentId: string;
name: string;
}
export default function DoctorPage() {
const [doctors, setDoctors] = useState<any[]>([]);
const [departments, setDepartments] = useState<Department[]>([]);
const [loading, setLoading] = useState(true);
const [openModal, setOpenModal] = useState(false);
const [editing, setEditing] = useState<any>(null);
const [depOpen, setDepOpen] = useState(false);
const [search, setSearch] = useState("");
const [form, setForm] = useState<any>({
doctorId: "",
name: "",
designation: "",
workingStatus: "",
qualification: "",
departments: [],
timing: {},
});
const fetchAll = useCallback(async () => {
setLoading(true);
try {
const [docRes, depRes] = await Promise.all([
getDoctorsApi(),
getDepartmentsApi(),
]);
setDoctors(docRes?.data || []);
setDepartments(depRes?.data || []);
} catch (err) {
console.error(err);
} finally {
setLoading(false);
}
}, []);
useEffect(() => {
fetchAll();
}, [fetchAll]);
function handleChange(e: any) {
setForm({...form, [e.target.name]: e.target.value});
}
function handleDepartmentChange(depId: string) {
const exists = form.departments.includes(depId);
setForm({
...form,
departments: exists
? form.departments.filter((d: string) => d !== depId)
: [...form.departments, depId],
});
}
function handleTimingChange(day: string, value: string) {
setForm({
...form,
timing: {...form.timing, [day]: value},
});
}
function openAdd() {
setEditing(null);
setForm({
doctorId: "",
name: "",
designation: "",
workingStatus: "",
qualification: "",
departments: [],
timing: {},
});
setOpenModal(true);
}
async function openEdit(doc: any) {
setEditing(doc);
try {
const timingRes = await getDoctorTimingApi(doc.Doctor_ID);
const t = timingRes?.data || {};
const formattedTiming = {
sunday: t.Sunday || "",
monday: t.Monday || "",
tuesday: t.Tuesday || "",
wednesday: t.Wednesday || "",
thursday: t.Thursday || "",
friday: t.Friday || "",
saturday: t.Saturday || "",
additional: t.Additional || "",
};
setForm({
doctorId: doc.Doctor_ID,
name: doc.Name,
designation: doc.Designation,
workingStatus: doc.workingStatus || doc.Working_Status || "",
qualification: doc.Qualification,
departments: doc.Department_ID || [],
timing: formattedTiming,
});
setOpenModal(true);
} catch (err) {
console.error(err);
}
}
async function handleSubmit() {
try {
const cleanTiming: any = {};
Object.keys(form.timing).forEach((day) => {
if (form.timing[day]) {
cleanTiming[day] = form.timing[day];
}
});
const payload = {
...form,
timing: cleanTiming,
};
if (editing) {
await updateDoctorApi(editing.Doctor_ID, payload);
} else {
await createDoctorApi(payload);
}
setOpenModal(false);
fetchAll();
} catch (err) {
console.error(err);
}
}
async function handleDelete(id: string) {
if (!confirm("Delete doctor?")) return;
await deleteDoctorApi(id);
fetchAll();
}
return (
<div className="p-6 space-y-6">
<div className="flex justify-between items-center">
<h1 className="text-2xl font-bold">Doctors</h1>
<Button onClick={openAdd}>
<Plus className="mr-2 h-4 w-4" />
Add Doctor
</Button>
</div>
<Card>
<CardHeader>
<CardTitle>Doctor List</CardTitle>
</CardHeader>
<CardContent>
<div className="tw-overflow-x-auto">
<Table className="tw-min-w-[1000px]">
<TableHeader>
<TableRow>
<TableHead>ID</TableHead>
<TableHead>Name</TableHead>
<TableHead>Designation</TableHead>
<TableHead>Status</TableHead>
<TableHead>Qualification</TableHead>
<TableHead>Departments</TableHead>
<TableHead>Timing</TableHead>
<TableHead>Actions</TableHead>
</TableRow>
</TableHeader>
<TableBody>
{loading ? (
<TableRow>
<TableCell colSpan={8} className="text-center">
<Loader2 className="h-6 w-6 animate-spin mx-auto" />
</TableCell>
</TableRow>
) : (
doctors.map((doc) => (
<TableRow key={doc.Doctor_ID}>
<TableCell>{doc.Doctor_ID}</TableCell>
<TableCell>{doc.Name}</TableCell>
<TableCell>{doc.Designation}</TableCell>
<TableCell>{doc.Working_Status}</TableCell>
<TableCell>{doc.Qualification}</TableCell>
<TableCell>{doc.Department_ID?.join(", ")}</TableCell>
<TableCell className="max-w-[200px] whitespace-normal break-words">
<div className="tw-whitespace-nowrap">{doc.Timing}</div>
</TableCell>
<TableCell className="flex gap-2">
<Button
size="sm"
variant="outline"
onClick={() => openEdit(doc)}
>
<Pencil className="h-4 w-4" />
</Button>
<Button
size="sm"
variant="destructive"
onClick={() => handleDelete(doc.Doctor_ID)}
>
<Trash className="h-4 w-4" />
</Button>
</TableCell>
</TableRow>
))
)}
</TableBody>
</Table>
</div>
</CardContent>
</Card>
{/* MODAL */}
<Dialog open={openModal} onOpenChange={setOpenModal}>
<DialogContent className="tw-overflow-x-auto">
<DialogHeader>
<DialogTitle>{editing ? "Edit Doctor" : "Add Doctor"}</DialogTitle>
</DialogHeader>
<div className="space-y-4 max-h-[70vh] overflow-y-auto">
<Input
name="doctorId"
placeholder="Doctor ID"
value={form.doctorId}
onChange={handleChange}
disabled={!!editing}
/>
<Input
name="name"
placeholder="Name"
value={form.name}
onChange={handleChange}
/>
<Input
name="designation"
placeholder="Designation"
value={form.designation}
onChange={handleChange}
/>
<Input
name="workingStatus"
placeholder="Working Status"
value={form.workingStatus}
onChange={handleChange}
/>
{/* Departments MULTI SELECT */}
<div>
<p className="tw-font-medium tw-mb-2">Departments</p>
<Popover>
<PopoverTrigger asChild>
<Button
variant="outline"
className="tw-w-full tw-justify-between tw-text-left"
>
{form.departments.length > 0
? departments
.filter((d) =>
form.departments.includes(d.departmentId),
)
.map((d) => d.name)
.join(", ")
: "Select Departments"}
</Button>
</PopoverTrigger>
<PopoverContent className="tw-w-full tw-p-0">
<Command>
<div className="tw-p-2 tw-border-b">
<input
placeholder="Search department..."
className="tw-w-full tw-border tw-rounded-md tw-px-2 tw-py-1 tw-text-sm"
value={search}
onChange={(e) => setSearch(e.target.value)}
/>
</div>
<CommandGroup className="tw-max-h-60 tw-overflow-y-auto">
{departments
.filter((d) =>
d.name.toLowerCase().includes(search.toLowerCase()),
)
.map((dep) => {
const selected = form.departments.includes(
dep.departmentId,
);
return (
<CommandItem
key={dep.departmentId}
onSelect={() =>
handleDepartmentChange(dep.departmentId)
}
className="tw-flex tw-justify-between"
>
<span>{dep.name}</span>
{selected && (
<span className="tw-text-green-600 tw-text-sm">
</span>
)}
</CommandItem>
);
})}
</CommandGroup>
</Command>
</PopoverContent>
</Popover>
</div>
<Input
placeholder="Sunday"
value={form.timing.sunday || ""}
onChange={(e) => handleTimingChange("sunday", e.target.value)}
/>
<Input
placeholder="Monday"
value={form.timing.monday || ""}
onChange={(e) => handleTimingChange("monday", e.target.value)}
/>
<Input
placeholder="Tuesday"
value={form.timing.tuesday || ""}
onChange={(e) => handleTimingChange("tuesday", e.target.value)}
/>
<Input
placeholder="Wednesday"
value={form.timing.wednesday || ""}
onChange={(e) => handleTimingChange("wednesday", e.target.value)}
/>
<Input
placeholder="Thursday"
value={form.timing.thursday || ""}
onChange={(e) => handleTimingChange("thursday", e.target.value)}
/>
<Input
placeholder="Friday"
value={form.timing.friday || ""}
onChange={(e) => handleTimingChange("friday", e.target.value)}
/>
<Input
placeholder="Saturday"
value={form.timing.saturday || ""}
onChange={(e) => handleTimingChange("saturday", e.target.value)}
/>
<Input
placeholder="Additional"
value={form.timing.additional || ""}
onChange={(e) => handleTimingChange("additional", e.target.value)}
/>
</div>
<DialogFooter>
<Button variant="outline" onClick={() => setOpenModal(false)}>
Cancel
</Button>
<Button onClick={handleSubmit}>
{editing ? "Update" : "Create"}
</Button>
</DialogFooter>
</DialogContent>
</Dialog>
</div>
);
}