import { useState } from "react"; import { API_URL } from "../config/api.ts"; import type { Playlist, RawPlaylist } from "../model.ts"; import { deserializePlaylist } from "../model.ts"; import { useAuth } from "../hooks/useAuth.ts"; interface PlaylistCreateFormProps { /** If provided, the new playlist will have this dump added to it. */ dumpId?: string; onCreated: (playlist: Playlist) => void; onCancel: () => void; } export function PlaylistCreateForm( { dumpId, onCreated, onCancel }: PlaylistCreateFormProps, ) { const { authFetch } = useAuth(); const [title, setTitle] = useState(""); const [description, setDescription] = useState(""); const [isPublic, setIsPublic] = useState(true); const [submitting, setSubmitting] = useState(false); const [error, setError] = useState(null); const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); if (!title.trim()) return; setSubmitting(true); setError(null); try { const res = await authFetch(`${API_URL}/api/playlists`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ title: title.trim(), description: description.trim() || undefined, isPublic, }), }); const body = await res.json(); if (!body.success) { setError(body.error?.message ?? "Failed to create playlist"); return; } const playlist = deserializePlaylist(body.data as RawPlaylist); if (dumpId) { await authFetch( `${API_URL}/api/playlists/${playlist.id}/dumps/${dumpId}`, { method: "POST" }, ); } onCreated(playlist); } catch { setError("Failed to create playlist"); } finally { setSubmitting(false); } }; return (
setTitle(e.target.value)} autoFocus required />