| 1 | import React, { useState, useEffect } from 'react';
|
|---|
| 2 | import { useParams, Link, useNavigate } from 'react-router-dom';
|
|---|
| 3 | import { doctorService } from '../../services/doctorService';
|
|---|
| 4 | import Loading from '../../components/Loading';
|
|---|
| 5 | import ErrorAlert from '../../components/ErrorAlert';
|
|---|
| 6 |
|
|---|
| 7 | function DoctorDetail() {
|
|---|
| 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 { id } = useParams();
|
|---|
| 14 | const navigate = useNavigate();
|
|---|
| 15 | const user = JSON.parse(localStorage.getItem('user') || '{}');
|
|---|
| 16 | const isPatient = user.role === 'PATIENT';
|
|---|
| 17 | const isAdmin = user.role === 'ADMIN';
|
|---|
| 18 | const isLabTechnician = user.role === 'LAB_TECHNICIAN';
|
|---|
| 19 | const isBillingAdmin = user.role === 'BILLING_ADMIN';
|
|---|
| 20 |
|
|---|
| 21 | const [doctor, setDoctor] = useState(null);
|
|---|
| 22 | const [loading, setLoading] = useState(true);
|
|---|
| 23 | const [error, setError] = useState(null);
|
|---|
| 24 |
|
|---|
| 25 | useEffect(() => {
|
|---|
| 26 | fetchDoctor();
|
|---|
| 27 | }, [id]);
|
|---|
| 28 |
|
|---|
| 29 | const fetchDoctor = async () => {
|
|---|
| 30 | try {
|
|---|
| 31 | setLoading(true);
|
|---|
| 32 | const response = await doctorService.getDoctorById(id);
|
|---|
| 33 | setDoctor(response.data);
|
|---|
| 34 | } catch (err) {
|
|---|
| 35 | setError('Failed to fetch doctor details');
|
|---|
| 36 | console.error(err);
|
|---|
| 37 | } finally {
|
|---|
| 38 | setLoading(false);
|
|---|
| 39 | }
|
|---|
| 40 | };
|
|---|
| 41 |
|
|---|
| 42 | if (loading) return <Loading />;
|
|---|
| 43 |
|
|---|
| 44 | if (!doctor) {
|
|---|
| 45 | return (
|
|---|
| 46 | <div>
|
|---|
| 47 | <ErrorAlert message="Doctor not found" onClose={() => navigate('/doctors')} />
|
|---|
| 48 | </div>
|
|---|
| 49 | );
|
|---|
| 50 | }
|
|---|
| 51 |
|
|---|
| 52 | return (
|
|---|
| 53 | <div>
|
|---|
| 54 | <div className="flex justify-between items-center mb-6">
|
|---|
| 55 | <h1 className="text-3xl font-bold" style={{ color: '#7c3aed' }}>Dr. {doctor.firstName} {doctor.lastName}</h1>
|
|---|
| 56 | <div className="space-x-2">
|
|---|
| 57 | {isAdmin && (
|
|---|
| 58 | <Link to={`/doctors/${id}/edit`} className="bg-purple-600 text-white px-4 py-2 rounded hover:bg-purple-700">
|
|---|
| 59 | Edit
|
|---|
| 60 | </Link>
|
|---|
| 61 | )}
|
|---|
| 62 | <button onClick={() => navigate('/doctors')} className="bg-gray-300 text-gray-700 px-4 py-2 rounded hover:bg-gray-400">
|
|---|
| 63 | Back
|
|---|
| 64 | </button>
|
|---|
| 65 | </div>
|
|---|
| 66 | </div>
|
|---|
| 67 |
|
|---|
| 68 | {error && <ErrorAlert message={error} onClose={() => setError(null)} />}
|
|---|
| 69 |
|
|---|
| 70 | <div className="grid grid-cols-1 md:grid-cols-2 gap-6">
|
|---|
| 71 | <div className="bg-white rounded-lg shadow p-6">
|
|---|
| 72 | <h2 className="text-xl font-bold mb-4">Professional Information</h2>
|
|---|
| 73 | <div className="space-y-3">
|
|---|
| 74 | <InfoRow label="Email" value={doctor.emailAddress} />
|
|---|
| 75 | <InfoRow label="Specialization" value={doctor.specialization?.specializationName} />
|
|---|
| 76 | <InfoRow label="Level" value={doctor.level?.level} />
|
|---|
| 77 | <InfoRow label="Department" value={formatDepartmentName(doctor.department?.departmentName)} />
|
|---|
| 78 | </div>
|
|---|
| 79 | </div>
|
|---|
| 80 |
|
|---|
| 81 | {!isPatient && !isLabTechnician && !isBillingAdmin && (
|
|---|
| 82 | <div className="bg-white rounded-lg shadow p-6">
|
|---|
| 83 | <h2 className="text-xl font-bold mb-4">Quick Links</h2>
|
|---|
| 84 | <div className="space-y-2">
|
|---|
| 85 | <Link to={`/appointments?doctorId=${id}`} className="block p-3 bg-blue-50 hover:bg-purple-100 rounded text-purple-600">
|
|---|
| 86 | View My Appointments
|
|---|
| 87 | </Link>
|
|---|
| 88 | <Link to="/patients" className="block p-3 bg-green-50 hover:bg-green-100 rounded text-green-600">
|
|---|
| 89 | View Patients
|
|---|
| 90 | </Link>
|
|---|
| 91 | </div>
|
|---|
| 92 | </div>
|
|---|
| 93 | )}
|
|---|
| 94 | </div>
|
|---|
| 95 | </div>
|
|---|
| 96 | );
|
|---|
| 97 | }
|
|---|
| 98 |
|
|---|
| 99 | function InfoRow({ label, value }) {
|
|---|
| 100 | return (
|
|---|
| 101 | <div className="flex justify-between">
|
|---|
| 102 | <span className="text-gray-800">{label}:</span>
|
|---|
| 103 | <span className="text-gray-800">{value || 'N/A'}</span>
|
|---|
| 104 | </div>
|
|---|
| 105 | );
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|
| 108 | export default DoctorDetail;
|
|---|