'use client' import { useState } from 'react' import { createList, updateList } from '@/lib/actions' import { useRouter } from 'next/navigation' import { useToast } from './ToastContainer' interface ListFormProps { list?: { id: string title: string color: string } onCancel?: () => void onSuccess?: () => void } const PRESET_COLORS = [ '#3B82F6', // blue '#EF4444', // red '#10B981', // green '#F59E0B', // amber '#8B5CF6', // purple '#EC4899', // pink '#06B6D4', // cyan '#F97316', // orange ] export default function ListForm({ list, onCancel, onSuccess }: ListFormProps) { const router = useRouter() const { showToast } = useToast() const [title, setTitle] = useState(list?.title || '') const [color, setColor] = useState(list?.color || PRESET_COLORS[0]) const [isSubmitting, setIsSubmitting] = useState(false) const [error, setError] = useState('') const handleSubmit = async (e: React.FormEvent) => { e.preventDefault() if (!title.trim()) { setError('List title is required') return } setIsSubmitting(true) setError('') try { if (list) { // Update existing list await updateList(list.id, { title: title.trim(), color }) showToast('List updated successfully', 'success') } else { // Create new list await createList(title.trim(), color) showToast('List created successfully', 'success') } if (onSuccess) { onSuccess() } else { router.refresh() } } catch (err) { setError('Failed to save list. Please try again.') showToast('Failed to save list', 'error') console.error(err) } finally { setIsSubmitting(false) } } return (
) }