EvalueTonSavoir/client/src/components/ImageGallery/ImageGallery.tsx

170 lines
5.7 KiB
TypeScript
Raw Normal View History

2025-03-04 19:49:18 -05:00
import React, { useState, useEffect } from "react";
import {
Dialog,
DialogTitle,
DialogContent,
DialogActions,
Button,
Table,
TableBody,
TableCell,
TableContainer,
TableRow,
IconButton,
Paper,
2025-03-13 18:02:26 -04:00
Box,
CircularProgress
2025-03-04 19:49:18 -05:00
} from "@mui/material";
import ContentCopyIcon from "@mui/icons-material/ContentCopy";
import CloseIcon from "@mui/icons-material/Close";
2025-03-13 18:02:26 -04:00
import DeleteIcon from "@mui/icons-material/Delete";
2025-03-04 19:49:18 -05:00
import { Images } from "../../Types/Images";
import ApiService from '../../services/ApiService';
2025-03-11 19:53:34 -04:00
import { ENV_VARIABLES } from '../../constants';
2025-03-04 19:49:18 -05:00
type Props = {
galleryOpen: boolean;
setDialogOpen: React.Dispatch<React.SetStateAction<boolean>>;
setImageLinks: React.Dispatch<React.SetStateAction<string[]>>;
2025-03-13 18:02:26 -04:00
};
2025-03-04 19:49:18 -05:00
2025-03-13 18:02:26 -04:00
const ImageDialog: React.FC<Props> = ({ galleryOpen, setDialogOpen, setImageLinks }) => {
2025-03-04 19:49:18 -05:00
const [copiedId, setCopiedId] = useState<string | null>(null);
const [images, setImages] = useState<Images[]>([]);
const [totalImg, setTotalImg] = useState(0);
const [imgPage, setImgPage] = useState(1);
const [imgLimit] = useState(3);
2025-03-13 18:02:26 -04:00
const [loading, setLoading] = useState(false);
const [deleteConfirm, setDeleteConfirm] = useState<{ id: string | null; linked: boolean }>({ id: null, linked: false });
2025-03-04 19:49:18 -05:00
const fetchImages = async (page: number, limit: number) => {
const data = await ApiService.getImages(page, limit);
setImages(data.images);
setTotalImg(data.total);
};
useEffect(() => {
fetchImages(imgPage, imgLimit);
2025-03-13 18:02:26 -04:00
}, [imgPage]);
2025-03-04 19:49:18 -05:00
const onCopy = (id: string) => {
const escLink = `${ENV_VARIABLES.IMG_URL}/api/image/get/${id}`;
2025-03-04 19:49:18 -05:00
setCopiedId(id);
2025-03-04 19:59:05 -05:00
setImageLinks(prevLinks => [...prevLinks, escLink]);
2025-03-04 19:49:18 -05:00
};
2025-03-13 18:02:26 -04:00
const handleDelete = async (id: string) => {
setLoading(true);
const isDeleted = await ApiService.deleteImage(id);
setLoading(false);
if (!isDeleted) {
setDeleteConfirm({ id, linked: true });
} else {
setImages(images.filter(image => image.id !== id));
setDeleteConfirm({ id: null, linked: false });
}
};
const confirmDelete = async () => {
if (deleteConfirm.id) {
setLoading(true);
await ApiService.deleteImage(deleteConfirm.id);
setImages(images.filter(image => image.id !== deleteConfirm.id));
setDeleteConfirm({ id: null, linked: false });
setLoading(false);
}
};
2025-03-04 19:49:18 -05:00
const handleNextPage = () => {
2025-03-13 18:02:26 -04:00
if ((imgPage * imgLimit) < totalImg) {
setImgPage(prev => prev + 1);
}
2025-03-04 19:49:18 -05:00
};
2025-03-13 18:02:26 -04:00
2025-03-04 19:49:18 -05:00
const handlePrevPage = () => {
2025-03-13 18:02:26 -04:00
if (imgPage > 1) {
setImgPage(prev => prev - 1);
}
2025-03-04 19:49:18 -05:00
};
return (
2025-03-13 18:02:26 -04:00
<Dialog open={galleryOpen} onClose={() => setDialogOpen(false)} maxWidth="xl">
2025-03-04 19:49:18 -05:00
<DialogTitle>
Images disponibles
<IconButton
aria-label="close"
color="primary"
onClick={() => setDialogOpen(false)}
style={{ position: "absolute", right: 8, top: 8 }}
>
<CloseIcon />
</IconButton>
</DialogTitle>
<DialogContent>
2025-03-13 18:02:26 -04:00
{loading ? (
<Box display="flex" justifyContent="center" alignItems="center" height={200}>
<CircularProgress />
</Box>
) : (
<TableContainer component={Paper}>
<Table>
<TableBody>
{images.map((obj: Images) => (
<TableRow key={obj.id}>
<TableCell>
<img
src={`data:${obj.mime_type};base64,${obj.file_content}`}
alt={`Image ${obj.file_name}`}
style={{ width: 350, height: "auto", borderRadius: 8 }}
2025-03-04 19:49:18 -05:00
/>
2025-03-13 18:02:26 -04:00
</TableCell>
<TableCell>{obj.file_name}</TableCell>
<TableCell style={{ minWidth: 150 }}>
{obj.id}
<IconButton onClick={() => onCopy(obj.id)} size="small" data-testid={`copy-button-${obj.id}`}>
<ContentCopyIcon fontSize="small" />
</IconButton>
<IconButton onClick={() => handleDelete(obj.id)} size="small" color="secondary" data-testid={`delete-button-${obj.id}`}>
<DeleteIcon fontSize="small" />
</IconButton>
{copiedId === obj.id && <span style={{ marginLeft: 8, color: "green" }}>Copié!</span>}
</TableCell>
</TableRow>
))}
</TableBody>
</Table>
</TableContainer>
)}
2025-03-04 19:49:18 -05:00
</DialogContent>
2025-03-13 18:02:26 -04:00
{deleteConfirm.linked && (
<Dialog open={Boolean(deleteConfirm.id)} onClose={() => setDeleteConfirm({ id: null, linked: false })}>
<DialogTitle>Confirmer la suppression</DialogTitle>
<DialogContent>
Cette image est liée à d'autres objets. Êtes-vous sûr de vouloir la supprimer ?
</DialogContent>
<DialogActions>
<Button onClick={() => setDeleteConfirm({ id: null, linked: false })} color="primary">
Annuler
2025-03-04 19:49:18 -05:00
</Button>
2025-03-13 18:02:26 -04:00
<Button onClick={confirmDelete} color="secondary">
Supprimer
2025-03-04 19:49:18 -05:00
</Button>
2025-03-13 18:02:26 -04:00
</DialogActions>
</Dialog>
)}
<DialogActions style={{ justifyContent: "center", width: "100%" }}>
<Box display="flex" justifyContent="center" width="100%">
<Button onClick={handlePrevPage} disabled={imgPage === 1} color="primary">
Précédent
</Button>
<Button onClick={handleNextPage} disabled={(imgPage * imgLimit) >= totalImg} color="primary">
Suivant
</Button>
</Box>
</DialogActions>
2025-03-04 19:49:18 -05:00
</Dialog>
);
};
export default ImageDialog;