import React, { useEffect } from 'react'; import Link from 'next/link'; import common from 'src/helpers/common'; const PublisherShiftsModal = ({ publisher, shifts, onClose, date }) => { //Refactor ToDo: show the whole month instead of just the current week by showing the shift start time in front of the rows, and show all shifts in the month from the first to the last week in the cell where we show one shift now const monthInfo = common.getMonthDatesInfo(new Date(date)); const monthShifts = shifts.filter(shift => { const shiftDate = new Date(shift.startTime); return shiftDate > monthInfo.firstDay && shiftDate < monthInfo.lastDay; }); const weekShifts = monthShifts.filter(shift => { const shiftDate = new Date(shift.startTime); return common.getStartOfWeek(date) <= shiftDate && shiftDate <= common.getEndOfWeek(date); }); const dayShifts = weekShifts.map(shift => { const isAvailable = publisher?.availabilities?.some(avail => avail.startTime <= shift.startTime && avail.endTime >= shift.endTime ); let color = isAvailable ? getColorForShift(shift) : 'bg-gray-300'; if (shift.isFromPreviousMonth) { color += ' border-l-4 border-orange-500 '; } if (shift.isFromPreviousAssignment) { color += ' border-l-4 border-red-500 '; } return { ...shift, isAvailable, color }; }).reduce((acc, shift) => { const dayIndex = new Date(shift.startTime).getDay(); acc[dayIndex] = acc[dayIndex] || []; acc[dayIndex].push(shift); return acc; }, {}); console.log("dayShifts:", dayShifts); const hasAssignment = (shiftId) => { // return publisher.assignments.some(ass => ass.shift.id == shiftId); return publisher.assignments?.some(ass => { //console.log(`Comparing: ${ass.shift.id} to ${shiftId}: ${ass.shift.id === shiftId}`); return ass.shift.id === shiftId; }); }; useEffect(() => { const handleKeyDown = (event) => { if (event.key === 'Escape') { console.log('ESC: closing modal.'); onClose(); // Call the onClose function when ESC key is pressed } }; // Add event listener window.addEventListener('keydown', handleKeyDown); // Remove event listener on cleanup return () => { window.removeEventListener('keydown', handleKeyDown); }; }, [onClose]); // Include onClose in the dependency array return (