import dayjs from 'dayjs';
import { useContext, useEffect, useState } from 'react';
import CalendarContext from '../../Providers/CalendarContext';
import { getShifts } from '../../api/shifts';
const Day2 = ({ day }) => {
const [dayEvents, setDayEvents] = useState([])
const { setEventModal, setDaySelected, setEvents, savedEvents, setSelectedEvent, selectedEvent } = useContext(CalendarContext)
const [shifts, setShifts] = useState([])
useEffect(() => {
getShifts()
.then(data => {
organizeShiftsByName(data)
})
}, [])
const organizeShiftsByName = (shifts) => {
const groupedShifts = shifts.reduce((acc, shift) => {
if (!acc[shift.name]) {
acc[shift.name] = [];
}
acc[shift.name].push(shift);
return acc;
}, {});
setShifts(groupedShifts);
};
console.log(shifts)
useEffect(() => {
if (day) {
const allShifts = Object.values(shifts).flat();
const events = allShifts.filter(evt => dayjs(evt.day).format("DD-MM-YY") === day.format("DD-MM-YY"))
setDayEvents(events)
}
}, [shifts, day])
if (!day) {
return; // or any placeholder content
}
return (
<div className="border grid justify-center item-center cursor-pointer" onClick={() => {
setEventModal(true);
setDaySelected(day);
}}
>
<p className='text-xs'>{day?.format("DD")}</p>
<p className='text-xs'>{day.format("ddd").toUpperCase()}</p>
<div className="flex-1 cursor-pointer">
{dayEvents.map((evt, idx) => (
<div key={idx} onClick={() => { setSelectedEvent(evt) }} className={`${evt.label} p-1 mr-1 text-gray flex text-xs rounded mb-1 truncate`}>
{evt.name}
</div>
))}
</div>
</div>
);
};
export default Day2;