import React, { useState, useEffect } from 'react'; import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query'; import { tcgApi } from '../../services/tcgApi'; import type { CreateDeckData } from '../../types'; interface DeckManagerProps { isOpen: boolean; onClose: () => void; deckId?: string; // For editing existing deck } const DeckManager: React.FC = ({ isOpen, onClose, deckId }) => { const queryClient = useQueryClient(); const [formData, setFormData] = useState({ name: '', description: '', game: 'MTG', format: '', tags: [], color: '#8b5cf6', isPublic: false, }); const [selectedTags, setSelectedTags] = useState([]); const colors = [ '#8b5cf6', '#3b82f6', '#10b981', '#f59e0b', '#ef4444', '#ec4899', '#6366f1', '#06b6d4', '#84cc16', '#f97316' ]; // Queries const { data: existingDeck } = useQuery({ queryKey: ['deck', deckId], queryFn: () => tcgApi.decks.getDeck(deckId!), enabled: !!deckId, }); const { data: tags = [] } = useQuery({ queryKey: ['tags'], queryFn: () => tcgApi.tags.getTags(), }); // Mutations const createMutation = useMutation({ mutationFn: tcgApi.decks.createDeck, onSuccess: () => { queryClient.invalidateQueries({ queryKey: ['decks'] }); onClose(); }, }); const updateMutation = useMutation({ mutationFn: ({ id, data }: { id: string; data: Partial }) => tcgApi.decks.updateDeck(id, data), onSuccess: () => { queryClient.invalidateQueries({ queryKey: ['decks'] }); queryClient.invalidateQueries({ queryKey: ['deck', deckId] }); onClose(); }, }); // Initialize form with existing deck data useEffect(() => { if (existingDeck) { setFormData({ name: existingDeck.name, description: existingDeck.description || '', game: existingDeck.game, format: existingDeck.format || '', tags: existingDeck.tags, color: existingDeck.color || '#8b5cf6', isPublic: existingDeck.isPublic, }); setSelectedTags(existingDeck.tags); } }, [existingDeck]); const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); const submitData = { ...formData, tags: selectedTags, }; if (deckId && existingDeck) { updateMutation.mutate({ id: deckId, data: submitData }); } else { createMutation.mutate(submitData); } }; const getGameBadgeColor = (game: string) => { switch (game) { case 'MTG': return 'bg-orange-100 text-orange-800 dark:bg-orange-900/30 dark:text-orange-300'; case 'POKEMON': return 'bg-yellow-100 text-yellow-800 dark:bg-yellow-900/30 dark:text-yellow-300'; case 'LORCANA': return 'bg-purple-100 text-purple-800 dark:bg-purple-900/30 dark:text-purple-300'; case 'YUGIOH': return 'bg-blue-100 text-blue-800 dark:bg-blue-900/30 dark:text-blue-300'; default: return 'bg-surface-100 text-surface-800 dark:bg-surface-700 dark:text-surface-300'; } }; const getFormatOptions = (game: string) => { switch (game) { case 'MTG': return [ { value: 'standard', label: 'Standard' }, { value: 'modern', label: 'Modern' }, { value: 'commander', label: 'Commander' }, { value: 'pioneer', label: 'Pioneer' }, { value: 'legacy', label: 'Legacy' }, { value: 'vintage', label: 'Vintage' }, { value: 'draft', label: 'Draft' }, { value: 'sealed', label: 'Sealed' }, ]; case 'POKEMON': return [ { value: 'standard', label: 'Standard' }, { value: 'expanded', label: 'Expanded' }, { value: 'unlimited', label: 'Unlimited' }, ]; case 'LORCANA': return [ { value: 'standard', label: 'Standard' }, { value: 'constructed', label: 'Constructed' }, ]; case 'YUGIOH': return [ { value: 'advanced', label: 'Advanced' }, { value: 'traditional', label: 'Traditional' }, ]; default: return []; } }; if (!isOpen) return null; return (
{/* Header */}

{deckId ? 'Edit Deck' : 'New Deck'}

e.stopPropagation()}> {/* Deck Preview */}
🎴

{formData.name || 'Deck Name'}

{formData.description && (

{formData.description}

)}
{formData.game} {formData.format && ( {formData.format} )}
{/* Name */}
setFormData(prev => ({ ...prev, name: e.target.value }))} placeholder="Enter deck name..." className="w-full px-4 py-3 bg-surface-50 dark:bg-surface-700 border border-surface-300 dark:border-surface-600 rounded-xl text-surface-900 dark:text-white placeholder-surface-500 dark:placeholder-surface-400 focus:outline-none focus:ring-2 focus:ring-primary-500 transition-colors" required />
{/* Description */}