diff --git a/components/publisher/PublisherShiftsModal.js b/components/publisher/PublisherShiftsModal.js index 9b61cff..04968ff 100644 --- a/components/publisher/PublisherShiftsModal.js +++ b/components/publisher/PublisherShiftsModal.js @@ -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; \ No newline at end of file