import React, { useState, useEffect } from 'react';
import { Link, useSearchParams } from 'react-router-dom';
import { appointmentService } from '../../services/appointmentService';
import Loading from '../../components/Loading';
import ErrorAlert from '../../components/ErrorAlert';
function AppointmentList() {
const [appointments, setAppointments] = useState([]);
const [loading, setLoading] = useState(true);
const [error, setError] = useState(null);
const [searchParams] = useSearchParams();
const doctorId = searchParams.get('doctorId');
const patientId = searchParams.get('patientId');
const user = JSON.parse(localStorage.getItem('user') || '{}');
useEffect(() => {
const fetchAppointmentsData = async () => {
try {
setLoading(true);
let response;
if (patientId) {
response = await appointmentService.getAppointmentsForPatient(patientId);
} else if (doctorId) {
response = await appointmentService.getAppointmentsForDoctor(doctorId);
} else {
response = await appointmentService.getAllAppointments();
}
setAppointments(response.data || []);
} catch (err) {
setError('Failed to fetch appointments');
console.error(err);
} finally {
setLoading(false);
}
};
fetchAppointmentsData();
}, [doctorId, patientId]);
const handleCancelAppointment = async (id) => {
if (window.confirm('Are you sure you want to cancel this appointment?')) {
try {
await appointmentService.cancelAppointment(id);
setAppointments(appointments.map(apt =>
apt.appointmentId === id ? { ...apt, status: 'CANCELLED' } : apt
));
} catch (err) {
setError('Failed to cancel appointment');
}
}
};
if (loading) return
| Patient | Doctor | Date | Time | Status | Actions |
|---|---|---|---|---|---|
| {appointment.patient?.firstName} {appointment.patient?.lastName} | Dr. {appointment.doctor?.firstName} {appointment.doctor?.lastName} | {appointment.appointmentDate} | {appointment.appointmentTime} | {appointment.status} | {appointment.status === 'SCHEDULED' && ( (user.role === 'DOCTOR' && appointment.doctor?.doctorId !== user.doctorId) ? null : ( ) )} |