import React, { useState, useEffect } from 'react';
import { Link } from 'react-router-dom';
import { patientService } from '../services/patientService';
import { doctorService } from '../services/doctorService';
import { appointmentService } from '../services/appointmentService';
import { labService } from '../services/labService';
import { billingService } from '../services/billingService';
import { medicalRecordService } from '../services/medicalRecordService';
import Loading from '../components/Loading';
function Dashboard() {
const [stats, setStats] = useState({
patients: 0,
doctors: 0,
appointments: 0,
});
const [userAppointments, setUserAppointments] = useState([]);
const [doctorInfo, setDoctorInfo] = useState(null);
const [pendingLabTests, setPendingLabTests] = useState([]);
const [billings, setBillings] = useState([]);
const [medicalRecords, setMedicalRecords] = useState([]);
const [patientBillings, setPatientBillings] = useState([]);
const [loading, setLoading] = useState(true);
const user = JSON.parse(localStorage.getItem('user') || '{}');
const isPatient = user.role === 'PATIENT';
const isDoctor = user.role === 'DOCTOR';
const isLabTechnician = user.role === 'LAB_TECHNICIAN';
const isBillingAdmin = user.role === 'BILLING_ADMIN';
useEffect(() => {
const fetchStats = async () => {
try {
if (isPatient) {
// For patients, fetch their own appointments, medical records, and billing
const [appointmentsRes, medicalRes, billingRes] = await Promise.all([
appointmentService.getAppointmentsForPatient(user.patientId),
medicalRecordService.getMedicalRecordByPatientId(user.patientId),
billingService.getBillingHistoryForPatient(user.patientId)
]);
setUserAppointments(appointmentsRes.data || []);
// Handle medical records - could be single object or array
const medicalData = medicalRes.data;
const medicalArray = Array.isArray(medicalData) ? medicalData : (medicalData ? [medicalData] : []);
setMedicalRecords(medicalArray);
setPatientBillings(billingRes.data || []);
} else if (isDoctor) {
// For doctors, fetch their own appointments and full doctor information
const [appointmentsRes, doctorRes] = await Promise.all([
appointmentService.getAppointmentsForDoctor(user.doctorId),
doctorService.getDoctorById(user.doctorId),
]);
setUserAppointments(appointmentsRes.data || []);
setDoctorInfo(doctorRes.data);
} else if (isLabTechnician) {
// For lab technicians, fetch pending lab tests
const pendingRes = await labService.getPendingLabTests();
setPendingLabTests(pendingRes.data || []);
} else if (isBillingAdmin) {
// For billing admins, fetch all billing records
const billingsRes = await billingService.getAllBillings();
setBillings(billingsRes.data || []);
} else {
// For admin/staff, fetch all stats
const [patientsRes, doctorsRes, appointmentsRes] = await Promise.all([
patientService.getAllPatients(),
doctorService.getAllDoctors(),
appointmentService.getAllAppointments(),
]);
setStats({
patients: patientsRes.data.length,
doctors: doctorsRes.data.length,
appointments: appointmentsRes.data.length,
});
}
} catch (error) {
console.error('Error fetching stats:', error);
} finally {
setLoading(false);
}
};
fetchStats();
}, [user.userId, isPatient, isDoctor, isLabTechnician]);
if (loading) return
No appointments scheduled
) : (| Doctor | Date | Time | Status |
|---|---|---|---|
| {apt.doctor?.firstName} {apt.doctor?.lastName} | {apt.appointmentDate} | {apt.appointmentTime} | {apt.status} |
No billing records
) : (No appointments scheduled
) : (| Patient | Date | Time | Status |
|---|---|---|---|
| {apt.patient?.firstName} {apt.patient?.lastName} | {apt.appointmentDate} | {apt.appointmentTime} | {apt.status} |
No pending lab tests
) : (| Test Name | Patient | Doctor | Test Date | Notes |
|---|---|---|---|---|
| {test.testName} | {test.patientName} | {test.doctorName} | {test.testDate} | {test.notes || 'N/A'} |
No billing records
) : (| Patient | Amount | Status | Date |
|---|---|---|---|
| {bill.patientName} | ${bill.totalCost} | {bill.paymentStatus} | {bill.paymentDate || 'N/A'} |
{title}
{count.toLocaleString()}
{delta}
Activity feed coming soon...