refactor: enhance date selection logic in DatePicker and update appointment request parameters
This commit is contained in:
@@ -5,6 +5,7 @@ import FirstDatePicker from "./date/FirstDatePicker";
|
||||
import SecondDatePicker from "./date/SecondDatePicker";
|
||||
import { dateToTimestamp } from "@/helper";
|
||||
import moment from "moment-jalaali";
|
||||
import "moment-timezone";
|
||||
|
||||
function DatePicker({ setDate, disabledDates = [] }) {
|
||||
const nextIconDatePickerF = useRef();
|
||||
@@ -15,6 +16,7 @@ function DatePicker({ setDate, disabledDates = [] }) {
|
||||
const [startDate, setStartDate] = useState(null);
|
||||
const [endDate, setEndDate] = useState();
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [autoSelected, setAutoSelected] = useState(false);
|
||||
|
||||
const handleStartDateChange = (date, e) => {
|
||||
if (date) {
|
||||
@@ -59,6 +61,47 @@ function DatePicker({ setDate, disabledDates = [] }) {
|
||||
}
|
||||
}, [nextIconDatePickerS, disabledDates]);
|
||||
|
||||
// انتخاب خودکار امروز یا نزدیکترین روز قابل انتخاب
|
||||
useEffect(() => {
|
||||
if (!loading && !autoSelected && disabledDates) {
|
||||
const today = moment().tz("Asia/Tehran");
|
||||
const todayTimestamp = moment().tz("Asia/Tehran").startOf("day").unix();
|
||||
|
||||
// بررسی اینکه امروز قابل انتخاب است یا نه
|
||||
const isTodayDisabled = disabledDates.some((ts) =>
|
||||
moment.unix(ts).startOf("day").isSame(today.startOf("day"))
|
||||
);
|
||||
|
||||
if (!isTodayDisabled) {
|
||||
// امروز قابل انتخاب است
|
||||
setStartDate(today);
|
||||
setDate(todayTimestamp);
|
||||
setAutoSelected(true);
|
||||
} else {
|
||||
// پیدا کردن نزدیکترین روز قابل انتخاب
|
||||
let nearestDate = null;
|
||||
let daysToCheck = 30; // بررسی 30 روز آینده
|
||||
|
||||
for (let i = 1; i <= daysToCheck; i++) {
|
||||
const futureDate = moment().tz("Asia/Tehran").add(i, "days");
|
||||
const futureDateTimestamp = futureDate.clone().startOf("day").unix();
|
||||
|
||||
const isFutureDateDisabled = disabledDates.some((ts) =>
|
||||
moment.unix(ts).startOf("day").isSame(futureDate.startOf("day"))
|
||||
);
|
||||
|
||||
if (!isFutureDateDisabled) {
|
||||
nearestDate = futureDate;
|
||||
setStartDate(futureDate);
|
||||
setDate(futureDateTimestamp);
|
||||
setAutoSelected(true);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}, [loading, disabledDates, autoSelected, setDate]);
|
||||
|
||||
return (
|
||||
<div className="datePickerApt flex items-start custom-datepicker justify-evenly w-full">
|
||||
<FirstDatePicker
|
||||
|
||||
Reference in New Issue
Block a user