| 1 | import React, { useState, useEffect } from 'react';
|
|---|
| 2 | import { useParams, useNavigate } from 'react-router-dom';
|
|---|
| 3 | import { departmentService } from '../../services/departmentService';
|
|---|
| 4 | import Loading from '../../components/Loading';
|
|---|
| 5 | import ErrorAlert from '../../components/ErrorAlert';
|
|---|
| 6 |
|
|---|
| 7 | function DoctorsByDepartment() {
|
|---|
| 8 | const formatDepartmentName = (name) => {
|
|---|
| 9 | if (!name) return 'N/A';
|
|---|
| 10 | return name.replace(/_DEPT$/, '').replace(/_/g, ' ').split(' ').map(word => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase()).join(' ');
|
|---|
| 11 | };
|
|---|
| 12 |
|
|---|
| 13 | const { departmentId } = useParams();
|
|---|
| 14 | const navigate = useNavigate();
|
|---|
| 15 | const [department, setDepartment] = useState(null);
|
|---|
| 16 | const [doctors, setDoctors] = useState([]);
|
|---|
| 17 | const [loading, setLoading] = useState(true);
|
|---|
| 18 | const [error, setError] = useState(null);
|
|---|
| 19 |
|
|---|
| 20 | useEffect(() => {
|
|---|
| 21 | const fetchDepartmentAndDoctors = async () => {
|
|---|
| 22 | try {
|
|---|
| 23 | setLoading(true);
|
|---|
| 24 |
|
|---|
| 25 | // Fetch department details
|
|---|
| 26 | const deptResponse = await departmentService.getDepartmentById(departmentId);
|
|---|
| 27 | setDepartment(deptResponse.data);
|
|---|
| 28 |
|
|---|
| 29 | // Fetch doctors for this department
|
|---|
| 30 | const doctorsResponse = await departmentService.getDoctorsByDepartment(departmentId);
|
|---|
| 31 | setDoctors(doctorsResponse.data);
|
|---|
| 32 | } catch (err) {
|
|---|
| 33 | setError('Failed to fetch data');
|
|---|
| 34 | console.error(err);
|
|---|
| 35 | } finally {
|
|---|
| 36 | setLoading(false);
|
|---|
| 37 | }
|
|---|
| 38 | };
|
|---|
| 39 |
|
|---|
| 40 | fetchDepartmentAndDoctors();
|
|---|
| 41 | }, [departmentId]);
|
|---|
| 42 |
|
|---|
| 43 | if (loading) return <Loading />;
|
|---|
| 44 |
|
|---|
| 45 | if (!department) {
|
|---|
| 46 | return (
|
|---|
| 47 | <div>
|
|---|
| 48 | <ErrorAlert message="Department not found" onClose={() => navigate('/departments')} />
|
|---|
| 49 | </div>
|
|---|
| 50 | );
|
|---|
| 51 | }
|
|---|
| 52 |
|
|---|
| 53 | return (
|
|---|
| 54 | <div>
|
|---|
| 55 | <div className="flex justify-between items-center mb-6">
|
|---|
| 56 | <div>
|
|---|
| 57 | <h1 className="text-3xl font-bold" style={{ color: '#7c3aed' }}>{formatDepartmentName(department.departmentName)}</h1>
|
|---|
| 58 | <p className="text-gray-600">Doctors in this department: {doctors.length}</p>
|
|---|
| 59 | </div>
|
|---|
| 60 | <button
|
|---|
| 61 | onClick={() => navigate('/departments')}
|
|---|
| 62 | className="bg-gray-300 text-gray-700 px-4 py-2 rounded hover:bg-gray-400"
|
|---|
| 63 | >
|
|---|
| 64 | Back
|
|---|
| 65 | </button>
|
|---|
| 66 | </div>
|
|---|
| 67 |
|
|---|
| 68 | {error && <ErrorAlert message={error} onClose={() => setError(null)} />}
|
|---|
| 69 |
|
|---|
| 70 | <div className="bg-white rounded-lg shadow overflow-hidden">
|
|---|
| 71 | {doctors.length > 0 ? (
|
|---|
| 72 | <table className="w-full">
|
|---|
| 73 | <thead className="bg-gray-100">
|
|---|
| 74 | <tr>
|
|---|
| 75 | <th className="px-6 py-3 text-left text-sm font-semibold">Name</th>
|
|---|
| 76 | <th className="px-6 py-3 text-left text-sm font-semibold">Email</th>
|
|---|
| 77 | <th className="px-6 py-3 text-left text-sm font-semibold">Actions</th>
|
|---|
| 78 | </tr>
|
|---|
| 79 | </thead>
|
|---|
| 80 | <tbody>
|
|---|
| 81 | {doctors.map((doctor) => (
|
|---|
| 82 | <tr key={doctor.doctorId} className="border-t hover:bg-gray-50">
|
|---|
| 83 | <td className="px-6 py-3">
|
|---|
| 84 | {doctor.firstName} {doctor.lastName}
|
|---|
| 85 | </td>
|
|---|
| 86 | <td className="px-6 py-3">{doctor.emailAddress}</td>
|
|---|
| 87 | <td className="px-6 py-3">
|
|---|
| 88 | <button
|
|---|
| 89 | onClick={() => navigate(`/doctors/${doctor.doctorId}`)}
|
|---|
| 90 | className="text-purple-600 hover:underline"
|
|---|
| 91 | >
|
|---|
| 92 | View Profile
|
|---|
| 93 | </button>
|
|---|
| 94 | </td>
|
|---|
| 95 | </tr>
|
|---|
| 96 | ))}
|
|---|
| 97 | </tbody>
|
|---|
| 98 | </table>
|
|---|
| 99 | ) : (
|
|---|
| 100 | <div className="p-6 text-center">
|
|---|
| 101 | <p className="text-gray-500">No doctors assigned to this department</p>
|
|---|
| 102 | </div>
|
|---|
| 103 | )}
|
|---|
| 104 | </div>
|
|---|
| 105 | </div>
|
|---|
| 106 | );
|
|---|
| 107 | }
|
|---|
| 108 |
|
|---|
| 109 | export default DoctorsByDepartment; |
|---|