import React, { useState, useEffect } from 'react'; import axiosInstance from '../../src/axiosSecure'; import PublisherSearchBox from '../publisher/PublisherSearchBox'; // Update the path const common = require('src/helpers/common'); interface ModalProps { children: React.ReactNode; isOpen: boolean; onClose: () => void; forDate: Date; useFilterDate: boolean; onUseFilterDateChange: (value: boolean) => void; } function Modal({ children, isOpen, onClose, forDate, useFilterDate, onUseFilterDateChange }: ModalProps) { if (!isOpen) return null; const isValidDate = forDate instanceof Date && !isNaN(forDate.getTime()); console.log("forDate", forDate, isValidDate); return (
{isValidDate && (

)} {children}
); } function ShiftComponent({ shift, onShiftSelect, isSelected, onPublisherSelect, allPublishersInfo }) { const [assignments, setAssignments] = useState(shift.assignments); const [isModalOpen, setIsModalOpen] = useState(false); const [useFilterDate, setUseFilterDate] = useState(true); const [selectedPublisher, setSelectedPublisher] = useState(null); const [showCopyHint, setShowCopyHint] = useState(false); // Update assignments when shift changes useEffect(() => { setAssignments(shift.assignments); }, [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) { onShiftSelect(shift); } // if (onPublisherSelect) { // onPublisherSelect(publisher); // } } const removeAssignment = async (id) => { try { console.log("Removing assignment with id:", id); await axiosInstance.delete("/api/data/assignments/" + id); setAssignments(prevAssignments => prevAssignments.filter(ass => ass.id !== id)); } catch (error) { console.error("Error removing assignment:", error); } }; const addAssignment = async (publisher, shiftId) => { try { console.log(`new assignment for publisher ${publisher.id} - ${publisher.firstName} ${publisher.lastName}`); const newAssignment = { publisher: { connect: { id: publisher.id } }, shift: { connect: { id: shiftId } }, isactive: true, isConfirmed: true }; const { data } = await axiosInstance.post("/api/data/assignments", newAssignment); // Update the 'publisher' property of the returned data with the full publisher object data.publisher = publisher; setAssignments(prevAssignments => [...prevAssignments, data]); } catch (error) { console.error("Error adding assignment:", error); } }; const copyAllPublisherNames = () => { const names = assignments.map(ass => `${ass.publisher.firstName} ${ass.publisher.lastName}`).join(', '); common.copyToClipboard(null, names); // Show hint and set a timer to hide it setShowCopyHint(true); setTimeout(() => setShowCopyHint(false), 1500); }; return (
{/* Time Window Header */}
{`${common.getTimeRange(new Date(shift.startTime), new Date(shift.endTime))}`} {/* Copy All Names Button */} {/* Hint Message */} {showCopyHint && (
Имената са копирани
)}
{/* Assignments */} {assignments.map((ass, index) => { const publisherInfo = allPublishersInfo.find(info => info?.id === ass.publisher.id) || ass.publisher; // Determine border styles let borderStyles = ''; //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 { //pub is not available for that shift assignment. if (publisherInfo.availabilities?.length === 0 || publisherInfo.availabilities?.every(avail => avail.isFromPreviousAssignment)) { borderStyles += 'border-l-3 border-r-3 border-orange-500 '; // Top border for manual publishers } // checkig if the publisher is available for this assignment if (publisherInfo.availabilities?.some(av => av.startTime <= ass.startTime && av.endTime >= ass.endTime)) { borderStyles += 'border-t-2 border-red-500 '; // Left border for specific availability conditions } //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 // } if (selectedPublisher && selectedPublisher.id === ass.publisher.id) { borderStyles += 'border-2 border-blue-300'; // Bottom border for selected publishers } if (publisherInfo.hasUpToDateAvailabilities) { //add green right border borderStyles += 'border-r-2 border-green-300'; } } return (
handlePublisherClick(ass.publisher)}> {publisherInfo.firstName} {publisherInfo.lastName}
); })} {/* This is a placeholder for the dropdown to add a publisher. You'll need to implement or integrate a dropdown component */}
{/* Add Button */}
{/* Modal for Publisher Search forDate={new Date(shift.startTime)} */} 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} />
); } export default ShiftComponent;