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

Last change on this file was 20468d3, checked in by MBK <marija.karapandzova@…>, 4 months ago

Add pages and routing for patients, doctors, departments and appointments with api services

  • Property mode set to 100644
File size: 4.4 KB
Line 
1import React, { useState, useEffect } from 'react';
2import { Link, useNavigate } from 'react-router-dom';
3import { departmentService } from '../../services/departmentService';
4import Loading from '../../components/Loading';
5import ErrorAlert from '../../components/ErrorAlert';
6
7function DepartmentList() {
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 user = JSON.parse(localStorage.getItem('user') || '{}');
14 const isAdmin = user.role === 'ADMIN';
15 const [departments, setDepartments] = useState([]);
16 const [loading, setLoading] = useState(true);
17 const [error, setError] = useState(null);
18 const navigate = useNavigate();
19
20 useEffect(() => {
21 fetchDepartments();
22 }, []);
23
24 const fetchDepartments = async () => {
25 try {
26 setLoading(true);
27 const response = await departmentService.getAllDepartments();
28 setDepartments(response.data);
29 } catch (err) {
30 setError('Failed to fetch departments');
31 console.error(err);
32 } finally {
33 setLoading(false);
34 }
35 };
36
37 if (loading) return <Loading />;
38
39 return (
40 <div>
41 <div className="flex justify-between items-center mb-6">
42 <h1 style={{ fontSize: '36px', fontWeight: 'normal', color: '#7c3aed' }}>Hospital Departments</h1>
43 {isAdmin && (
44 <div className="space-x-2">
45 <button
46 onClick={() => navigate('/departments/new')}
47 style={{
48 background: '#bfdbfe',
49 color: '#1e1035',
50 padding: '8px 16px',
51 borderRadius: '6px',
52 border: 'none',
53 cursor: 'pointer',
54 fontSize: '14px',
55 fontWeight: '400'
56 }}
57 onMouseEnter={(e) => e.currentTarget.style.background = '#93c5fd'}
58 onMouseLeave={(e) => e.currentTarget.style.background = '#bfdbfe'}
59 >
60 Add Department
61 </button>
62 </div>
63 )}
64 </div>
65
66 {error && <ErrorAlert message={error} onClose={() => setError(null)} />}
67
68 <div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
69 {departments.map((dept) => (
70 <div key={dept.departmentId} className="bg-white rounded-lg shadow p-6 hover:shadow-lg transition flex flex-col items-center">
71 <h2 style={{ fontSize: '28px', fontWeight: 'normal', marginBottom: '20px', textAlign: 'center' }}>{formatDepartmentName(dept.departmentName)}</h2>
72 <div className="flex gap-3 w-full">
73 <Link
74 to={`/departments/${dept.departmentId}/doctors`}
75 style={{
76 flex: 1,
77 display: 'flex',
78 alignItems: 'center',
79 justifyContent: 'center',
80 background: '#9333ea',
81 color: 'white',
82 padding: '6px 10px',
83 borderRadius: '50px',
84 textDecoration: 'none',
85 fontSize: '14px',
86 fontWeight: '400'
87 }}
88 onMouseEnter={(e) => e.currentTarget.style.background = '#7e22ce'}
89 onMouseLeave={(e) => e.currentTarget.style.background = '#9333ea'}
90 >
91 View Doctors
92 </Link>
93 <Link
94 to={`/departments/${dept.departmentId}`}
95 style={{
96 flex: 1,
97 display: 'flex',
98 alignItems: 'center',
99 justifyContent: 'center',
100 background: '#7c3aed',
101 color: 'white',
102 padding: '6px 10px',
103 borderRadius: '50px',
104 textDecoration: 'none',
105 fontSize: '14px',
106 fontWeight: '400'
107 }}
108 onMouseEnter={(e) => e.currentTarget.style.background = '#6d28d9'}
109 onMouseLeave={(e) => e.currentTarget.style.background = '#7c3aed'}
110 >
111 View Details
112 </Link>
113 </div>
114 </div>
115 ))}
116 </div>
117
118 {departments.length === 0 && (
119 <div className="text-center py-12">
120 <p className="text-gray-500">No departments found</p>
121 </div>
122 )}
123 </div>
124 );
125}
126
127export default DepartmentList;
Note: See TracBrowser for help on using the repository browser.