import { useEffect, useState } from 'react'; import { useTranslation } from 'react-i18next'; import { shoppingItemApi } from '../api/client'; import type { ShoppingItem } from '../types'; import { X, Plus, Trash2, ShoppingCart, Check, Loader2, Pencil, } from 'lucide-react'; import ConfirmModal from './ConfirmModal'; interface ShoppingListModalProps { familyId: number; onClose: () => void; } type ConfirmAction = | { type: 'delete-item'; itemId: number } | { type: 'mark-all' } | { type: 'clear-all' }; export default function ShoppingListModal({ familyId, onClose }: ShoppingListModalProps) { const { t } = useTranslation(); const [items, setItems] = useState([]); const [loading, setLoading] = useState(true); const [newItemName, setNewItemName] = useState(''); const [editingId, setEditingId] = useState(null); const [editingName, setEditingName] = useState(''); const [confirmAction, setConfirmAction] = useState(null); useEffect(() => { loadItems(); }, [familyId]); const loadItems = async () => { try { setLoading(true); const response = await shoppingItemApi.getAllByFamily(familyId); setItems(response.data); } catch (err) { console.error('Error loading shopping items:', err); alert(t('shopping.loadError')); } finally { setLoading(false); } }; const handleAddItem = async () => { if (!newItemName.trim()) return; try { await shoppingItemApi.create(familyId, { name: newItemName }); setNewItemName(''); loadItems(); } catch (err) { console.error('Error adding item:', err); alert(t('shopping.addError')); } }; const handleTogglePurchased = async (itemId: number, currentStatus: boolean) => { try { await shoppingItemApi.markAsPurchased(familyId, itemId, { is_purchased: !currentStatus }); loadItems(); } catch (err) { console.error('Error toggling purchased status:', err); alert(t('shopping.toggleError')); } }; const handleDeleteItem = (itemId: number) => { setConfirmAction({ type: 'delete-item', itemId }); }; const executeDeleteItem = async (itemId: number) => { try { await shoppingItemApi.delete(familyId, itemId); loadItems(); } catch (err) { console.error('Error deleting item:', err); alert(t('shopping.deleteError')); } }; const handleStartEdit = (item: ShoppingItem) => { setEditingId(item.id); setEditingName(item.name); }; const handleSaveEdit = async (itemId: number) => { if (!editingName.trim()) return; try { await shoppingItemApi.update(familyId, itemId, { name: editingName }); setEditingId(null); setEditingName(''); loadItems(); } catch (err) { console.error('Error updating item:', err); alert(t('shopping.updateError')); } }; const handleCancelEdit = () => { setEditingId(null); setEditingName(''); }; const handleMarkAllPurchased = () => { setConfirmAction({ type: 'mark-all' }); }; const executeMarkAllPurchased = async () => { try { await shoppingItemApi.markAllAsPurchased(familyId); loadItems(); } catch (err) { console.error('Error marking all as purchased:', err); alert(t('shopping.markAllError')); } }; const handleClearAll = () => { setConfirmAction({ type: 'clear-all' }); }; const executeClearAll = async () => { try { await shoppingItemApi.clearAll(familyId); loadItems(); } catch (err) { console.error('Error clearing all items:', err); alert(t('shopping.clearError')); } }; const handleConfirm = () => { if (!confirmAction) return; switch (confirmAction.type) { case 'delete-item': executeDeleteItem(confirmAction.itemId); break; case 'mark-all': executeMarkAllPurchased(); break; case 'clear-all': executeClearAll(); break; } setConfirmAction(null); }; const getConfirmModalContent = () => { if (!confirmAction) return null; switch (confirmAction.type) { case 'delete-item': return { title: t('confirm.deleteItem'), message: t('confirm.deleteItemMessage'), confirmText: t('common.delete'), }; case 'mark-all': return { title: t('confirm.markAll'), message: t('confirm.markAllMessage'), confirmText: t('confirm.markButton'), variant: 'info' as const, }; case 'clear-all': return { title: t('confirm.clearAll'), message: t('confirm.clearAllMessage'), confirmText: t('shopping.clear'), }; } }; const unpurchasedItems = items.filter(item => !item.is_purchased); const purchasedItems = items.filter(item => item.is_purchased); const confirmContent = getConfirmModalContent(); return ( <>

{t('shopping.title')}

setNewItemName(e.target.value)} onKeyPress={(e) => e.key === 'Enter' && handleAddItem()} className="flex-1 px-4 py-3 border-2 border-gray-300 rounded-2xl focus:border-green-500 focus:ring-2 focus:ring-green-200 transition-all" />
{loading ? (
) : (
{unpurchasedItems.length > 0 && (

{t('shopping.toBuy')}

{unpurchasedItems.map((item) => (
{editingId === item.id ? (
setEditingName(e.target.value)} onKeyPress={(e) => e.key === 'Enter' && handleSaveEdit(item.id)} className="flex-1 px-3 py-2 border-2 border-green-300 rounded-xl focus:border-green-500 focus:ring-2 focus:ring-green-200" autoFocus />
) : (
)}
))}
)} {purchasedItems.length > 0 && (

{t('shopping.purchased')}

{purchasedItems.map((item) => (
{item.name}
))}
)} {items.length === 0 && (

{t('shopping.empty')}

)}
)}
{items.length > 0 && (
)}
{confirmContent && ( setConfirmAction(null)} variant={confirmContent.variant || 'danger'} /> )} ); }