fix week view range when month transitioning

This commit is contained in:
Dobromir Popov
2024-09-26 11:48:39 +03:00
parent 1ca2b51e4b
commit 791affcca6

View File

@ -1,17 +1,22 @@
import React, { useEffect } from 'react'; import React, { useEffect, useState } from 'react';
import Link from 'next/link'; import Link from 'next/link';
import common from 'src/helpers/common'; import common from 'src/helpers/common';
import axiosInstance from 'src/axiosSecure'; import axiosInstance from 'src/axiosSecure';
const PublisherShiftsModal = ({ publisher, _shifts, onClose, date, onAssignmentChange }) => { 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 //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 monthShifts = shifts.filter(shift => {
const shiftDate = new Date(shift.startTime); 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 weekShifts = monthShifts.filter(shift => {
const shiftDate = new Date(shift.startTime); const shiftDate = new Date(shift.startTime);
@ -38,11 +43,13 @@ const PublisherShiftsModal = ({ publisher, _shifts, onClose, date, onAssignmentC
console.log("dayShifts:", dayShifts); console.log("dayShifts:", dayShifts);
const hasAssignment = (shiftId) => { const hasAssignment = (shiftId) => {
// return publisher.assignments.some(ass => ass.shift.id == shiftId);
return publisher.assignments?.some(ass => { return assignments.some(ass => ass.shift.id === shiftId);
//console.log(`Comparing: ${ass.shift.id} to ${shiftId}: ${ass.shift.id === shiftId}`); // // return publisher.assignments.some(ass => ass.shift.id == shiftId);
return 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); data.shift = shifts.find(shift => shift.id === shiftId);
publisher.assignments = [...publisher.assignments, data]; publisher.assignments = [...publisher.assignments, data];
// handleAssignmentChange(publisher.id, 'add'); // handleAssignmentChange(publisher.id, 'add');
setAssignments(prevAssignments => [...prevAssignments, data]);
if (onAssignmentChange) { onAssignmentChange(publisher.id, 'add'); } if (onAssignmentChange) { onAssignmentChange(publisher.id, 'add'); }
} catch (error) { } catch (error) {
console.error("Error adding assignment:", 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}`); const { data } = await axiosInstance.delete(`/api/data/assignments/${assignment.id}`);
//remove from local assignments: //remove from local assignments:
publisher.assignments = publisher.assignments.filter(a => a.id !== assignment.id) 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') // handleAssignmentChange(publisher.id, 'remove')
if (onAssignmentChange) { if (onAssignmentChange) {