'use client'; import { useState, useEffect } from 'react'; import { motion, AnimatePresence } from 'framer-motion'; import { FaTimes, FaPlus, FaTrash, FaUpload, FaImage } from 'react-icons/fa'; import { Album, Song } from '@/lib/types'; interface EditAlbumModalProps { show: boolean; album: Album | null; onClose: () => void; onUpdate: (albumId: string, album: Album) => void; } export default function EditAlbumModal({ show, album, onClose, onUpdate }: EditAlbumModalProps) { const [title, setTitle] = useState(''); const [year, setYear] = useState(''); const [genre, setGenre] = useState(''); const [description, setDescription] = useState(''); const [price, setPrice] = useState(''); const [coverImage, setCoverImage] = useState(''); const [coverImageFile, setCoverImageFile] = useState(null); const [isUploading, setIsUploading] = useState(false); const [songs, setSongs] = useState([{ id: '', title: '', duration: '', previewUrl: '', fullUrl: '' }]); const [error, setError] = useState(''); const [tag, setTag] = useState<'Album' | 'EP' | 'Demo' | 'Deluxe' | 'Single'>('Album'); const [format, setFormat] = useState<'mp3' | 'm4a' | 'flac' | 'wav'>('mp3'); const [bitrate, setBitrate] = useState('320kbps'); // Populate form when album changes useEffect(() => { if (album) { setTitle(album.title); setYear(album.year.toString()); setGenre(album.genre); setDescription(album.description); setPrice(album.price.toString()); setCoverImage(album.coverImage || ''); setSongs(album.songs.map((song) => ({ ...song }))); setTag(album.tag); setFormat(album.format); setBitrate(album.bitrate); } }, [album]); const handleAddSong = () => { setSongs([...songs, { id: '', title: '', duration: '', previewUrl: '', fullUrl: '' }]); }; const handleRemoveSong = (index: number) => { if (songs.length > 1) { setSongs(songs.filter((_, i) => i !== index)); } }; const handleSongChange = (index: number, field: 'title' | 'duration' | 'previewUrl' | 'fullUrl', value: string) => { const updatedSongs = [...songs]; updatedSongs[index][field] = value; setSongs(updatedSongs); }; const handleCoverImageChange = async (e: React.ChangeEvent) => { const file = e.target.files?.[0]; if (!file) return; setCoverImageFile(file); setIsUploading(true); setError(''); try { const formData = new FormData(); formData.append('file', file); formData.append('folder', 'album-covers'); const response = await fetch('/api/upload', { method: 'POST', body: formData, }); if (!response.ok) { throw new Error('Upload failed'); } const data = await response.json(); setCoverImage(data.url); } catch (err) { setError('Failed to upload cover image'); setCoverImageFile(null); } finally { setIsUploading(false); } }; const handleSubmit = (e: React.FormEvent) => { e.preventDefault(); setError(''); if (!album) return; // Validation if (!title.trim()) { setError('Title is required'); return; } const yearNum = parseInt(year); if (!year || yearNum < 1900 || yearNum > new Date().getFullYear() + 1) { setError('Please enter a valid year'); return; } if (!genre.trim()) { setError('Genre is required'); return; } if (!description.trim()) { setError('Description is required'); return; } const priceNum = parseFloat(price); if (!price || priceNum <= 0) { setError('Please enter a valid price'); return; } // Validate songs for (let i = 0; i < songs.length; i++) { if (!songs[i].title.trim()) { setError(`Song ${i + 1} title is required`); return; } if (!songs[i].duration.trim()) { setError(`Song ${i + 1} duration is required`); return; } } // Update album const albumSongs: Song[] = songs.map((song, index) => ({ id: song.id || `${album.id}-${index + 1}`, title: song.title, duration: song.duration, previewUrl: song.previewUrl || '/audio/preview-1.mp3', fullUrl: song.fullUrl || '/audio/default-full.mp3', })); const updatedAlbum: Album = { id: album.id, title, coverImage: coverImage || album.coverImage || '/albums/default-cover.jpg', year: yearNum, genre, description, price: priceNum, songs: albumSongs, tag, format, bitrate, }; onUpdate(album.id, updatedAlbum); handleClose(); }; const handleClose = () => { setError(''); onClose(); }; if (!album) return null; return ( {show && ( e.stopPropagation()} className="glass-effect rounded-2xl max-w-2xl w-full max-h-[90vh] overflow-y-auto p-8 border-2 border-accent-orange/30" > {/* Header */}

Edit Album

{/* Form */}
{/* Title */}
setTitle(e.target.value)} placeholder="Enter album title" className="w-full px-4 py-3 bg-white/5 border border-white/10 rounded-lg focus:border-accent-orange focus:outline-none focus:ring-2 focus:ring-accent-orange/50 text-white placeholder-gray-500" required />
{/* Cover Image Upload */}
{coverImage && (
Cover preview
)}

Upload an album cover image (max 5MB, JPG/PNG/WEBP)

{/* Year and Genre */}
setYear(e.target.value)} placeholder="2024" min="1900" max={new Date().getFullYear() + 1} className="w-full px-4 py-3 bg-white/5 border border-white/10 rounded-lg focus:border-accent-orange focus:outline-none focus:ring-2 focus:ring-accent-orange/50 text-white placeholder-gray-500" required />
setGenre(e.target.value)} placeholder="Progressive Rock" className="w-full px-4 py-3 bg-white/5 border border-white/10 rounded-lg focus:border-accent-orange focus:outline-none focus:ring-2 focus:ring-accent-orange/50 text-white placeholder-gray-500" required />
{/* Description */}