"use client"; import React, { useState, useEffect } from 'react'; import { motion, AnimatePresence } from 'framer-motion'; import Image from 'next/image'; import { createProject, deleteProject, updateProject } from './actions'; interface Project { id: number; title: string; year: string; location: string; image: string; gallery: string[]; category: string | null; description: string | null; slug: string; } export default function ProjectsClient({ initialProjects }: { initialProjects: Project[] }) { const [projects, setProjects] = useState(initialProjects); const [isAdding, setIsAdding] = useState(false); const [editingProject, setEditingProject] = useState(null); const [currentCover, setCurrentCover] = useState(null); const [currentGallery, setCurrentGallery] = useState([]); const [isLoading, setIsLoading] = useState(false); const [searchQuery, setSearchQuery] = useState(''); const filteredProjects = projects.filter(p => p.title.toLowerCase().includes(searchQuery.toLowerCase()) || p.location.toLowerCase().includes(searchQuery.toLowerCase()) ); useEffect(() => { if (editingProject) { setCurrentCover(editingProject.image || null); setCurrentGallery(editingProject.gallery || []); } else { setCurrentCover(null); setCurrentGallery([]); } }, [editingProject]); const handleDelete = async (id: number) => { if (confirm('Emin misiniz? Bu işlem projeyi kalıcı olarak silecektir.')) { const prevProjects = [...projects]; setProjects(projects.filter(p => p.id !== id)); try { await deleteProject(id); } catch (err) { setProjects(prevProjects); alert('Proje silinemedi'); } } }; const removeGalleryImage = (url: string) => { setCurrentGallery(prev => prev.filter(img => img !== url)); }; const removeCoverImage = () => { setCurrentCover(null); }; const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); setIsLoading(true); const formData = new FormData(e.currentTarget); formData.append('existingCover', currentCover || ''); formData.append('keepGallery', JSON.stringify(currentGallery)); try { if (editingProject) { await updateProject(editingProject.id, formData); } else { await createProject(formData); } setIsAdding(false); setEditingProject(null); window.location.reload(); } catch (err) { alert('Kaydedilemedi. Bağlantınızı veya dosya boyutlarını kontrol edin.'); } finally { setIsLoading(false); } }; const openEdit = (project: Project) => { setEditingProject(project); setIsAdding(true); }; return ( <>

Proje Yönetimi

Veritabanı Bağlı // {projects.length} Toplam Kayıt

setSearchQuery(e.target.value)} className="flex-1 md:w-64 bg-white/5 border border-white/10 rounded-2xl px-6 py-3 text-sm focus:outline-none focus:ring-2 focus:ring-cyan-500/50 transition-all backdrop-blur-md" />
{filteredProjects.map((project) => (
{project.image ? ( {project.title} ) : (
GÖRSEL_YOK
)}
{project.category}

{project.title}

{project.year}

{project.location}

))}
{isAdding && (
!isLoading && (setIsAdding(false), setEditingProject(null))} className="absolute inset-0 bg-black/90 backdrop-blur-sm" />

{editingProject ? 'Projeyi Düzenle' : 'Yeni Proje Ekle'}