From 791affcca6ba0579d4ee428886404f3eb8c3f63d Mon Sep 17 00:00:00 2001 From: Dobromir Popov Date: Thu, 26 Sep 2024 11:48:39 +0300 Subject: [PATCH] fix week view range when month transitioning --- components/publisher/PublisherShiftsModal.js | 30 ++++++++++++++------ 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/components/publisher/PublisherShiftsModal.js b/components/publisher/PublisherShiftsModal.js index d8d1bd7..f00a4e4 100644 --- a/components/publisher/PublisherShiftsModal.js +++ b/components/publisher/PublisherShiftsModal.js @@ -1,17 +1,22 @@ -import React, { useEffect } from 'react'; +import React, { useEffect, useState } from 'react'; import Link from 'next/link'; import common from 'src/helpers/common'; import axiosInstance from 'src/axiosSecure'; const PublisherShiftsModal = ({ publisher, _shifts, onClose, date, onAssignmentChange }) => { - const [shifts, setShifts] = React.useState([..._shifts]); + const [shifts, setShifts] = useState([..._shifts]); + const [assignments, setAssignments] = useState(publisher.assignments || []); //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)); + let monthInfo = common.getMonthDatesInfo(new Date(date)); + //if date is before monthInfo.firstMonday, get the previous month indexOf + if (date < monthInfo.firstMonday) { + monthInfo = common.getMonthDatesInfo(new Date(date.getFullYear(), date.getMonth() - 1, 1)); + } const monthShifts = shifts.filter(shift => { const shiftDate = new Date(shift.startTime); - return shiftDate > monthInfo.firstDay && shiftDate < monthInfo.lastDay; + return shiftDate > monthInfo.firstDay && shiftDate < monthInfo.lastSunday; }); const weekShifts = monthShifts.filter(shift => { const shiftDate = new Date(shift.startTime); @@ -38,11 +43,13 @@ const PublisherShiftsModal = ({ publisher, _shifts, onClose, date, onAssignmentC 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; - }); + + return assignments.some(ass => ass.shift.id === 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; + // }); }; @@ -163,6 +170,8 @@ const addAssignment = async (publisher, shiftId) => { data.shift = shifts.find(shift => shift.id === shiftId); publisher.assignments = [...publisher.assignments, data]; // handleAssignmentChange(publisher.id, 'add'); + + setAssignments(prevAssignments => [...prevAssignments, data]); if (onAssignmentChange) { onAssignmentChange(publisher.id, 'add'); } } catch (error) { console.error("Error adding assignment:", error); @@ -175,6 +184,9 @@ const removeAssignment = async (publisher, shiftId) => { const { data } = await axiosInstance.delete(`/api/data/assignments/${assignment.id}`); //remove from local assignments: publisher.assignments = publisher.assignments.filter(a => a.id !== assignment.id) + // Update local state + setAssignments(prevAssignments => prevAssignments.filter(a => a.id !== assignment.id)); + // // handleAssignmentChange(publisher.id, 'remove') if (onAssignmentChange) {