fix(appointment): render real slot structure in time picker
The slots API returns data.sessions[].slots[] with start_time (HH:MM), is_available, and start/end unix timestamps — not the morning/evening + time/status shape the UI assumed. Add lib/appointmentSlots.js to flatten sessions into morning/evening by start_time, fetch with doctor.uuid, and read item.start_time / item.is_available in the slot grid. Derive the clinic address from the already-loaded doctor.address by location_id instead of a separate auth-required call. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -5,6 +5,7 @@ import Hours from "./hours";
|
||||
import Tabs from "../../Tabs";
|
||||
import { request } from "@/services/response";
|
||||
import SendAppo from "./SendAppo";
|
||||
import { adaptSlots, hasAvailable } from "@/lib/appointmentSlots";
|
||||
|
||||
const listTab = ["صبح", "بعد از ظهر"];
|
||||
const nameHours = ["hours_morning", "hours_afternoon"];
|
||||
@@ -13,63 +14,33 @@ function DateTime({ date, doctor, setStep, setSelectedSlot, setSelectedDate }) {
|
||||
const [value, setValue] = useState(0);
|
||||
const [hour, setHour] = useState();
|
||||
const [appo, setAppo] = useState("تاریخ مورد نظرتان را انتخاب کنید.");
|
||||
const [locationAddress, setLocationAddress] = useState(null);
|
||||
|
||||
const handleChange = (event, newValue) => {
|
||||
setHour();
|
||||
setValue(newValue);
|
||||
};
|
||||
|
||||
// دریافت آدرس بر اساس location_id وقتی ساعت انتخاب میشود
|
||||
useEffect(() => {
|
||||
if (hour && hour.location_id) {
|
||||
request
|
||||
.getDoctorAddress(hour.location_id)
|
||||
.then((res) => {
|
||||
setLocationAddress(res.address);
|
||||
})
|
||||
.catch((err) => {
|
||||
console.error("Error fetching address:", err);
|
||||
setLocationAddress(null);
|
||||
});
|
||||
} else {
|
||||
setLocationAddress(null);
|
||||
}
|
||||
}, [hour]);
|
||||
const locationAddress = hour?.location_id
|
||||
? doctor?.address?.find((a) => String(a.id) === String(hour.location_id))?.address ?? null
|
||||
: null;
|
||||
|
||||
useEffect(() => {
|
||||
if (date && doctor?.id) {
|
||||
if (date && doctor?.uuid) {
|
||||
setAppo("loading");
|
||||
setHour();
|
||||
request
|
||||
.getAppointment(doctor.id, date)
|
||||
.getAppointmentSlots(doctor.uuid, date)
|
||||
.then((res) => {
|
||||
setAppo(res);
|
||||
|
||||
// بررسی وجود اسلاتهای available در صبح و عصر
|
||||
const hasAvailableMorning = res.morning?.some(slot => slot.status === "available");
|
||||
const hasAvailableEvening = res.evening?.some(slot => slot.status === "available");
|
||||
|
||||
// اگر هر دو صبح و عصر اسلات available داشته باشند، صبح را نمایش بده
|
||||
if (hasAvailableMorning && hasAvailableEvening) {
|
||||
setValue(0);
|
||||
}
|
||||
// اگر فقط صبح اسلات available داشته باشد
|
||||
else if (hasAvailableMorning && !hasAvailableEvening) {
|
||||
setValue(0);
|
||||
}
|
||||
// اگر فقط عصر اسلات available داشته باشد یا هیچ کدام available نداشته باشند، عصر را نمایش بده
|
||||
else {
|
||||
setValue(1);
|
||||
}
|
||||
const slots = adaptSlots(res);
|
||||
setAppo(slots);
|
||||
setValue(hasAvailable(slots.morning) || !hasAvailable(slots.evening) ? 0 : 1);
|
||||
})
|
||||
.catch((err) => {
|
||||
setAppo("در حال حاضر، نوبتی برای ساعتهای صبح موجود نمیباشد.");
|
||||
// در صورت خطا هم عصر را نمایش بده
|
||||
setValue(1);
|
||||
.catch(() => {
|
||||
setAppo("در حال حاضر، نوبتی برای این روز موجود نمیباشد.");
|
||||
setValue(0);
|
||||
});
|
||||
}
|
||||
}, [date, doctor?.id]);
|
||||
}, [date, doctor?.uuid]);
|
||||
|
||||
return (
|
||||
<div className="w-full">
|
||||
|
||||
Reference in New Issue
Block a user