"use client" import { useState, useEffect } from "react" import { Button } from "@/components/ui/button" import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, DialogTrigger, } from "@/components/ui/dialog" import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "@/components/ui/select" import { ListMusic, Plus, Loader2 } from "lucide-react" import { useToast } from "@/components/ui/use-toast" import { getApiUrl } from "@/lib/api-config" import { Label } from "@/components/ui/label" import { Input } from "@/components/ui/input" interface AddToPlaylistDialogProps { performanceId: number songTitle: string } export function AddToPlaylistDialog({ performanceId, songTitle }: AddToPlaylistDialogProps) { const [open, setOpen] = useState(false) const [playlists, setPlaylists] = useState([]) const [loading, setLoading] = useState(false) const [submitting, setSubmitting] = useState(false) const [selectedPlaylistId, setSelectedPlaylistId] = useState("") const [notes, setNotes] = useState("") const { toast } = useToast() useEffect(() => { if (open && playlists.length === 0) { setLoading(true) const token = localStorage.getItem("token") if (!token) return fetch(`${getApiUrl()}/playlists/mine`, { headers: { Authorization: `Bearer ${token}` } }) .then(res => res.json()) .then(data => setPlaylists(data)) .catch(err => console.error(err)) .finally(() => setLoading(false)) } }, [open, playlists.length]) const handleSubmit = async () => { if (!selectedPlaylistId) return setSubmitting(true) const token = localStorage.getItem("token") try { const res = await fetch(`${getApiUrl()}/playlists/${selectedPlaylistId}/performances/${performanceId}`, { method: "POST", headers: { "Content-Type": "application/json", Authorization: `Bearer ${token}` }, body: JSON.stringify({ notes: notes || undefined }) }) if (res.ok) { toast({ title: "Added to playlist" }) setOpen(false) } else if (res.status === 400) { toast({ title: "Already in playlist", variant: "default" }) } else { throw new Error("Failed to add") } } catch (error) { toast({ title: "Error", description: "Could not add to playlist", variant: "destructive" }) } finally { setSubmitting(false) } } return ( Add to Playlist Add "{songTitle}" to one of your collections.
setNotes(e.target.value)} />
) }