import React, { useState, useEffect } from "react"; import { Dialog, DialogTitle, DialogContent, DialogActions, Button, Table, TableBody, TableCell, TableContainer, TableRow, IconButton, Paper, TextField, Box } from "@mui/material"; import ContentCopyIcon from "@mui/icons-material/ContentCopy"; import CloseIcon from "@mui/icons-material/Close"; import EditIcon from "@mui/icons-material/Edit"; import { Images } from "../../Types/Images"; import ApiService from '../../services/ApiService'; type Props = { galleryOpen: boolean; admin: boolean; setDialogOpen: React.Dispatch>; setImageLinks: React.Dispatch>; } const ImageDialog: React.FC = ({ galleryOpen, admin, setDialogOpen, setImageLinks }) => { const [copiedId, setCopiedId] = useState(null); const [editingId, setEditingId] = useState(null); const [images, setImages] = useState([]); const [totalImg, setTotalImg] = useState(0); const [imgPage, setImgPage] = useState(1); const [imgLimit] = useState(3); const fetchImages = async (page: number, limit: number) => { const data = await ApiService.getImages(page, limit); setImages(data.images); setTotalImg(data.total); }; useEffect(() => { fetchImages(imgPage, imgLimit); }, [imgPage]); // Re-fetch images when page changes const handleEditClick = (id: string) => { setEditingId(id === editingId ? null : id); }; const onCopy = (id: string) => { const escLink = 'http://localhost:4400/api/image/get/'+id; setCopiedId(id); setImageLinks(prevLinks => [...prevLinks, escLink]); }; const handleNextPage = () => { if ((imgPage * imgLimit) < totalImg) { setImgPage(prev => prev + 1); } }; const handlePrevPage = () => { if (imgPage > 1) { setImgPage(prev => prev - 1); } }; return ( setDialogOpen(false)} maxWidth="xl" // 'md' stands for medium size > Images disponibles setDialogOpen(false)} style={{ position: "absolute", right: 8, top: 8 }} > {images.map((obj: Images) => ( {`Image {admin && editingId === obj.id ? ( ) : ( obj.file_name )} {obj.id} onCopy(obj.id)} size="small" data-testid={`copy-button-${obj.id}`}> {admin && ( <> handleEditClick(obj.id)} size="small" color="primary" data-testid={`edit-button-${obj.id}`}> )} {copiedId === obj.id && Copié!} ))}
); }; export default ImageDialog;