"use client"; import { useState, useEffect, useCallback } from "react"; import Image from "next/image"; import { useDropzone } from "react-dropzone"; import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogDescription, DialogFooter, } from "@/components/ui/dialog"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs"; import Media from "@/interfaces/Media"; import Link from "next/link"; interface PhotoDialogProps { photo?: Media; isOpen: boolean; onClose: () => void; onDelete?: (id: Media["id"]) => void; onSave: (photo: Omit | Media, file: File) => void; } export function PhotoDialog({ photo, isOpen, onClose, onDelete, onSave, }: PhotoDialogProps) { const [file, setFile] = useState(null); const [newPhoto, setNewPhoto] = useState | Media>({ url: "", alt: "", path: "", type: "", size: 0, }); const [activeTab, setActiveTab] = useState("url"); useEffect(() => { setNewPhoto({ url: photo?.url ?? "", alt: photo?.alt ?? "", type: photo?.type ?? "", path: photo?.path ?? "", size: photo?.size ?? 0, }); }, [photo]); const onDrop = useCallback((acceptedFiles: File[]) => { const file = acceptedFiles[0]; if (file) { setFile(file); const reader = new FileReader(); reader.onload = (e) => { setNewPhoto((prev) => ({ ...prev, url: e.target?.result as string, })); }; reader.readAsDataURL(file); } }, []); const { getRootProps, getInputProps, isDragActive } = useDropzone({ onDrop, accept: { "image/*": [] }, }); const handleSave = () => { if (file) onSave(newPhoto, file); onClose(); }; const handleFileChange = (e: React.ChangeEvent) => { const file = e.target.files?.[0]; if (file) { setFile(file); const reader = new FileReader(); reader.onload = (e) => { setNewPhoto((prev) => ({ ...prev, url: e.target?.result as string, })); }; reader.readAsDataURL(file); } }; const isAddMode = !photo; return ( {isAddMode ? "Ajouter une nouvelle photo" : "Éditer la photo"} {isAddMode ? "Ajouter une nouvelle photo dans la gallerie" : "Mettre à jour les informations"} URL Drag & Drop
setNewPhoto({ ...newPhoto, url: e.target.value, }) } placeholder="Enter URL for the image" />
{isDragActive ? (

Déposez une image ici...

) : (

Déposez une image directement ou cliquer pour sélectionner.

)}
{newPhoto.alt}
setNewPhoto({ ...newPhoto, alt: e.target.value }) } placeholder="Entrer une description pour l'image" />
{!isAddMode && onDelete && ( )}
); }