diff --git a/components/calendar/ShiftComponent.tsx b/components/calendar/ShiftComponent.tsx index a50db70..1d694dd 100644 --- a/components/calendar/ShiftComponent.tsx +++ b/components/calendar/ShiftComponent.tsx @@ -10,7 +10,7 @@ const common = require('src/helpers/common'); function ShiftComponent({ shift, onShiftSelect, isSelected, onPublisherSelect, allPublishersInfo }) { - + const [isDeleted, setIsDeleted] = useState(false); const [assignments, setAssignments] = useState(shift.assignments); const [isModalOpen, setIsModalOpen] = useState(false); const [useFilterDate, setUseFilterDate] = useState(true); @@ -24,24 +24,14 @@ function ShiftComponent({ shift, onShiftSelect, isSelected, onPublisherSelect, a }, [shift.assignments]); const handleShiftClick = (shiftId) => { - // console.log("onShiftSelect prop:", onShiftSelect); - // console.log("Shift clicked:", shift); - //shift.selectedPublisher = selectedPublisher; if (onShiftSelect) { onShiftSelect(shift); } }; const handlePublisherClick = (publisher) => { - - //toggle selected - // if (selectedPublisher != null) { - // setSelectedPublisher(null); - // } - // else { setSelectedPublisher(publisher); - console.log("Publisher clicked:", publisher, "selected publisher:", selectedPublisher); shift.selectedPublisher = publisher; if (onShiftSelect) { @@ -54,6 +44,17 @@ function ShiftComponent({ shift, onShiftSelect, isSelected, onPublisherSelect, a common.copyToClipboard(null, publisher.firstName + ' ' + publisher.lastName); } + const deleteShift = async (id) => { + try { + console.log("Removing shift with id:", id); + await axiosInstance.delete("/api/data/shifts/" + id); + setIsDeleted(true); + } catch (error) { + console.error("Error removing shift:", error); + } + } + + const removeAssignment = async (id) => { try { console.log("Removing assignment with id:", id); @@ -100,137 +101,162 @@ function ShiftComponent({ shift, onShiftSelect, isSelected, onPublisherSelect, a } catch (error) { } } + async function toggleRequiresTransport(shiftId): Promise { + try { + shift.requiresTransport = !shift.requiresTransport; + const { data } = await axiosInstance.put("/api/data/shifts/" + shiftId, + { requiresTransport: shift.requiresTransport }) + .then(() => { + console.log("shift '" + shiftId + "' transport required:" + shift.requiresTransport); + // setTransportProvided(assignments.some(ass => ass.isWithTransport)) + }); + } catch (error) { } + } + return ( -
- {/* Time Window Header */} -
- - {`${common.getTimeRange(new Date(shift.startTime), new Date(shift.endTime))}`} - {/* {shift.requiresTransport && ()} */} - + <>{!isDeleted && ( +
+ {/* Time Window Header */} +
+ + {`${common.getTimeRange(new Date(shift.startTime), new Date(shift.endTime))}`} + {/* {shift.requiresTransport && ()} */} + {/* Toggle for Transport Requirement */} + + - {/* Copy All Names Button */} - - {/* Hint Message */} - {showCopyHint && ( -
- Имената са копирани -
- )} -
+ {/* Copy All Names Button */} + + {/* Hint Message */} + {showCopyHint && ( +
+ Имената са копирани +
+ )} +
- {/* Assignments */} - {assignments.map((ass, index) => { - const publisherInfo = allPublishersInfo.find(info => info?.id === ass.publisher.id) || ass.publisher; + {/* Assignments */} + {assignments.map((ass, index) => { + const publisherInfo = allPublishersInfo.find(info => info?.id === ass.publisher.id) || ass.publisher; - // Determine border styles - let borderStyles = ''; - let canTransport = false; - if (selectedPublisher && selectedPublisher.id === ass.publisher.id) { - borderStyles += 'border-2 border-blue-300'; // Bottom border for selected publishers - } - else { - if (publisherInfo.availabilityCount == 0) //user has never the form - { - borderStyles = 'border-2 border-orange-300 '; + // Determine border styles + let borderStyles = ''; + let canTransport = false; + if (selectedPublisher && selectedPublisher.id === ass.publisher.id) { + borderStyles += 'border-2 border-blue-300'; // Bottom border for selected publishers } - else - //if there is no publisherInfo - draw red border - publisher is no longer available for the day! - if (!publisherInfo.availabilities || publisherInfo.availabilities.length == 0) { - borderStyles = 'border-2 border-red-500 '; + else { + if (publisherInfo.availabilityCount == 0) //user has never the form + { + borderStyles = 'border-2 border-orange-300 '; } - else { + else + //if there is no publisherInfo - draw red border - publisher is no longer available for the day! + if (!publisherInfo.availabilities || publisherInfo.availabilities.length == 0) { + borderStyles = 'border-2 border-red-500 '; + } + else { + + // checkig if the publisher is available for this assignment + const av = publisherInfo.availabilities?.find(av => + av.startTime <= shift.startTime && av.endTime >= shift.endTime + ); + if (av) { + borderStyles += 'border-l-2 border-blue-500 '; // Left border for specific availability conditions + ass.canTransport = av.isWithTransportIn || av.isWithTransportOut; + } + + if (publisherInfo.hasUpToDateAvailabilities) { + //add green right border + borderStyles += 'border-r-2 border-green-300'; + } + + //the pub is the same time as last month + // if (publisherInfo.availabilities?.some(av => + // (!av.dayOfMonth || av.isFromPreviousMonth) && + // av.startTime <= ass.startTime && + // av.endTime >= ass.endTime)) { + // borderStyles += 'border-t-2 border-yellow-500 '; // Left border for specific availability conditions + // } - // checkig if the publisher is available for this assignment - const av = publisherInfo.availabilities?.find(av => - av.startTime <= shift.startTime && av.endTime >= shift.endTime - ); - if (av) { - borderStyles += 'border-l-2 border-blue-500 '; // Left border for specific availability conditions - ass.canTransport = av.isWithTransportIn || av.isWithTransportOut; } - if (publisherInfo.hasUpToDateAvailabilities) { - //add green right border - borderStyles += 'border-r-2 border-green-300'; - } + } - //the pub is the same time as last month - // if (publisherInfo.availabilities?.some(av => - // (!av.dayOfMonth || av.isFromPreviousMonth) && - // av.startTime <= ass.startTime && - // av.endTime >= ass.endTime)) { - // borderStyles += 'border-t-2 border-yellow-500 '; // Left border for specific availability conditions - // } + return ( +
+
handlePublisherClick(ass.publisher)}> + {publisherInfo.firstName} {publisherInfo.lastName} +
+ {/* //if shift.isWithTransport, add trnsport button toggle, which sets ass.isWithTransportIn */} + {shift.requiresTransport && ( + toggleTransport(ass) : undefined} + className={`material-icons ${ass.isWithTransport ? 'text-green-500 font-bold' : (transportProvided ? 'text-gray-400 ' : 'text-orange-400 font-bold')} ${ass.canTransport ? ' cursor-pointer' : 'cursor-not-allowed'} px-3 py-1 ml-2 rounded-md`} + > + {ass.isWithTransport ? "транспорт" : ass.canTransport ? "може транспорт" : "без транспорт"} + + )} + - } - - } - - return ( -
-
handlePublisherClick(ass.publisher)}> - {publisherInfo.firstName} {publisherInfo.lastName} -
- {/* //if shift.isWithTransport, add trnsport button toggle, which sets ass.isWithTransportIn */} - {shift.requiresTransport && ( - toggleTransport(ass) : undefined} - className={`material-icons ${ass.isWithTransport ? 'text-green-500 font-bold' : (transportProvided ? 'text-gray-400 ' : 'text-orange-400 font-bold')} ${ass.canTransport ? ' cursor-pointer' : 'cursor-not-allowed'} px-3 py-1 ml-2 rounded-md`} - > - {ass.isWithTransport ? "транспорт" : ass.canTransport ? "може транспорт" : "без транспорт"} - - )} - +
-
-
- ); - })} + ); + })} - {/* This is a placeholder for the dropdown to add a publisher. You'll need to implement or integrate a dropdown component */} + {/* This is a placeholder for the dropdown to add a publisher. You'll need to implement or integrate a dropdown component */} -
- {/* Add Button */} - -
+
+ {/* Add Button */} + + {assignments.length == 0 && ( + + )} +
- {/* Modal for Publisher Search + {/* Modal for Publisher Search forDate={new Date(shift.startTime)} */} - setIsModalOpen(false)} - forDate={new Date(shift.startTime)} - useFilterDate={useFilterDate} - onUseFilterDateChange={(value) => setUseFilterDate(value)}> + setIsModalOpen(false)} + forDate={new Date(shift.startTime)} + useFilterDate={useFilterDate} + onUseFilterDateChange={(value) => setUseFilterDate(value)}> - { - // Add publisher as assignment logic - setIsModalOpen(false); - addAssignment(publisher, shift.id); - }} - showAllAuto={true} - showSearch={true} - showList={false} - /> - -
+ { + // Add publisher as assignment logic + setIsModalOpen(false); + addAssignment(publisher, shift.id); + }} + showAllAuto={true} + showSearch={true} + showList={false} + /> + +
+ )} ); } diff --git a/pages/cart/calendar/index.tsx b/pages/cart/calendar/index.tsx index 6bdacb6..6ad9127 100644 --- a/pages/cart/calendar/index.tsx +++ b/pages/cart/calendar/index.tsx @@ -140,6 +140,7 @@ export default function CalendarPage({ initialEvents, initialShifts }) { onChange(selectedDate); } } + const handleShiftSelection = (selectedShift) => { setSelectedShiftId(selectedShift.id); const updatedPubs = availablePubs.map(pub => { @@ -535,6 +536,38 @@ export default function CalendarPage({ initialEvents, initialShifts }) { await axiosInstance.get(`/api/?action=copyOldAvailabilities&date=${common.getISODateOnly(value)}`); } + async function handleCreateNewShift(event: MouseEvent): Promise { + //get last shift end time + let lastShift = shifts.sort((a, b) => new Date(b.endTime).getTime() - new Date(a.endTime).getTime())[0]; + //default to 9:00 if no shifts + if (!lastShift) { + //get cart event id + var dayName = common.DaysOfWeekArray[value.getDayEuropean()]; + const cartEvent = events.find(event => event.dayofweek == dayName); + lastShift = { + endTime: new Date(value.setHours(9, 0, 0, 0)), + cartEventId: cartEvent.id + }; + } + const lastShiftEndTime = new Date(lastShift.endTime); + //add 90 minutes + const newShiftEndTime = new Date(lastShiftEndTime.getTime() + 90 * 60000); + await axiosInstance.post(`/api/data/shifts`, { + name: "Нова смяна", + startTime: lastShiftEndTime, + endTime: newShiftEndTime, + isPublished: false, + cartEvent: { connect: { id: lastShift.cartEventId } } + }).then((response) => { + console.log("New shift created:", response.data); + // setShifts([...shifts, response.data]); + handleCalDateChange(value); + } + ).catch((error) => { + console.error("Error creating new shift:", error); + }); + } + return ( <> @@ -621,22 +654,6 @@ export default function CalendarPage({ initialEvents, initialShifts }) {
)} - {/* - // - // - // - // - // - // - // - // - // - */} {/* progress bar holder */} @@ -737,6 +754,12 @@ export default function CalendarPage({ initialEvents, initialShifts }) { allPublishersInfo={availablePubs} /> ))} + @@ -907,13 +930,6 @@ export const getServerSideProps = async (context) => { const url = `/api/data/shifts?where={"startTime":{"$and":[{"$gte":"${common.getISODateOnly(firstDayOfMonth)}","$lt":"${common.getISODateOnly(lastDayOfMonth)}"}]}}`; const prismaClient = common.getPrismaClient(); - // let events = await prismaClient.cartEvent.findMany({ where: { isActive: true } }); - // events = events.map(event => ({ - // ...event, - // // Convert Date objects to ISO strings - // startTime: event.startTime.toISOString(), - // endTime: event.endTime.toISOString(), - // })); const { data: events } = await axios.get(`/api/data/cartevents?where={"isActive":true}`); //const { data: shifts } = await axios.get(url);