= {};
adminAvailability.available_days.forEach((day) => {
- initialSlots[day] = dayTimeSlots[day] || ["morning", "lunchtime", "afternoon"];
+ initialSlots[day] = dayTimeSlots[day] || ["morning", "afternoon"];
});
setDayTimeSlots(initialSlots);
+ } else {
+ // No existing availability, start fresh
+ setSelectedDays([]);
+ setDayTimeSlots({});
}
setAvailabilityDialogOpen(true);
};
@@ -213,7 +281,6 @@ export default function Booking() {
const data = await listAppointments();
setAppointments(data || []);
} catch (error) {
- console.error("Failed to fetch appointments:", error);
toast.error("Failed to load appointments. Please try again.");
setAppointments([]);
} finally {
@@ -337,7 +404,6 @@ export default function Booking() {
const data = await listAppointments();
setAppointments(data || []);
} catch (error) {
- console.error("Failed to schedule appointment:", error);
const errorMessage = error instanceof Error ? error.message : "Failed to schedule appointment";
toast.error(errorMessage);
} finally {
@@ -363,7 +429,6 @@ export default function Booking() {
const data = await listAppointments();
setAppointments(data || []);
} catch (error) {
- console.error("Failed to reject appointment:", error);
const errorMessage = error instanceof Error ? error.message : "Failed to reject appointment";
toast.error(errorMessage);
} finally {
@@ -371,14 +436,6 @@ export default function Booking() {
}
};
- // Generate time slots
- const timeSlots = [];
- for (let hour = 8; hour <= 18; hour++) {
- for (let minute = 0; minute < 60; minute += 30) {
- const timeString = `${hour.toString().padStart(2, "0")}:${minute.toString().padStart(2, "0")}`;
- timeSlots.push(timeString);
- }
- }
const filteredAppointments = appointments.filter(
(appointment) =>
@@ -442,34 +499,70 @@ export default function Booking() {
Weekly Availability
- {adminAvailability.available_days_display && adminAvailability.available_days_display.length > 0 ? (
+ {(adminAvailability.availability_schedule || (adminAvailability.available_days_display && adminAvailability.available_days_display.length > 0)) ? (
- {adminAvailability.available_days.map((dayNum, index) => {
- const dayName = daysOfWeek.find(d => d.value === dayNum)?.label || adminAvailability.available_days_display[index];
- const timeSlots = dayTimeSlots[dayNum] || [];
- const slotLabels = timeSlots.map(slot => {
- const option = timeSlotOptions.find(opt => opt.value === slot);
- return option ? option.label : slot;
- });
- return (
-
-
- {dayName}
- {slotLabels.length > 0 && (
-
- ({slotLabels.join(", ")})
-
- )}
-
- );
- })}
+ {(() => {
+ // Try new format first
+ if (adminAvailability.availability_schedule) {
+ return Object.keys(adminAvailability.availability_schedule).map((dayKey) => {
+ const dayNum = parseInt(dayKey);
+ const dayName = daysOfWeek.find(d => d.value === dayNum)?.label || `Day ${dayNum}`;
+ const timeSlots = adminAvailability.availability_schedule![dayKey] || [];
+ const slotLabels = timeSlots.map((slot: string) => {
+ const option = timeSlotOptions.find(opt => opt.value === slot);
+ return option ? option.label : slot;
+ });
+ return (
+
+
+ {dayName}
+ {slotLabels.length > 0 && (
+
+ ({slotLabels.join(", ")})
+
+ )}
+
+ );
+ });
+ }
+ // Fallback to legacy format
+ else if (adminAvailability.available_days && adminAvailability.available_days.length > 0) {
+ return adminAvailability.available_days.map((dayNum, index) => {
+ const dayName = daysOfWeek.find(d => d.value === dayNum)?.label || adminAvailability.available_days_display?.[index];
+ const timeSlots = dayTimeSlots[dayNum] || [];
+ const slotLabels = timeSlots.map(slot => {
+ const option = timeSlotOptions.find(opt => opt.value === slot);
+ return option ? option.label : slot;
+ });
+ return (
+
+
+ {dayName}
+ {slotLabels.length > 0 && (
+
+ ({slotLabels.join(", ")})
+
+ )}
+
+ );
+ });
+ }
+ return null;
+ })()}
) : (
@@ -586,13 +679,19 @@ export default function Booking() {
- {appointment.preferred_dates && appointment.preferred_dates.length > 0 ? (
+ {appointment.preferred_dates ? (
- {appointment.preferred_dates.slice(0, 2).map((date, idx) => (
- {formatDate(date)}
- ))}
- {appointment.preferred_dates.length > 2 && (
- +{appointment.preferred_dates.length - 2} more
+ {Array.isArray(appointment.preferred_dates) ? (
+ <>
+ {(appointment.preferred_dates as string[]).slice(0, 2).map((date, idx) => (
+ {formatDate(date)}
+ ))}
+ {appointment.preferred_dates.length > 2 && (
+ +{appointment.preferred_dates.length - 2} more
+ )}
+ >
+ ) : (
+ {appointment.preferred_dates_display || appointment.preferred_dates}
)}
) : (
@@ -655,103 +754,27 @@ export default function Booking() {
{/* Schedule Appointment Dialog */}
-
+ {
+ setScheduleDialogOpen(open);
+ if (!open) {
+ setScheduledDate(undefined);
+ setScheduledTime("09:00");
+ setScheduledDuration(60);
+ }
+ }}
+ appointment={selectedAppointment}
+ scheduledDate={scheduledDate}
+ setScheduledDate={setScheduledDate}
+ scheduledTime={scheduledTime}
+ setScheduledTime={setScheduledTime}
+ scheduledDuration={scheduledDuration}
+ setScheduledDuration={setScheduledDuration}
+ onSchedule={handleSchedule}
+ isScheduling={isScheduling}
+ isDark={isDark}
+ />
{/* Reject Appointment Dialog */}
|