import { useState } from 'react'; import { useForm } from 'react-hook-form'; import { zodResolver } from '@hookform/resolvers/zod'; import { z } from 'zod'; // Ensure PlaylistError is exported from service or assume general Error if not available import { createPlaylist } from '../services/playlistService'; import { Dialog } from '@/components/ui/dialog'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { Label } from '@/components/ui/label'; import { Textarea } from '@/components/ui/textarea'; import { Checkbox } from '@/components/ui/checkbox'; import { useToast } from '@/hooks/useToast'; import { Loader2 } from 'lucide-react'; const createPlaylistSchema = z.object({ title: z .string() .min(1, 'Le titre est requis') .max(200, 'Le titre ne peut pas dépasser 200 caractères'), description: z .string() .max(1000, 'La description ne peut pas dépasser 1000 caractères') .optional(), is_public: z.boolean().default(true), }); type CreatePlaylistFormData = z.infer; interface CreatePlaylistDialogProps { open: boolean; onOpenChange: (open: boolean) => void; onCreated?: () => void; } export function CreatePlaylistDialog({ open, onOpenChange, onCreated, }: CreatePlaylistDialogProps) { const [isSubmitting, setIsSubmitting] = useState(false); const { success: showSuccess, error: showError } = useToast(); const { register, handleSubmit, formState: { errors }, reset, watch, } = useForm({ resolver: zodResolver(createPlaylistSchema), defaultValues: { title: '', description: '', is_public: true, }, }); const isPublic = watch('is_public'); const onSubmit = async (data: CreatePlaylistFormData) => { setIsSubmitting(true); try { await createPlaylist({ title: data.title, description: data.description || undefined, is_public: data.is_public, }); showSuccess('Playlist créée : Votre playlist a été créée avec succès.'); reset(); onOpenChange(false); onCreated?.(); } catch (error) { console.error('Failed to create playlist:', error); showError( error instanceof Error ? error.message : 'Impossible de créer la playlist', ); } finally { setIsSubmitting(false); } }; const handleCancel = () => { reset(); onOpenChange(false); }; return (
{errors.title && ( )}