update TimePicker slot logic based on check-in and check-out time conditions

This commit is contained in:
ashutosh.nehete 2025-05-04 12:20:16 +05:30
parent 292f80c9a8
commit d5cbe67d3e

View File

@ -1,6 +1,7 @@
import React, { useState, useRef, useEffect } from "react";
import {THRESH_HOLD} from "../../utils/constants"
const TimePicker = ({ label, onChange, interval = 10, value,checkInTime,activity }) => {
const TimePicker = ({ label, onChange, interval = 10, value,checkInTime,checkOutTime }) => {
const [time, setTime] = useState(value || "");
const [isOpen, setIsOpen] = useState(false);
const dropdownRef = useRef(null);
@ -41,59 +42,50 @@ const TimePicker = ({ label, onChange, interval = 10, value,checkInTime,activity
const checkInDate = checkInTime ? new Date(checkInTime) : null;
const fullStart = checkInDate ? new Date(checkInDate) : new Date();
fullStart.setHours(0, 0, 0, 0);
const fullEnd = checkInDate ? new Date(checkInDate) : new Date();
fullEnd.setHours(23, 59, 59, 999);
const checkOutDate = checkOutTime ? new Date(checkOutTime) : null;
const dayStart = new Date();
const dayEnd = new Date();
if (checkInDate) {
dayStart.setTime(checkInDate.getTime());
dayEnd.setTime(checkInDate.getTime());
}
dayStart.setHours(0, 0, 0, 0);
dayEnd.setHours(23, 59, 59, 999);
let minSelectable = null;
let maxSelectable = null;
// Activity 0: Time slots based on current time
if (activity === 0 && !checkInDate) {
minSelectable = new Date(currentSlot.getTime() - 60 * 60000); // one hour before current time
if (!checkInDate) {
// Case 1: No check-in time (new check-in)
minSelectable = new Date(dayStart);
maxSelectable = new Date(currentSlot);
}
// Activity 1: Time slots based on check-in time today
else if (activity === 1 && checkInDate) {
if (checkInDate.toDateString() === now.toDateString()) {
else if (checkInDate && !checkOutDate) {
const hoursDiff = (now - checkInDate) / (1000 * 60 * 60);
if (hoursDiff < THRESH_HOLD) {
// Case 2: Check-in present, no checkout, within THRESH_HOLD hours
minSelectable = new Date(checkInDate);
maxSelectable = new Date(currentSlot);
} else {
// Case 4: Check-in present, no checkout, more than THRESH_HOLD hours
minSelectable = new Date(checkInDate);
maxSelectable = new Date(dayEnd);
}
}
// Activity 2: Time slots from check-in time to 12:00 AM of the same day
else if (activity === 2 && checkInDate) {
// Set minSelectable to the exact check-in time
minSelectable = new Date(checkInDate);
// Set maxSelectable to midnight (12:00 AM) of the same day
maxSelectable = new Date(checkInDate);
maxSelectable.setHours(23, 59, 59, 999); // Set to 23:59:59.999 (end of the day)
} else if (checkInDate && checkOutDate) {
// Case 3: Both check-in and checkout present
minSelectable = new Date(checkOutDate);
maxSelectable = new Date(currentSlot);
}
// Loop through every slot in the day to check which ones are selectable
const slot = new Date(fullStart.getTime());
while (slot <= fullEnd) {
const slot = new Date(dayStart.getTime());
while (slot <= dayEnd) {
const formatted = formatTime(slot);
let isSelectable = false;
// For activity 2, allow only slots from the check-in time up to 12:00 AM of the same day
if (minSelectable && maxSelectable) {
if (activity === 2 && checkInDate) {
// Ensure slot is greater than or equal to minSelectable and less than or equal to maxSelectable
isSelectable = slot >= minSelectable && slot <= maxSelectable;
} else {
isSelectable = slot >= minSelectable && slot <= maxSelectable;
}
}
slots.push({
time: formatted,
isSelectable,
});
const isSelectable = slot >= minSelectable && slot <= maxSelectable;
slots.push({ time: formatted, isSelectable });
slot.setMinutes(slot.getMinutes() + interval);
}
@ -137,13 +129,6 @@ const TimePicker = ({ label, onChange, interval = 10, value,checkInTime,activity
}
}, [isOpen, time]);
useEffect(() => {
if (activity === 2 && checkInTime) {
const slots = generateTimeSlots();
const selectableSlots = slots.filter(slot => slot.isSelectable);
console.log("Selectable Slots for Activity 2:", selectableSlots);
}
}, [activity, checkInTime]);
return (
<div className="position-relative w-100" ref={dropdownRef}>
@ -205,4 +190,4 @@ const TimePicker = ({ label, onChange, interval = 10, value,checkInTime,activity
);
};
export default TimePicker;
export default TimePicker;