'use client'; import { useEffect, useState } from 'react'; import { useRouter } from 'next/navigation'; import AdminGuard from '@/components/admin-guard'; import { authApi, wishlistsApi, itemsApi, settingsApi, type Wishlist, type Settings } from '@/lib/api'; import Header from '@/components/header'; import Link from 'next/link'; import StatsGrid from '@/components/admin/StatsGrid'; import SettingsSection from '@/components/admin/SettingsSection'; import WishlistCard from '@/components/admin/WishlistCard'; import CreateWishlistModal from '@/components/admin/CreateWishlistModal'; export default function AdminPage() { return ( ); } function AdminPageContent() { const router = useRouter(); const [wishlists, setWishlists] = useState([]); const [itemCounts, setItemCounts] = useState>({}); const [isLoading, setIsLoading] = useState(true); const [settings, setSettings] = useState({ siteTitle: 'Wishlist', homepageSubtext: 'Browse and explore available wishlists', }); const logout = async () => { await authApi.logout(); router.push('/'); }; const [showCreateModal, setShowCreateModal] = useState(false); const [createError, setCreateError] = useState(''); useEffect(() => { fetchWishlists(); fetchSettings(); }, []); const fetchSettings = async () => { try { const data = await settingsApi.getSettings(); setSettings(data); } catch (error) { console.error('Failed to fetch settings:', error); } }; const fetchWishlists = async () => { try { const data = await wishlistsApi.getAll(); setWishlists(data); // Fetch item counts for each wishlist const counts: Record = {}; await Promise.all( data.map(async (w) => { const items = await itemsApi.getAll(w.id); counts[w.id] = items.length; }) ); setItemCounts(counts); } catch (error) { console.error('Failed to fetch wishlists:', error); } finally { setIsLoading(false); } }; const handleUpdateSettings = async (updatedSettings: Settings) => { await settingsApi.updateSettings(updatedSettings); setSettings(updatedSettings); }; const handleCreateWishlist = async (data: any) => { setCreateError(''); try { await wishlistsApi.create(data); setShowCreateModal(false); fetchWishlists(); } catch (error: any) { setCreateError(error.message || 'Failed to create wishlist'); throw error; } }; const handleUpdateWishlist = async (id: string, data: Partial) => { await wishlistsApi.update(id, data); fetchWishlists(); }; const handleDeleteWishlist = async (id: string) => { if (!confirm('Are you sure you want to delete this wishlist?')) return; try { await wishlistsApi.delete(id); fetchWishlists(); } catch (error) { alert('Failed to delete wishlist'); } }; const handleMoveWishlistUp = async (wishlistId: string) => { const currentIndex = wishlists.findIndex((w) => w.id === wishlistId); if (currentIndex <= 0) return; try { await wishlistsApi.reorder(wishlistId, currentIndex - 1); await fetchWishlists(); } catch (error: any) { alert(`Error: ${error?.message || 'Failed to reorder wishlist'}`); } }; const handleMoveWishlistDown = async (wishlistId: string) => { const currentIndex = wishlists.findIndex((w) => w.id === wishlistId); if (currentIndex === -1 || currentIndex === wishlists.length - 1) return; try { await wishlistsApi.reorder(wishlistId, currentIndex + 1); await fetchWishlists(); } catch (error: any) { alert(`Error: ${error?.message || 'Failed to reorder wishlist'}`); } }; const stats = { totalWishlists: wishlists.length, publicWishlists: wishlists.filter((w) => w.isPublic).length, totalItems: Object.values(itemCounts).reduce((sum, count) => sum + count, 0), }; return ( <>
Reservas Convidados } />
{isLoading ? (

Loading...

) : ( <> {/* Stats Grid */} {/* Settings Section */} {/* Wishlists Section */}

Your Wishlists

{wishlists.length === 0 ? (

No wishlists yet

) : (
{wishlists.map((wishlist, index) => ( ))}
)}
)}
{/* Create Modal */} { setShowCreateModal(false); setCreateError(''); }} onCreate={handleCreateWishlist} error={createError} />
); }