fix modal add/remove

This commit is contained in:
Dobromir Popov
2024-06-28 20:43:45 +03:00
parent 5dba87f3dd
commit 8b2cdc517e

View File

@ -145,4 +145,41 @@ function getColorForShift(shift) {
}
}
//ToDo: DRY - move to common
const addAssignment = async (publisher, shiftId) => {
try {
console.log(`calendar.idx: new assignment for publisher ${publisher.id} - ${publisher.firstName} ${publisher.lastName}`);
const newAssignment = {
publisher: { connect: { id: publisher.id } },
shift: { connect: { id: shiftId } },
isConfirmed: true
};
const { data } = await axiosInstance.post("/api/data/assignments", newAssignment);
if (selectedShiftId == shiftId) {
handleShiftSelection(shifts.find(shift => shift.id === shiftId));
}
// Update the 'publisher' property of the returned data with the full publisher object
data.publisher = publisher;
data.shift = shifts.find(shift => shift.id === shiftId);
publisher.assignments = [...publisher.assignments, data];
handleAssignmentChange(publisher.id, 'add');
} catch (error) {
console.error("Error adding assignment:", error);
}
};
const removeAssignment = async (publisher, shiftId) => {
try {
const assignment = publisher.assignments.find(ass => ass.shift.id === shiftId);
console.log(`calendar.idx: remove assignment for shift ${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)
//
handleAssignmentChange(publisher.id, 'remove')
} catch (error) {
console.error("Error removing assignment:", error);
}
}
export default PublisherShiftsModal;