fix calendar popup

This commit is contained in:
Dobromir Popov
2024-04-14 02:22:27 +03:00
parent e09d960b0f
commit 4fb8022c46
5 changed files with 70 additions and 113 deletions

View File

@ -16,7 +16,12 @@ import { MdToday } from 'react-icons/md';
import { useSwipeable } from 'react-swipeable';
import axiosInstance from '../../src/axiosSecure';
import { set } from 'date-fns';
// import { set, format, addDays } from 'date-fns';
import { isEqual, isSameDay, getHours, getMinutes } from 'date-fns';
import { filter } from 'jszip';
// Set moment to use the Bulgarian locale
moment.locale('bg');
@ -162,6 +167,31 @@ const AvCalendar = ({ publisherId, events, selectedDate }) => {
occurrences.push(occurrence);
}
};
const filterEvents = (evts, publisherId, startdate) => {
setDate(startdate); // Assuming setDate is a function that sets some state or context
// Filter events based on the publisher ID and the start date/time
const existingEvents = evts?.filter(event => {
// Ensure the event belongs to the specified publisher
const isPublisherMatch = (event.publisher?.id || event.publisherId) === publisherId;
let isDateMatch;
if (event.repeatWeekly && event.date) {
// Compare only the time part
const eventDate = new Date(event.startTime);
const isSameHour = getHours(eventDate) === getHours(startdate);
const isSameMinute = getMinutes(eventDate) === getMinutes(startdate);
isDateMatch = isSameHour && isSameMinute;
} else if (event.date) {
// Compare the full date
isDateMatch = isSameDay(new Date(event.date), startdate);
}
return isPublisherMatch && isDateMatch;
});
return existingEvents;
};
// Define min and max times
const minHour = 8; // 8:00 AM
@ -177,7 +207,8 @@ const AvCalendar = ({ publisherId, events, selectedDate }) => {
const enddate = typeof end === 'string' ? new Date(end) : end;
if (!start || !end) return;
if (startdate < new Date() || end < new Date() || startdate > end) return;
//readonly for past dates (ToDo: if not admin)
//if (startdate < new Date() || end < new Date() || startdate > end) return;
// Check if start and end are on the same day
if (startdate.toDateString() !== enddate.toDateString()) {
@ -198,52 +229,15 @@ const AvCalendar = ({ publisherId, events, selectedDate }) => {
setDate(start);
// get exising events for the selected date
const existingEvents = evts?.filter(event => (event.publisher?.id || event.publisherId) === publisherId && new Date(event.date).toDateString() === startdate.toDateString());
// const existingEvents = evts?.filter(event => {
// return event.publisherId === publisherId &&
// new Date(event.startTime).getFullYear() === start.getFullYear() &&
// new Date(event.startTime).getMonth() === start.getMonth() &&
// new Date(event.startTime).getDate() === start.getDate();
// });
const existingEvents = filterEvents(evts, publisherId, startdate);
console.log("handleSelect: " + existingEvents);
setSelectedEvents(existingEvents);
// setSelectedEvent({
// date: start,
// startTime: start,
// endTime: end,
// dayOfMonth: start.getDate(),
// isActive: true,
// publisherId: publisherId,
// // Add any other initial values needed
// //set dayOfMonth to null, so that we repeat the availability every week
// dayOfMonth: null,
// });
setIsModalOpen(true);
setIsModalOpen(true);
};
const handleEventClick = (event) => {
if (event.type === "assignment") return;
handleSelect({ start: event.startTime, end: event.endTime });
// Handle event click
// const eventForEditing = {
// ...event,
// startTime: new Date(event.startTime),
// endTime: new Date(event.endTime),
// publisherId: event.publisherId || event.publisher?.connect?.id,
// repeatWeekly: event.repeatWeekly || false,
// };
// //strip title, start, end and allDay properties
// delete eventForEditing.title;
// delete eventForEditing.start;
// delete eventForEditing.end;
// delete eventForEditing.type;
// delete eventForEditing.publisher
// console.log("handleEventClick: " + eventForEditing);
// setSelectedEvents([eventForEditing]);
// setIsModalOpen(true);
};
const handleDialogClose = async (dialogEvent) => {
@ -256,10 +250,8 @@ const AvCalendar = ({ publisherId, events, selectedDate }) => {
newEvents.forEach(event => {
event.startTime = new Date(event.startTime);
event.endTime = new Date(event.endTime);
});
setEvents(newEvents);
}
console.log("handleSave: ", dialogEvent);