import React, { useState, useEffect } from "react"; import "./ImagePop.css"; import { useModal } from "./ModalContext"; import moment from "moment"; const ImagePop = ({ batch, initialIndex = 0 }) => { const { closeModal } = useModal(); const [currentIndex, setCurrentIndex] = useState(initialIndex); // Effect to update currentIndex if the initialIndex prop changes useEffect(() => { setCurrentIndex(initialIndex); }, [initialIndex, batch]); // If no batch or documents are provided, don't render if (!batch || !batch.documents || batch.documents.length === 0) return null; // Get the current image document from the batch's documents array const image = batch.documents[currentIndex]; // Fallback if for some reason the image at the current index doesn't exist if (!image) return null; // Format details for display from the individual image document const fullName = `${image.uploadedBy?.firstName || ""} ${ image.uploadedBy?.lastName || "" }`.trim(); const date = moment(image.uploadedAt).format("YYYY-MM-DD"); const time = moment(image.uploadedAt).format("hh:mm A"); // Location and category details from the 'batch' object (as previously corrected) const buildingName = batch.buildingName; const floorName = batch.floorName; const workAreaName = batch.workAreaName; const activityName = batch.activityName; const batchComment = batch.comment; // Handler for navigating to the previous image const handlePrev = () => { setCurrentIndex((prevIndex) => Math.max(0, prevIndex - 1)); }; // Handler for navigating to the next image const handleNext = () => { setCurrentIndex((prevIndex) => Math.min(batch.documents.length - 1, prevIndex + 1) ); }; // Determine if previous/next buttons should be enabled/visible const hasPrev = currentIndex > 0; const hasNext = currentIndex < batch.documents.length - 1; return (
{/* Close button */} {/* Previous button, only shown if there's a previous image */} {hasPrev && ( )} {/* The main image display */} Preview {/* Next button, only shown if there's a next image */} {hasNext && ( )} {/* Image details */}

👤 Uploaded By: {fullName}

📅 Date: {date} {time}

🏢 Location: {buildingName} > {floorName} >{" "} {workAreaName || "Unknown"} > {activityName}

{/* Display the comment from the batch object */} 📝 Comments: {batchComment || "N/A"}

); }; export default ImagePop;