feat: simplify to single-wishlist instance
Some checks failed
Build and Push Docker Image / build-and-push (push) Has been cancelled
Some checks failed
Build and Push Docker Image / build-and-push (push) Has been cancelled
- Home page redirects guests directly to the single wishlist slug - Admin shows settings + single wishlist header + items only - Removed multi-wishlist create/delete/reorder UI Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -3,13 +3,22 @@
|
|||||||
import { useEffect, useState } from 'react';
|
import { useEffect, useState } from 'react';
|
||||||
import { useRouter } from 'next/navigation';
|
import { useRouter } from 'next/navigation';
|
||||||
import AdminGuard from '@/components/admin-guard';
|
import AdminGuard from '@/components/admin-guard';
|
||||||
import { authApi, wishlistsApi, itemsApi, settingsApi, type Wishlist, type Settings } from '@/lib/api';
|
import {
|
||||||
|
authApi,
|
||||||
|
wishlistsApi,
|
||||||
|
itemsApi,
|
||||||
|
settingsApi,
|
||||||
|
type Wishlist,
|
||||||
|
type Item,
|
||||||
|
type Settings,
|
||||||
|
} from '@/lib/api';
|
||||||
import Header from '@/components/header';
|
import Header from '@/components/header';
|
||||||
import Link from 'next/link';
|
import Link from 'next/link';
|
||||||
import StatsGrid from '@/components/admin/StatsGrid';
|
|
||||||
import SettingsSection from '@/components/admin/SettingsSection';
|
import SettingsSection from '@/components/admin/SettingsSection';
|
||||||
import WishlistCard from '@/components/admin/WishlistCard';
|
import ItemCard from '@/components/admin/ItemCard';
|
||||||
import CreateWishlistModal from '@/components/admin/CreateWishlistModal';
|
import ItemForm from '@/components/admin/ItemForm';
|
||||||
|
import ImageUpload from '@/components/image-upload';
|
||||||
|
import RichTextEditor from '@/components/RichTextEditor';
|
||||||
|
|
||||||
export default function AdminPage() {
|
export default function AdminPage() {
|
||||||
return (
|
return (
|
||||||
@@ -21,53 +30,154 @@ export default function AdminPage() {
|
|||||||
|
|
||||||
function AdminPageContent() {
|
function AdminPageContent() {
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
const [wishlists, setWishlists] = useState<Wishlist[]>([]);
|
|
||||||
const [itemCounts, setItemCounts] = useState<Record<string, number>>({});
|
// Single wishlist state
|
||||||
|
const [wishlist, setWishlist] = useState<Wishlist | null>(null);
|
||||||
|
const [items, setItems] = useState<Item[]>([]);
|
||||||
const [isLoading, setIsLoading] = useState(true);
|
const [isLoading, setIsLoading] = useState(true);
|
||||||
|
|
||||||
|
// Settings
|
||||||
const [settings, setSettings] = useState<Settings>({
|
const [settings, setSettings] = useState<Settings>({
|
||||||
siteTitle: 'Wishlist',
|
siteTitle: 'Wishlist',
|
||||||
homepageSubtext: 'Browse and explore available wishlists',
|
homepageSubtext: '',
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Wishlist edit state
|
||||||
|
const [isEditingWishlist, setIsEditingWishlist] = useState(false);
|
||||||
|
const [editForm, setEditForm] = useState({
|
||||||
|
name: '',
|
||||||
|
slug: '',
|
||||||
|
description: '',
|
||||||
|
preferences: '',
|
||||||
|
imageUrl: '',
|
||||||
|
isPublic: true,
|
||||||
|
});
|
||||||
|
const [editError, setEditError] = useState('');
|
||||||
|
const [isWishlistImageUploading, setIsWishlistImageUploading] = useState(false);
|
||||||
|
|
||||||
|
// Item management state
|
||||||
|
const [editingItemId, setEditingItemId] = useState<string | null>(null);
|
||||||
|
const [showAddItemForm, setShowAddItemForm] = useState(false);
|
||||||
|
const [newItemError, setNewItemError] = useState('');
|
||||||
|
|
||||||
const logout = async () => {
|
const logout = async () => {
|
||||||
await authApi.logout();
|
await authApi.logout();
|
||||||
router.push('/');
|
router.push('/');
|
||||||
};
|
};
|
||||||
const [showCreateModal, setShowCreateModal] = useState(false);
|
|
||||||
const [createError, setCreateError] = useState('');
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
fetchWishlists();
|
fetchData();
|
||||||
fetchSettings();
|
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
const fetchSettings = async () => {
|
const fetchData = async () => {
|
||||||
try {
|
try {
|
||||||
const data = await settingsApi.getSettings();
|
const [lists, settingsData] = await Promise.all([
|
||||||
setSettings(data);
|
wishlistsApi.getAll(),
|
||||||
|
settingsApi.getSettings(),
|
||||||
|
]);
|
||||||
|
setSettings(settingsData);
|
||||||
|
if (lists.length > 0) {
|
||||||
|
const w = lists[0];
|
||||||
|
setWishlist(w);
|
||||||
|
const itemsData = await itemsApi.getAll(w.id);
|
||||||
|
setItems(itemsData.sort((a, b) => a.sortOrder - b.sortOrder));
|
||||||
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Failed to fetch settings:', error);
|
console.error('Failed to fetch data:', error);
|
||||||
|
} finally {
|
||||||
|
setIsLoading(false);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const fetchWishlists = async () => {
|
const refreshItems = async () => {
|
||||||
try {
|
if (!wishlist) return;
|
||||||
const data = await wishlistsApi.getAll();
|
const itemsData = await itemsApi.getAll(wishlist.id);
|
||||||
setWishlists(data);
|
setItems(itemsData.sort((a, b) => a.sortOrder - b.sortOrder));
|
||||||
|
};
|
||||||
|
|
||||||
// Fetch item counts for each wishlist
|
// Wishlist edit handlers
|
||||||
const counts: Record<string, number> = {};
|
const startEditingWishlist = () => {
|
||||||
await Promise.all(
|
if (!wishlist) return;
|
||||||
data.map(async (w) => {
|
setEditForm({
|
||||||
const items = await itemsApi.getAll(w.id);
|
name: wishlist.name,
|
||||||
counts[w.id] = items.length;
|
slug: wishlist.slug,
|
||||||
})
|
description: wishlist.description || '',
|
||||||
);
|
preferences: wishlist.preferences || '',
|
||||||
setItemCounts(counts);
|
imageUrl: wishlist.imageUrl || '',
|
||||||
} catch (error) {
|
isPublic: wishlist.isPublic,
|
||||||
console.error('Failed to fetch wishlists:', error);
|
});
|
||||||
} finally {
|
setEditError('');
|
||||||
setIsLoading(false);
|
setIsEditingWishlist(true);
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleUpdateWishlist = async (e: React.FormEvent) => {
|
||||||
|
e.preventDefault();
|
||||||
|
if (!wishlist) return;
|
||||||
|
setEditError('');
|
||||||
|
try {
|
||||||
|
const updated = await wishlistsApi.update(wishlist.id, editForm);
|
||||||
|
setWishlist(updated);
|
||||||
|
setIsEditingWishlist(false);
|
||||||
|
} catch (error: any) {
|
||||||
|
setEditError(error.message || 'Failed to update wishlist');
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Item handlers
|
||||||
|
const handleCreateItem = async (itemData: Partial<Item>) => {
|
||||||
|
if (!wishlist) return;
|
||||||
|
setNewItemError('');
|
||||||
|
try {
|
||||||
|
await itemsApi.create(wishlist.id, itemData);
|
||||||
|
setShowAddItemForm(false);
|
||||||
|
await refreshItems();
|
||||||
|
} catch (error: any) {
|
||||||
|
setNewItemError(error.message || 'Failed to create item');
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleUpdateItem = async (itemData: Partial<Item>) => {
|
||||||
|
if (!editingItemId) return;
|
||||||
|
try {
|
||||||
|
await itemsApi.update(editingItemId, itemData);
|
||||||
|
setEditingItemId(null);
|
||||||
|
await refreshItems();
|
||||||
|
} catch (error: any) {
|
||||||
|
alert(error.message || 'Failed to update item');
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleDeleteItem = async (itemId: string) => {
|
||||||
|
if (!confirm('Tem certeza que deseja excluir este item?')) return;
|
||||||
|
try {
|
||||||
|
await itemsApi.delete(itemId);
|
||||||
|
await refreshItems();
|
||||||
|
} catch {
|
||||||
|
alert('Failed to delete item');
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleMoveItemUp = async (itemId: string) => {
|
||||||
|
const currentIndex = items.findIndex((item) => item.id === itemId);
|
||||||
|
if (currentIndex <= 0) return;
|
||||||
|
try {
|
||||||
|
await itemsApi.reorder(itemId, currentIndex - 1);
|
||||||
|
await refreshItems();
|
||||||
|
} catch (error: any) {
|
||||||
|
alert(error?.message || 'Failed to reorder item');
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleMoveItemDown = async (itemId: string) => {
|
||||||
|
const currentIndex = items.findIndex((item) => item.id === itemId);
|
||||||
|
if (currentIndex === -1 || currentIndex === items.length - 1) return;
|
||||||
|
try {
|
||||||
|
await itemsApi.reorder(itemId, currentIndex + 1);
|
||||||
|
await refreshItems();
|
||||||
|
} catch (error: any) {
|
||||||
|
alert(error?.message || 'Failed to reorder item');
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -76,70 +186,13 @@ function AdminPageContent() {
|
|||||||
setSettings(updatedSettings);
|
setSettings(updatedSettings);
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleCreateWishlist = async (data: any) => {
|
const editingItem = items.find((item) => item.id === editingItemId);
|
||||||
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<Wishlist>) => {
|
|
||||||
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 (
|
return (
|
||||||
<>
|
|
||||||
<div className="min-h-screen bg-gray-50 dark:bg-gray-900">
|
<div className="min-h-screen bg-gray-50 dark:bg-gray-900">
|
||||||
<Header
|
<Header
|
||||||
title="Dashboard"
|
title="Dashboard"
|
||||||
subtitle="Manage your wishlists and items"
|
subtitle={wishlist ? `${items.length} itens` : undefined}
|
||||||
actions={
|
actions={
|
||||||
<>
|
<>
|
||||||
<Link
|
<Link
|
||||||
@@ -172,76 +225,212 @@ function AdminPageContent() {
|
|||||||
<div className="px-4 sm:px-0">
|
<div className="px-4 sm:px-0">
|
||||||
{isLoading ? (
|
{isLoading ? (
|
||||||
<div className="text-center py-12">
|
<div className="text-center py-12">
|
||||||
<p className="text-gray-600 dark:text-gray-400">Loading...</p>
|
<p className="text-gray-600 dark:text-gray-400">Carregando...</p>
|
||||||
</div>
|
</div>
|
||||||
) : (
|
) : (
|
||||||
<>
|
<>
|
||||||
{/* Stats Grid */}
|
|
||||||
<StatsGrid stats={stats} />
|
|
||||||
|
|
||||||
{/* Settings Section */}
|
{/* Settings Section */}
|
||||||
<SettingsSection settings={settings} onUpdate={handleUpdateSettings} />
|
<SettingsSection settings={settings} onUpdate={handleUpdateSettings} />
|
||||||
|
|
||||||
{/* Wishlists Section */}
|
{/* Wishlist Info Section */}
|
||||||
|
{wishlist && (
|
||||||
|
<div className="mb-6 bg-white dark:bg-gray-800 rounded-lg shadow-sm border border-gray-200 dark:border-gray-700 overflow-hidden">
|
||||||
|
<div className="p-5">
|
||||||
|
{isEditingWishlist ? (
|
||||||
|
<form onSubmit={handleUpdateWishlist} className="space-y-4">
|
||||||
|
<h2 className="text-xl font-bold text-gray-900 dark:text-white mb-4">
|
||||||
|
Editar Lista
|
||||||
|
</h2>
|
||||||
|
{editError && (
|
||||||
|
<div className="p-3 bg-red-50 dark:bg-red-900/20 text-red-800 dark:text-red-400 rounded-lg text-base">
|
||||||
|
{editError}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
<ImageUpload
|
||||||
|
currentImageUrl={editForm.imageUrl}
|
||||||
|
onImageChange={(url) =>
|
||||||
|
setEditForm((prev) => ({ ...prev, imageUrl: url }))
|
||||||
|
}
|
||||||
|
onUploadStateChange={setIsWishlistImageUploading}
|
||||||
|
type="wishlist"
|
||||||
|
label="Imagem da Lista"
|
||||||
|
/>
|
||||||
|
<div>
|
||||||
|
<label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">
|
||||||
|
Nome *
|
||||||
|
</label>
|
||||||
|
<input
|
||||||
|
type="text"
|
||||||
|
required
|
||||||
|
value={editForm.name}
|
||||||
|
onChange={(e) =>
|
||||||
|
setEditForm((prev) => ({ ...prev, name: e.target.value }))
|
||||||
|
}
|
||||||
|
className="w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-lg focus:outline-none focus:ring-2 focus:ring-indigo-500 dark:bg-gray-700 dark:text-white"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">
|
||||||
|
Descrição
|
||||||
|
</label>
|
||||||
|
<textarea
|
||||||
|
value={editForm.description}
|
||||||
|
onChange={(e) =>
|
||||||
|
setEditForm((prev) => ({ ...prev, description: e.target.value }))
|
||||||
|
}
|
||||||
|
className="w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-lg focus:outline-none focus:ring-2 focus:ring-indigo-500 dark:bg-gray-700 dark:text-white"
|
||||||
|
rows={2}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">
|
||||||
|
Preferências
|
||||||
|
</label>
|
||||||
|
<RichTextEditor
|
||||||
|
value={editForm.preferences}
|
||||||
|
onChange={(html) =>
|
||||||
|
setEditForm((prev) => ({ ...prev, preferences: html }))
|
||||||
|
}
|
||||||
|
placeholder="Interesses e preferências gerais..."
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<div className="flex gap-3 justify-end">
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={() => setIsEditingWishlist(false)}
|
||||||
|
className="px-4 py-2 border border-gray-300 dark:border-gray-600 rounded-lg text-gray-700 dark:text-gray-300 hover:bg-gray-50 dark:hover:bg-gray-700 cursor-pointer"
|
||||||
|
>
|
||||||
|
Cancelar
|
||||||
|
</button>
|
||||||
|
<button
|
||||||
|
type="submit"
|
||||||
|
disabled={isWishlistImageUploading}
|
||||||
|
className="px-4 py-2 bg-indigo-600 text-white rounded-lg hover:bg-indigo-700 disabled:opacity-50 cursor-pointer"
|
||||||
|
>
|
||||||
|
{isWishlistImageUploading ? 'Enviando...' : 'Salvar'}
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
) : (
|
||||||
|
<div className="flex items-start gap-4">
|
||||||
|
{wishlist.imageUrl && (
|
||||||
|
<img
|
||||||
|
src={wishlist.imageUrl}
|
||||||
|
alt={wishlist.name}
|
||||||
|
className="w-20 h-20 object-cover rounded border border-gray-200 dark:border-gray-600 flex-shrink-0"
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
<div className="flex-1">
|
||||||
|
<h2 className="text-xl font-bold text-gray-900 dark:text-white">
|
||||||
|
{wishlist.name}
|
||||||
|
</h2>
|
||||||
|
{wishlist.description && (
|
||||||
|
<p className="text-sm text-gray-600 dark:text-gray-400 mt-1">
|
||||||
|
{wishlist.description}
|
||||||
|
</p>
|
||||||
|
)}
|
||||||
|
<p className="text-sm text-gray-500 dark:text-gray-500 mt-1">
|
||||||
|
/{wishlist.slug}
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
<button
|
||||||
|
onClick={startEditingWishlist}
|
||||||
|
className="flex-shrink-0 px-4 py-2 border border-gray-300 dark:border-gray-600 rounded-lg text-gray-700 dark:text-gray-300 hover:bg-gray-50 dark:hover:bg-gray-700 text-sm cursor-pointer"
|
||||||
|
>
|
||||||
|
Editar
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{/* Items Section */}
|
||||||
|
{wishlist && (
|
||||||
<div className="mb-6">
|
<div className="mb-6">
|
||||||
<div className="flex items-center justify-between mb-4">
|
<div className="flex items-center justify-between mb-4">
|
||||||
<h2 className="text-2xl font-bold text-gray-900 dark:text-white">
|
<h2 className="text-2xl font-bold text-gray-900 dark:text-white">
|
||||||
Your Wishlists
|
Itens ({items.length})
|
||||||
</h2>
|
</h2>
|
||||||
<button
|
<button
|
||||||
onClick={() => setShowCreateModal(true)}
|
onClick={() => {
|
||||||
|
setShowAddItemForm(true);
|
||||||
|
setNewItemError('');
|
||||||
|
}}
|
||||||
className="inline-flex items-center px-6 py-3 border border-transparent text-base font-semibold rounded-lg text-white bg-indigo-600 hover:bg-indigo-700 shadow-md hover:shadow-lg transition-all cursor-pointer"
|
className="inline-flex items-center px-6 py-3 border border-transparent text-base font-semibold rounded-lg text-white bg-indigo-600 hover:bg-indigo-700 shadow-md hover:shadow-lg transition-all cursor-pointer"
|
||||||
>
|
>
|
||||||
+ Create Wishlist
|
+ Adicionar Item
|
||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
{wishlists.length === 0 ? (
|
|
||||||
|
{/* Add Item Form */}
|
||||||
|
{showAddItemForm && (
|
||||||
|
<ItemForm
|
||||||
|
mode="create"
|
||||||
|
onSubmit={handleCreateItem}
|
||||||
|
onCancel={() => {
|
||||||
|
setShowAddItemForm(false);
|
||||||
|
setNewItemError('');
|
||||||
|
}}
|
||||||
|
error={newItemError}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{items.length === 0 && !showAddItemForm ? (
|
||||||
<div className="text-center py-12 bg-white dark:bg-gray-800 rounded-lg border border-gray-200 dark:border-gray-700">
|
<div className="text-center py-12 bg-white dark:bg-gray-800 rounded-lg border border-gray-200 dark:border-gray-700">
|
||||||
<p className="text-gray-500 dark:text-gray-400 mb-6 text-lg">
|
<p className="text-gray-500 dark:text-gray-400 mb-6 text-lg">
|
||||||
No wishlists yet
|
Nenhum item ainda
|
||||||
</p>
|
</p>
|
||||||
<button
|
<button
|
||||||
onClick={() => setShowCreateModal(true)}
|
onClick={() => {
|
||||||
className="inline-flex items-center px-6 py-3 border border-transparent text-base font-semibold rounded-lg text-white bg-indigo-600 hover:bg-indigo-700 shadow-md hover:shadow-lg transition-all"
|
setShowAddItemForm(true);
|
||||||
|
setNewItemError('');
|
||||||
|
}}
|
||||||
|
className="inline-flex items-center px-6 py-3 border border-transparent text-base font-semibold rounded-lg text-white bg-indigo-600 hover:bg-indigo-700 shadow-md hover:shadow-lg transition-all cursor-pointer"
|
||||||
>
|
>
|
||||||
Create Your First Wishlist
|
Adicionar Primeiro Item
|
||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
) : (
|
) : (
|
||||||
<div className="space-y-3">
|
<div className="space-y-3">
|
||||||
{wishlists.map((wishlist, index) => (
|
{items.map((item, index) => (
|
||||||
<WishlistCard
|
<div key={item.id}>
|
||||||
key={wishlist.id}
|
{editingItemId === item.id ? (
|
||||||
wishlist={wishlist}
|
<ItemForm
|
||||||
itemCount={itemCounts[wishlist.id] || 0}
|
mode="edit"
|
||||||
onUpdate={handleUpdateWishlist}
|
item={editingItem}
|
||||||
onDelete={handleDeleteWishlist}
|
onSubmit={handleUpdateItem}
|
||||||
onMoveUp={handleMoveWishlistUp}
|
onCancel={() => setEditingItemId(null)}
|
||||||
onMoveDown={handleMoveWishlistDown}
|
|
||||||
isFirst={index === 0}
|
|
||||||
isLast={index === wishlists.length - 1}
|
|
||||||
onItemsChange={fetchWishlists}
|
|
||||||
/>
|
/>
|
||||||
|
) : (
|
||||||
|
<ItemCard
|
||||||
|
item={item}
|
||||||
|
onEdit={() => setEditingItemId(item.id)}
|
||||||
|
onDelete={() => handleDeleteItem(item.id)}
|
||||||
|
onMoveUp={() => handleMoveItemUp(item.id)}
|
||||||
|
onMoveDown={() => handleMoveItemDown(item.id)}
|
||||||
|
isFirst={index === 0}
|
||||||
|
isLast={index === items.length - 1}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
))}
|
))}
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{!wishlist && !isLoading && (
|
||||||
|
<div className="text-center py-12 bg-white dark:bg-gray-800 rounded-lg border border-gray-200 dark:border-gray-700">
|
||||||
|
<p className="text-gray-500 dark:text-gray-400 text-lg">
|
||||||
|
Nenhuma lista encontrada no banco de dados.
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
</>
|
</>
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{/* Create Modal */}
|
|
||||||
<CreateWishlistModal
|
|
||||||
isOpen={showCreateModal}
|
|
||||||
onClose={() => {
|
|
||||||
setShowCreateModal(false);
|
|
||||||
setCreateError('');
|
|
||||||
}}
|
|
||||||
onCreate={handleCreateWishlist}
|
|
||||||
error={createError}
|
|
||||||
/>
|
|
||||||
</div>
|
</div>
|
||||||
</>
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
57
app/page.tsx
57
app/page.tsx
@@ -1,14 +1,11 @@
|
|||||||
'use client';
|
'use client';
|
||||||
|
|
||||||
import { useEffect, useState } from 'react';
|
import { useEffect } from 'react';
|
||||||
import Link from 'next/link';
|
|
||||||
import { useRouter } from 'next/navigation';
|
import { useRouter } from 'next/navigation';
|
||||||
import { authApi, wishlistsApi, type Wishlist } from '@/lib/api';
|
import { authApi, wishlistsApi } from '@/lib/api';
|
||||||
|
|
||||||
export default function HomePage() {
|
export default function HomePage() {
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
const [state, setState] = useState<'checking' | 'guest' | 'none'>('checking');
|
|
||||||
const [wishlists, setWishlists] = useState<Wishlist[]>([]);
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
let cancelled = false;
|
let cancelled = false;
|
||||||
@@ -31,65 +28,27 @@ export default function HomePage() {
|
|||||||
router.replace('/admin');
|
router.replace('/admin');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
// For guests (and after usr token auth), redirect to the single wishlist
|
||||||
if (who.role === 'guest') {
|
if (who.role === 'guest') {
|
||||||
const lists = await wishlistsApi.getAllPublic();
|
const lists = await wishlistsApi.getAllPublic();
|
||||||
if (cancelled) return;
|
if (cancelled) return;
|
||||||
if (lists.length === 1) {
|
if (lists.length > 0) {
|
||||||
router.replace(`/${lists[0].slug}`);
|
router.replace(`/${lists[0].slug}`);
|
||||||
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
setWishlists(lists);
|
// Unauthenticated — show a simple landing
|
||||||
setState('guest');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
setState('none');
|
|
||||||
} catch {
|
} catch {
|
||||||
if (!cancelled) setState('none');
|
// ignore
|
||||||
}
|
}
|
||||||
})();
|
})();
|
||||||
return () => { cancelled = true; };
|
return () => { cancelled = true; };
|
||||||
}, [router]);
|
}, [router]);
|
||||||
|
|
||||||
if (state === 'checking') {
|
|
||||||
return <div className="min-h-screen flex items-center justify-center text-gray-500">Carregando…</div>;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (state === 'guest') {
|
|
||||||
return (
|
|
||||||
<div className="min-h-screen flex items-center justify-center px-6">
|
|
||||||
<div className="w-full max-w-2xl text-center">
|
|
||||||
<h1 className="text-3xl font-bold mb-6">Bem-vindo</h1>
|
|
||||||
{wishlists.length === 0 ? (
|
|
||||||
<p className="text-gray-500">Nenhuma lista disponível ainda.</p>
|
|
||||||
) : (
|
|
||||||
<ul className="space-y-3 text-left">
|
|
||||||
{wishlists.map((w) => (
|
|
||||||
<li key={w.id}>
|
|
||||||
<Link
|
|
||||||
href={`/${w.slug}`}
|
|
||||||
className="block rounded-lg border border-gray-200 dark:border-gray-700 p-4 hover:border-indigo-400 hover:bg-indigo-50/30 dark:hover:bg-indigo-900/10 transition"
|
|
||||||
>
|
|
||||||
<div className="font-semibold text-lg">{w.name}</div>
|
|
||||||
{w.description && (
|
|
||||||
<div className="text-sm text-gray-500 mt-1">{w.description}</div>
|
|
||||||
)}
|
|
||||||
</Link>
|
|
||||||
</li>
|
|
||||||
))}
|
|
||||||
</ul>
|
|
||||||
)}
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="min-h-screen flex items-center justify-center text-center px-6">
|
<div className="min-h-screen flex items-center justify-center text-center px-6">
|
||||||
<div className="max-w-xl">
|
<div className="max-w-xl">
|
||||||
<h1 className="text-4xl font-bold mb-3">Lista de Presentes</h1>
|
<p className="text-lg text-gray-500">Carregando…</p>
|
||||||
<p className="text-lg text-gray-500">
|
|
||||||
Gerencie listas de presentes e convites com facilidade.
|
|
||||||
</p>
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
|||||||
Reference in New Issue
Block a user