import React, { useState, useEffect } from 'react'; import { referralService } from '../../services/referralService'; import { patientService } from '../../services/patientService'; import { doctorService } from '../../services/doctorService'; import { medicalRecordService } from '../../services/medicalRecordService'; import ErrorAlert from '../../components/ErrorAlert'; import SuccessAlert from '../../components/SuccessAlert'; function ReferralList() { const user = JSON.parse(localStorage.getItem('user') || '{}'); const isDoctor = user.role === 'DOCTOR'; const [referrals, setReferrals] = useState([]); const [doctors, setDoctors] = useState([]); const [patients, setPatients] = useState([]); const [error, setError] = useState(null); const [success, setSuccess] = useState(null); const [searchType, setSearchType] = useState('patient'); // 'patient', 'fromDoctor', 'toDoctor' const [searchValue, setSearchValue] = useState(''); const [searched, setSearched] = useState(false); const [loading, setLoading] = useState(false); // For creating referral const [showCreateForm, setShowCreateForm] = useState(false); const getDefaultAppointmentDate = () => { const tomorrow = new Date(); tomorrow.setDate(tomorrow.getDate() + 1); return tomorrow.toISOString().split('T')[0]; }; const [formData, setFormData] = useState({ fromDoctorId: isDoctor ? (user.doctorId || '') : '', toDoctorId: '', patientId: '', recordId: '', reason: '', referralDate: new Date().toISOString().split('T')[0], appointmentDate: getDefaultAppointmentDate(), appointmentTime: '14:00', }); useEffect(() => { loadDoctorsAndPatients(); }, []); const loadDoctorsAndPatients = async () => { try { const [doctorsRes, patientsRes] = await Promise.all([ doctorService.getAllDoctors(), patientService.getAllPatients(), ]); setDoctors(doctorsRes.data); setPatients(patientsRes.data); } catch (err) { console.error('Error loading data:', err); } }; const handleSearch = async (e) => { e.preventDefault(); setError(null); setLoading(true); try { if (!searchValue.trim()) { setError('Please enter a search value'); setLoading(false); return; } let response; if (searchType === 'patient') { response = await referralService.getReferralsByPatient(searchValue); } else if (searchType === 'fromDoctor') { response = await referralService.getReferralsByFromDoctor(searchValue); } else { response = await referralService.getReferralsByToDoctor(searchValue); } setReferrals(Array.isArray(response.data) ? response.data : [response.data]); setSearched(true); } catch (err) { setError('No referrals found'); setReferrals([]); setSearched(true); } finally { setLoading(false); } }; const handleCreateReferral = async (e) => { e.preventDefault(); setError(null); try { if (!formData.fromDoctorId || !formData.toDoctorId || !formData.patientId || !formData.reason || !formData.appointmentDate || !formData.appointmentTime) { setError('Please fill in all required fields'); return; } // Get patient's medical record ID const patientId = parseInt(formData.patientId); const medicalRecordRes = await medicalRecordService.getMedicalRecordByPatientId(patientId); const recordId = medicalRecordRes.data.recordId; const referralData = { medicalRecordId: recordId, fromDoctorId: parseInt(formData.fromDoctorId), toDoctorId: parseInt(formData.toDoctorId), reason: formData.reason, referralDate: formData.referralDate, appointmentDate: formData.appointmentDate, appointmentTime: formData.appointmentTime + ':00', }; await referralService.createReferral(referralData); setSuccess('Referral created successfully!'); setFormData({ fromDoctorId: '', toDoctorId: '', patientId: '', recordId: '', reason: '', referralDate: new Date().toISOString().split('T')[0], appointmentDate: getDefaultAppointmentDate(), appointmentTime: '14:00', }); setShowCreateForm(false); // Refresh referrals list setTimeout(() => { setSearched(false); setSearchValue(''); }, 1500); } catch (err) { setError(err.response?.data?.error || 'Failed to create referral'); } }; const handleFormChange = (e) => { const { name, value } = e.target; setFormData(prev => ({ ...prev, [name]: value })); }; return (

Doctor Referrals

{error && setError(null)} />} {success && setSuccess(null)} />} {/* Create Referral Button */}
{/* Create Referral Form */} {showCreateForm && (

Create New Referral

{isDoctor ? (

Dr. {user.firstName} {user.lastName}

) : ( )}