source: frontend/src/pages/departments/DepartmentDetail.js

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 2 weeks ago

Fix frontend appearance

  • Property mode set to 100644
File size: 4.9 KB
Line 
1import React, { useState, useEffect } from 'react';
2import { useParams, useNavigate } from 'react-router-dom';
3import { departmentService } from '../../services/departmentService';
4import Loading from '../../components/Loading';
5import ErrorAlert from '../../components/ErrorAlert';
6
7function DepartmentDetail() {
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 <h1 className="text-3xl font-bold" style={{ color: '#7c3aed' }}>{formatDepartmentName(department.departmentName)}</h1>
57 <button
58 onClick={() => navigate('/departments')}
59 className="bg-gray-300 text-gray-700 px-4 py-2 rounded hover:bg-gray-400"
60 >
61 Back
62 </button>
63 </div>
64
65 {error && <ErrorAlert message={error} onClose={() => setError(null)} />}
66
67 <div className="grid grid-cols-1 md:grid-cols-3 gap-6">
68 <div className="bg-white rounded-lg shadow p-6">
69 <h2 className="text-xl font-bold mb-4">Department Information</h2>
70 <div className="space-y-3">
71 <div className="flex justify-between">
72 <span className="font-semibold text-gray-600">Department ID:</span>
73 <span className="text-gray-800">{department.departmentId}</span>
74 </div>
75 <div className="flex justify-between">
76 <span className="font-semibold text-gray-600">Name:</span>
77 <span className="text-gray-800">{formatDepartmentName(department.departmentName)}</span>
78 </div>
79 <div className="flex justify-between">
80 <span className="font-semibold text-gray-600">Total Doctors:</span>
81 <span className="text-gray-800 font-bold">{doctors.length}</span>
82 </div>
83 </div>
84 </div>
85
86 <div className="bg-blue-50 rounded-lg shadow p-6">
87 <h2 className="text-xl font-bold mb-4">Quick Actions</h2>
88 <div className="space-y-2">
89 <button
90 onClick={() => navigate(`/departments/${departmentId}/doctors`)}
91 className="w-full bg-purple-600 text-white px-4 py-2 rounded hover:bg-purple-700"
92 >
93 View All Doctors
94 </button>
95 </div>
96 </div>
97 </div>
98
99 <div className="bg-white rounded-lg shadow p-6 mt-6">
100 <h2 className="text-xl font-bold mb-4">Assigned Doctors</h2>
101 {doctors.length > 0 ? (
102 <table className="w-full">
103 <thead className="bg-gray-100">
104 <tr>
105 <th className="px-4 py-2 text-left">Name</th>
106 <th className="px-4 py-2 text-left">Email</th>
107 <th className="px-4 py-2 text-left">Actions</th>
108 </tr>
109 </thead>
110 <tbody>
111 {doctors.map((doctor) => (
112 <tr key={doctor.doctorId} className="border-t hover:bg-gray-50">
113 <td className="px-4 py-2">
114 {doctor.firstName} {doctor.lastName}
115 </td>
116 <td className="px-4 py-2">{doctor.emailAddress}</td>
117 <td className="px-4 py-2">
118 <button
119 onClick={() => navigate(`/doctors/${doctor.doctorId}`)}
120 className="text-purple-600 hover:underline text-sm"
121 >
122 View Profile
123 </button>
124 </td>
125 </tr>
126 ))}
127 </tbody>
128 </table>
129 ) : (
130 <p className="text-gray-500 text-center py-4">No doctors assigned to this department</p>
131 )}
132 </div>
133 </div>
134 );
135}
136
137export default DepartmentDetail;
Note: See TracBrowser for help on using the repository browser.