- Changement de table en gallery
- Ajout option de résolution
- Ajout warning
- Intégration de upload
- Ajout de snackbar
This commit is contained in:
Eddi3_As 2025-03-20 21:11:46 -04:00
parent 4e0d5d778d
commit c50cd3e6e7
4 changed files with 320 additions and 210 deletions

View file

@ -0,0 +1,17 @@
export interface ImageType {
id: string;
file_content: string;
file_name: string;
mime_type: string;
}
export interface ImagesResponse {
images: ImageType[];
total: number;
}
export interface ImagesParams {
page: number;
limit: number;
uid?: string;
}

View file

@ -1,169 +1,280 @@
import React, { useState, useEffect } from "react";
import {
Box,
CircularProgress,
Button,
IconButton,
Card,
CardContent,
Dialog,
DialogTitle,
DialogContent,
DialogActions,
Button,
Table,
TableBody,
TableCell,
TableContainer,
TableRow,
IconButton,
Paper,
Box,
CircularProgress
DialogTitle,
DialogContentText,
Tabs,
Tab,
TextField, Snackbar,
Alert
} from "@mui/material";
import DeleteIcon from "@mui/icons-material/Delete";
import ContentCopyIcon from "@mui/icons-material/ContentCopy";
import CloseIcon from "@mui/icons-material/Close";
import DeleteIcon from "@mui/icons-material/Delete";
import { Images } from "../../Types/Images";
import ApiService from '../../services/ApiService';
import { ENV_VARIABLES } from '../../constants';
import { ImageType } from "../../Types/ImageType";
import ApiService from "../../services/ApiService";
import { Upload } from "@mui/icons-material";
type Props = {
galleryOpen: boolean;
setDialogOpen: React.Dispatch<React.SetStateAction<boolean>>;
setImageLinks: React.Dispatch<React.SetStateAction<string[]>>;
};
interface ImagesProps {
handleCopy?: (id: string) => void;
}
const ImageDialog: React.FC<Props> = ({ galleryOpen, setDialogOpen, setImageLinks }) => {
const [copiedId, setCopiedId] = useState<string | null>(null);
const [images, setImages] = useState<Images[]>([]);
const ImageGallery: React.FC<ImagesProps> = ({ handleCopy }) => {
const [images, setImages] = useState<ImageType[]>([]);
const [totalImg, setTotalImg] = useState(0);
const [imgPage, setImgPage] = useState(1);
const [imgLimit] = useState(3);
const [imgLimit] = useState(6);
const [loading, setLoading] = useState(false);
const [deleteConfirm, setDeleteConfirm] = useState<{ id: string | null; linked: boolean }>({ id: null, linked: false });
const [selectedImage, setSelectedImage] = useState<ImageType | null>(null);
const [openDeleteDialog, setOpenDeleteDialog] = useState(false);
const [imageToDelete, setImageToDelete] = useState<ImageType | null>(null);
const [tabValue, setTabValue] = useState(0);
const [importedImage, setImportedImage] = useState<File | null>(null);
const [preview, setPreview] = useState<string | null>(null);
const [snackbarOpen, setSnackbarOpen] = useState(false);
const [snackbarMessage, setSnackbarMessage] = useState("");
const [snackbarSeverity, setSnackbarSeverity] = useState<"success" | "error">("success");
const fetchImages = async (page: number, limit: number) => {
const data = await ApiService.getImages(page, limit);
const fetchImages = async () => {
setLoading(true);
const data = await ApiService.getImages(imgPage, imgLimit);
setImages(data.images);
setTotalImg(data.total);
setLoading(false);
};
useEffect(() => {
fetchImages(imgPage, imgLimit);
fetchImages();
}, [imgPage]);
const onCopy = (id: string) => {
const escLink = `${ENV_VARIABLES.IMG_URL}/api/image/get/${id}`;
setCopiedId(id);
setImageLinks(prevLinks => [...prevLinks, escLink]);
};
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) {
const handleDelete = async () => {
if (imageToDelete) {
setLoading(true);
await ApiService.deleteImage(deleteConfirm.id);
setImages(images.filter(image => image.id !== deleteConfirm.id));
setDeleteConfirm({ id: null, linked: false });
const isDeleted = await ApiService.deleteImage(imageToDelete.id);
setLoading(false);
if (isDeleted) {
setImages(images.filter((image) => image.id !== imageToDelete.id));
setSnackbarMessage("Image supprimée avec succès !");
setSnackbarSeverity("success");
} else {
setSnackbarMessage("Erreur lors de la suppression de l'image. Veuillez réessayer.");
setSnackbarSeverity("error");
}
setSnackbarOpen(true);
setSelectedImage(null);
setImageToDelete(null);
setOpenDeleteDialog(false);
}
};
const handleNextPage = () => {
if ((imgPage * imgLimit) < totalImg) {
setImgPage(prev => prev + 1);
const defaultHandleCopy = (id: string) => {
if (navigator.clipboard) {
navigator.clipboard.writeText(id);
}
};
const handlePrevPage = () => {
if (imgPage > 1) {
setImgPage(prev => prev - 1);
const handleCopyFunction = handleCopy || defaultHandleCopy;
const handleImageUpload = (event: React.ChangeEvent<HTMLInputElement>) => {
const file = event.target.files ? event.target.files[0] : null;
setImportedImage(file);
if (file) {
const objectUrl = URL.createObjectURL(file);
setPreview(objectUrl);
}
};
const handleSaveImage = async () => {
try {
if (!importedImage) {
setSnackbarMessage("Veuillez d'abord choisir une image à téléverser.");
setSnackbarSeverity("error");
setSnackbarOpen(true);
return;
}
const imageUrl = await ApiService.uploadImage(importedImage);
if (imageUrl.includes("ERROR")) {
setSnackbarMessage("Une erreur est survenue. Veuillez réessayer plus tard.");
setSnackbarSeverity("error");
setSnackbarOpen(true);
return;
}
fetchImages();
setSnackbarMessage("Téléversée avec succès !");
setSnackbarSeverity("success");
setSnackbarOpen(true);
// Reset the input field and preview after successful upload
setImportedImage(null);
setPreview(null);
} catch (error) {
setSnackbarMessage(`Une erreur est survenue.\n${error}\nVeuillez réessayer plus tard.`);
setSnackbarSeverity("error");
setSnackbarOpen(true);
}
};
return (
<Dialog open={galleryOpen} onClose={() => setDialogOpen(false)} maxWidth="xl">
<DialogTitle>
Images disponibles
<IconButton
aria-label="close"
color="primary"
onClick={() => setDialogOpen(false)}
style={{ position: "absolute", right: 8, top: 8 }}
>
<CloseIcon />
</IconButton>
</DialogTitle>
<DialogContent>
{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>
<Box p={3}>
<Tabs value={tabValue} onChange={(_, newValue) => setTabValue(newValue)}>
<Tab label="Gallery" />
<Tab label="Import" />
</Tabs>
{tabValue === 0 && (
<>
{loading ? (
<Box display="flex" justifyContent="center" alignItems="center" height={200}>
<CircularProgress />
</Box>
) : (
<>
<Box display="grid" gridTemplateColumns="repeat(3, 1fr)" gridTemplateRows="repeat(2, 1fr)" gap={2} maxWidth="900px" margin="auto">
{images.map((obj) => (
<Card key={obj.id} sx={{ cursor: "pointer", display: "flex", flexDirection: "column", alignItems: "center" }} onClick={() => setSelectedImage(obj)}>
<CardContent sx={{ p: 0 }}>
<img
src={`data:${obj.mime_type};base64,${obj.file_content}`}
alt={`Image ${obj.file_name}`}
style={{ width: 350, height: "auto", borderRadius: 8 }}
style={{ width: "100%", height: 250, objectFit: "cover", borderRadius: 8 }}
/>
</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" />
</CardContent>
<Box display="flex" justifyContent="center" mt={1}>
<IconButton onClick={(e) => {
e.stopPropagation();
handleCopyFunction(obj.id);
}}
color="primary" >
<ContentCopyIcon sx={{ fontSize: 18 }} />
</IconButton>
<IconButton onClick={() => handleDelete(obj.id)} size="small" color="primary" data-testid={`delete-button-${obj.id}`}>
<DeleteIcon fontSize="small" />
<IconButton
onClick={(e) => {
e.stopPropagation();
setImageToDelete(obj);
setOpenDeleteDialog(true);
}}
color="error" >
<DeleteIcon sx={{ fontSize: 18 }} />
</IconButton>
{copiedId === obj.id && <span style={{ marginLeft: 8, color: "green" }}>Copié!</span>}
</TableCell>
</TableRow>
</Box>
</Card>
))}
</TableBody>
</Table>
</TableContainer>
)}
</DialogContent>
{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
</Button>
<Button onClick={confirmDelete} color="secondary">
Supprimer
</Button>
</DialogActions>
</Dialog>
</Box>
<Box display="flex" justifyContent="center" mt={2}>
<Button onClick={() => setImgPage((prev) => Math.max(prev - 1, 1))} disabled={imgPage === 1} color="primary">
Précédent
</Button>
<Button onClick={() => setImgPage((prev) => (prev * imgLimit < totalImg ? prev + 1 : prev))} disabled={imgPage * imgLimit >= totalImg} color="primary">
Suivant
</Button>
</Box>
</>
)}
</>
)}
<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>
{tabValue === 1 && (
<Box display="flex" flexDirection="column" alignItems="center" width="100%" mt={3}>
{/* Image Preview at the top */}
{preview && (
<Box
mt={2}
mb={2}
sx={{
width: "100%",
maxWidth: 600,
textAlign: "center",
display: "flex",
justifyContent: "center",
alignItems: "center",
}}
>
<img
src={preview}
alt="Preview"
style={{
width: "100%",
height: "auto",
borderRadius: 8,
maxHeight: "600px",
}}
/>
</Box>
)}
<Box display="flex" flexDirection="row" alignItems="center" width="100%" maxWidth={400}>
<TextField
type="file"
onChange={handleImageUpload}
slotProps={{
htmlInput: {
accept: "image/*",
},
}}
sx={{ flexGrow: 1 }}
/>
<Button
variant="outlined"
aria-label="Téléverser"
onClick={() => { handleSaveImage() }}
sx={{ ml: 2, height: "100%" }}
>
Téléverser <Upload sx={{ ml: 1 }} />
</Button>
</Box>
</Box>
</DialogActions>
</Dialog>
)}
<Dialog open={!!selectedImage} onClose={() => setSelectedImage(null)} maxWidth="md">
<IconButton color="primary" onClick={() => setSelectedImage(null)} sx={{ position: "absolute", right: 8, top: 8, zIndex: 1 }}>
<CloseIcon />
</IconButton>
<DialogContent>
{selectedImage && (
<img
src={`data:${selectedImage.mime_type};base64,${selectedImage.file_content}`}
alt="Enlarged view"
style={{ width: "100%", height: "auto", borderRadius: 8, maxHeight: "500px" }}
/>
)}
</DialogContent>
</Dialog>
{/* Delete Confirmation Dialog */}
<Dialog open={openDeleteDialog} onClose={() => setOpenDeleteDialog(false)}>
<DialogTitle>Supprimer</DialogTitle>
<DialogContent>
<DialogContentText>Voulez-vous supprimer cette image?</DialogContentText>
</DialogContent>
<DialogActions>
<Button onClick={() => setOpenDeleteDialog(false)} color="primary">
Annuler
</Button>
<Button onClick={handleDelete} color="error">
Delete
</Button>
</DialogActions>
</Dialog>
<Snackbar open={snackbarOpen} autoHideDuration={4000} onClose={() => setSnackbarOpen(false)}>
<Alert onClose={() => setSnackbarOpen(false)} severity={snackbarSeverity} sx={{ width: "100%" }}>
{snackbarMessage}
</Alert>
</Snackbar>
</Box>
);
};
export default ImageDialog;
export default ImageGallery;

View file

@ -0,0 +1,54 @@
import React, { useState } from "react";
import {
Button,
IconButton,
Dialog,
DialogContent,
} from "@mui/material";
import CloseIcon from "@mui/icons-material/Close";
import ImageGallery from "../ImageGallery";
import { ImageSearch } from "@mui/icons-material";
interface ImageGalleryModalProps {
handleCopy?: (id: string) => void;
}
const ImageGalleryModal: React.FC<ImageGalleryModalProps> = ({ handleCopy }) => {
const [open, setOpen] = useState<boolean>(false);
const handleOpen = () => setOpen(true);
const handleClose = () => setOpen(false);
return (
<>
<Button
variant="outlined"
aria-label='Téléverser'
onClick={() => handleOpen()}>
Images <ImageSearch />
</Button>
<Dialog open={open} onClose={handleClose} maxWidth="md" fullWidth>
<DialogContent sx={{ display: "flex", flexDirection: "column", alignItems: "center", py: 3 }}>
<IconButton
onClick={handleClose}
color="primary"
sx={{
position: "absolute",
right: 8,
top: 8,
zIndex: 1,
}}
>
<CloseIcon />
</IconButton>
<ImageGallery handleCopy={handleCopy}/>
</DialogContent>
</Dialog>
</>
);
};
export default ImageGalleryModal;

View file

@ -1,5 +1,5 @@
// EditorQuiz.tsx
import React, { useState, useEffect, useRef, CSSProperties } from 'react';
import React, { useState, useEffect, CSSProperties } from 'react';
import { useParams, useNavigate } from 'react-router-dom';
import { FolderType } from '../../../Types/FolderType';
@ -11,13 +11,13 @@ import GIFTTemplatePreview from 'src/components/GiftTemplate/GIFTTemplatePreview
import { QuizType } from '../../../Types/QuizType';
import './editorQuiz.css';
import { Button, TextField, NativeSelect, Divider, Dialog, DialogTitle, DialogActions, DialogContent } from '@mui/material';
import { Button, TextField, NativeSelect, Divider } from '@mui/material';
import ReturnButton from 'src/components/ReturnButton/ReturnButton';
import ImageGallery from 'src/components/ImageGallery/ImageGallery';
import ImageGalleryModal from 'src/components/ImageGallery/ImageGalleryModal/ImageGalleryModal';
import ApiService from '../../../services/ApiService';
import { escapeForGIFT } from '../../../utils/giftUtils';
import { Upload, ImageSearch } from '@mui/icons-material';
import { ENV_VARIABLES } from '../../../constants';
interface EditQuizParams {
id: string;
@ -39,9 +39,6 @@ const QuizForm: React.FC = () => {
const handleSelectFolder = (event: React.ChangeEvent<HTMLSelectElement>) => {
setSelectedFolder(event.target.value);
};
const fileInputRef = useRef<HTMLInputElement>(null);
const [dialogOpen, setDialogOpen] = useState(false);
const [galleryOpen, setGalleryOpen] = useState(false);
const [showScrollButton, setShowScrollButton] = useState(false);
const scrollToTop = () => {
@ -168,44 +165,16 @@ const QuizForm: React.FC = () => {
return <div>Chargement...</div>;
}
const handleSaveImage = async () => {
try {
const inputElement = document.getElementById('file-input') as HTMLInputElement;
if (!inputElement?.files || inputElement.files.length === 0) {
setDialogOpen(true);
return;
}
if (!inputElement.files || inputElement.files.length === 0) {
window.alert("Veuillez d'abord choisir une image à téléverser.")
return;
}
const imageUrl = await ApiService.uploadImage(inputElement.files[0]);
// Check for errors
if(imageUrl.indexOf("ERROR") >= 0) {
window.alert(`Une erreur est survenue.\n Veuillez réessayer plus tard`)
return;
}
setImageLinks(prevLinks => [...prevLinks, imageUrl]);
// Reset the file input element
if (fileInputRef.current) {
fileInputRef.current.value = '';
}
} catch (error) {
window.alert(`Une erreur est survenue.\n${error}\nVeuillez réessayer plus tard.`)
}
};
const handleCopyToClipboard = async (link: string) => {
navigator.clipboard.writeText(link);
}
const handleCopyImage = (id: string) => {
const escLink = `${ENV_VARIABLES.IMG_URL}/api/image/get/${id}`;
navigator.clipboard.writeText(id);
setImageLinks(prevLinks => [...prevLinks, escLink]);
}
return (
<div className='quizEditor'>
@ -260,52 +229,11 @@ const QuizForm: React.FC = () => {
onEditorChange={handleUpdatePreview} />
<div className='images'>
<div className='upload'>
<label className="dropArea">
<input type="file" id="file-input" className="file-input"
accept="image/jpeg, image/png"
multiple
ref={fileInputRef} />
<Button
variant="outlined"
aria-label='Téléverser'
onClick={handleSaveImage}>
Téléverser <Upload />
</Button>
</label>
<Dialog
open={dialogOpen}
onClose={() => setDialogOpen(false)} >
<DialogTitle>Erreur</DialogTitle>
<DialogContent>
Veuillez d&apos;abord choisir une image à téléverser.
</DialogContent>
<DialogActions>
<Button onClick={() => setDialogOpen(false)} color="primary">
OK
</Button>
</DialogActions>
</Dialog>
<div style={{ display: 'flex', alignItems: 'center', gap: '10px' }}>
<h4>Mes images :</h4>
<ImageGalleryModal handleCopy={handleCopyImage} />
</div>
<h4>Mes images :</h4>
<Button
variant="outlined"
aria-label='Téléverser'
onClick={() => setGalleryOpen(true)}>
Images <ImageSearch />
</Button>
<ImageGallery
galleryOpen={galleryOpen}
setDialogOpen={setGalleryOpen}
setImageLinks={setImageLinks}
>
</ImageGallery>
<div>
<div>
<div style={{ display: "inline" }}>(Voir section </div>
@ -319,7 +247,7 @@ const QuizForm: React.FC = () => {
</div>
<ul>
{imageLinks.map((link, index) => {
const imgTag = `![alt_text](${escapeForGIFT(link)} "texte de l'infobulle")`;
const imgTag = `[markdown]![alt_text](${escapeForGIFT(link)} "texte de l'infobulle") {T}`;
return (
<li key={index}>
<code