fix calendar popup
This commit is contained in:
@ -10,6 +10,8 @@ import { bgBG } from '../x-date-pickers/locales/bgBG';
|
||||
import { ToastContainer } from 'react-toastify';
|
||||
const common = require('src/helpers/common');
|
||||
//todo import Availability type from prisma schema
|
||||
import { isBefore, addMinutes, isAfter, isEqual, set, getHours, getMinutes, getSeconds } from 'date-fns';
|
||||
|
||||
|
||||
|
||||
const fetchConfig = async () => {
|
||||
@ -183,59 +185,6 @@ export default function AvailabilityForm({ publisherId, existingItems, inline, o
|
||||
return groupedIntervals;
|
||||
}
|
||||
|
||||
// // const firstSlotWithTransport = timeSlots[0].checked && timeSlots[0]?.isWithTransport;
|
||||
// // const lastSlotWithTransport = timeSlots[timeSlots.length - 1].checked && timeSlots[timeSlots.length - 1]?.isWithTransport;
|
||||
// function createAvailabilityFromGroup(group) {
|
||||
// let startTime = new Date(day);
|
||||
// startTime.setHours(group[0].startTime.getHours(), group[0].startTime.getMinutes(), group[0].startTime.getSeconds(), 0);
|
||||
|
||||
// let endTime = new Date(day);
|
||||
// endTime.setHours(group[group.length - 1].endTime.getHours(), group[group.length - 1].endTime.getMinutes(), group[group.length - 1].endTime.getSeconds(), 0);
|
||||
|
||||
|
||||
// return {
|
||||
// name: common.getTimeFomatted(startTime) + "-" + common.getTimeFomatted(endTime),
|
||||
// publisherId: publisher.id,
|
||||
// startTime: startTime,
|
||||
// endTime: endTime,
|
||||
// isWithTransportIn: group[0].isFirst && timeSlots[0].isWithTransport,
|
||||
// isWithTransportOut: group[group.length - 1].isLast && timeSlots[timeSlots.length - 1].isWithTransport,
|
||||
// dayofweek: common.getDayOfWeekNameEnEnumForDate(day.getDay()),
|
||||
// repeatWeekly: doRepeat,
|
||||
// dayOfMonth: doRepeat ? null : startTime.getDate(),
|
||||
// endDate: doRepeat ? repeatUntil : null,
|
||||
// dateOfEntry: new Date(),
|
||||
// };
|
||||
// }
|
||||
|
||||
// function updateAvailabilityFromGroup(availability, group) {
|
||||
// availability.startTime.setTime(group[0].startTime);
|
||||
// availability.endTime.setTime(group[group.length - 1].endTime);
|
||||
// availability.name = common.getTimeFomatted(availability.startTime) + "-" + common.getTimeFomatted(availability.endTime);
|
||||
|
||||
// availability.isWithTransportIn = group[0].isFirst && timeSlots[0].isWithTransport;
|
||||
// availability.isWithTransportOut = group[group.length - 1].isLast && timeSlots[timeSlots.length - 1].isWithTransport;
|
||||
|
||||
// delete availability.weekOfMonth;
|
||||
// if (doRepeat) {
|
||||
// availability.repeatWeekly = true;
|
||||
// availability.dayOfMonth = null;
|
||||
// availability.weekOfMonth = 0;
|
||||
// availability.endDate = repeatUntil;
|
||||
// } else {
|
||||
// availability.repeatWeekly = false;
|
||||
// availability.dayOfMonth = availability.startTime.getDate();
|
||||
// availability.endDate = null;
|
||||
// }
|
||||
|
||||
// availability.dateOfEntry = new Date();
|
||||
// if (availability.parentAvailabilityId) {
|
||||
// availability.parentAvailability = { connect: { id: parentAvailabilityId } };
|
||||
// }
|
||||
// delete availability.parentAvailabilityId;
|
||||
|
||||
// return availability;
|
||||
// }
|
||||
// Common function to set shared properties
|
||||
function setSharedAvailabilityProperties(availability, group, timeSlots) {
|
||||
let startTime = new Date(availability.startTime || day);
|
||||
@ -332,22 +281,25 @@ export default function AvailabilityForm({ publisherId, existingItems, inline, o
|
||||
}
|
||||
|
||||
// console.log("AvailabilityForm: publisherId: " + publisher.id + ", id: " + availabilit .id, ", inline: " + isInline);
|
||||
|
||||
//ToDo: this is examplary function to be used in the future. replace all date/time related functions with this one
|
||||
const generateTimeSlots = (start, end, increment, items) => {
|
||||
const slots = [];
|
||||
let currentTime = start.getTime();
|
||||
let currentTime = start;
|
||||
|
||||
const endTime = end.getTime();
|
||||
const baseDate = new Date(2000, 0, 1); // Use a constant date for all time comparisons
|
||||
|
||||
while (currentTime < endTime) {
|
||||
let slotStart = new Date(currentTime);
|
||||
let slotEnd = new Date(currentTime + increment * 60000); // increment is in minutes
|
||||
while (isBefore(currentTime, end)) {
|
||||
let slotStart = normalizeTime(currentTime, baseDate);
|
||||
let slotEnd = normalizeTime(addMinutes(currentTime, increment), baseDate);
|
||||
|
||||
const isChecked = items.some(item =>
|
||||
item.startTime && item.endTime &&
|
||||
(slotStart.getTime() < item.endTime.getTime()) &&
|
||||
(slotEnd.getTime() > item.startTime.getTime())
|
||||
);
|
||||
const isChecked = items.some(item => {
|
||||
let itemStart = item.startTime ? normalizeTime(new Date(item.startTime), baseDate) : null;
|
||||
let itemEnd = item.endTime ? normalizeTime(new Date(item.endTime), baseDate) : null;
|
||||
|
||||
return itemStart && itemEnd &&
|
||||
(slotStart.getTime() <= itemEnd.getTime()) &&
|
||||
(slotEnd.getTime() >= itemStart.getTime());
|
||||
});
|
||||
|
||||
slots.push({
|
||||
startTime: slotStart,
|
||||
@ -355,10 +307,9 @@ export default function AvailabilityForm({ publisherId, existingItems, inline, o
|
||||
isChecked: isChecked,
|
||||
});
|
||||
|
||||
currentTime += increment * 60000; // Increment in milliseconds (minutes to milliseconds)
|
||||
currentTime = addMinutes(currentTime, increment);
|
||||
}
|
||||
|
||||
// Optional: Add isFirst, isLast, and isWithTransport properties
|
||||
if (slots.length > 0 && items?.length > 0) {
|
||||
slots[0].isFirst = true;
|
||||
slots[slots.length - 1].isLast = true;
|
||||
@ -369,6 +320,16 @@ export default function AvailabilityForm({ publisherId, existingItems, inline, o
|
||||
return slots;
|
||||
};
|
||||
|
||||
// Normalize the time part of a date by using a base date
|
||||
function normalizeTime(date, baseDate) {
|
||||
return set(baseDate, {
|
||||
hours: getHours(date),
|
||||
minutes: getMinutes(date),
|
||||
seconds: getSeconds(date),
|
||||
milliseconds: 0
|
||||
});
|
||||
}
|
||||
|
||||
const TimeSlotCheckboxes = ({ slots, setSlots, items: [] }) => {
|
||||
const [allDay, setAllDay] = useState(slots.every(slot => slot.isChecked));
|
||||
const handleAllDayChange = (e) => {
|
||||
|
Reference in New Issue
Block a user