Compare commits
2 Commits
96d38301c4
...
4e031a9d4d
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4e031a9d4d | ||
|
|
007aa35521 |
82
app/admin/claims/page.tsx
Normal file
82
app/admin/claims/page.tsx
Normal file
@@ -0,0 +1,82 @@
|
||||
'use client';
|
||||
|
||||
import { useEffect, useMemo, useState } from 'react';
|
||||
import AdminGuard from '@/components/admin-guard';
|
||||
import { claimsApi, type AdminClaim } from '@/lib/api';
|
||||
|
||||
export default function AdminClaimsPage() {
|
||||
return (
|
||||
<AdminGuard>
|
||||
<ClaimsView />
|
||||
</AdminGuard>
|
||||
);
|
||||
}
|
||||
|
||||
function ClaimsView() {
|
||||
const [claims, setClaims] = useState<AdminClaim[]>([]);
|
||||
const [loading, setLoading] = useState(true);
|
||||
|
||||
useEffect(() => {
|
||||
claimsApi.list().then((c) => setClaims(c)).finally(() => setLoading(false));
|
||||
}, []);
|
||||
|
||||
const totals = useMemo(() => {
|
||||
const byItem: Record<string, { reserved: number; total: number }> = {};
|
||||
for (const c of claims) {
|
||||
const k = c.itemId;
|
||||
if (!byItem[k]) byItem[k] = { reserved: 0, total: c.itemQuantity };
|
||||
byItem[k].reserved += c.quantity;
|
||||
}
|
||||
return byItem;
|
||||
}, [claims]);
|
||||
|
||||
if (loading) {
|
||||
return <div className="min-h-screen flex items-center justify-center text-gray-500">Carregando…</div>;
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="max-w-5xl mx-auto p-6 space-y-6">
|
||||
<div className="flex items-center justify-between">
|
||||
<h1 className="text-2xl font-bold">Reservas</h1>
|
||||
<div className="text-sm text-gray-500">{claims.length} no total</div>
|
||||
</div>
|
||||
|
||||
{claims.length === 0 ? (
|
||||
<div className="border rounded p-6 text-center text-gray-500">Nenhuma reserva ainda.</div>
|
||||
) : (
|
||||
<div className="overflow-x-auto border rounded">
|
||||
<table className="w-full text-sm">
|
||||
<thead className="bg-gray-50 dark:bg-gray-800">
|
||||
<tr className="text-left">
|
||||
<th className="p-3">Item</th>
|
||||
<th className="p-3">Qtd</th>
|
||||
<th className="p-3">Convidado</th>
|
||||
<th className="p-3">Nota</th>
|
||||
<th className="p-3">Quando</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{claims.map((c) => {
|
||||
const t = totals[c.itemId];
|
||||
return (
|
||||
<tr key={c.claimId} className="border-t">
|
||||
<td className="p-3">
|
||||
<div className="font-medium">{c.itemName}</div>
|
||||
<div className="text-xs text-gray-500">
|
||||
{c.wishlistName} · {t.reserved}/{t.total} reservados
|
||||
</div>
|
||||
</td>
|
||||
<td className="p-3">{c.quantity}</td>
|
||||
<td className="p-3">{c.guestName}</td>
|
||||
<td className="p-3 italic text-gray-600 dark:text-gray-400">{c.note ?? '—'}</td>
|
||||
<td className="p-3 text-gray-500">{new Date(c.claimedAt).toLocaleString('pt-BR')}</td>
|
||||
</tr>
|
||||
);
|
||||
})}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -10,7 +10,6 @@ import StatsGrid from '@/components/admin/StatsGrid';
|
||||
import SettingsSection from '@/components/admin/SettingsSection';
|
||||
import WishlistCard from '@/components/admin/WishlistCard';
|
||||
import CreateWishlistModal from '@/components/admin/CreateWishlistModal';
|
||||
import ShareButton from '@/components/share-button';
|
||||
|
||||
export default function AdminPage() {
|
||||
return (
|
||||
@@ -143,16 +142,11 @@ function AdminPageContent() {
|
||||
subtitle="Manage your wishlists and items"
|
||||
actions={
|
||||
<>
|
||||
<ShareButton
|
||||
title="Check out my wishlist site!"
|
||||
text="I wanted to share my wishlist site with you."
|
||||
url="https://wishlist.tieso.co/"
|
||||
/>
|
||||
<Link
|
||||
href="/"
|
||||
href="/admin/claims"
|
||||
className="inline-flex items-center px-6 py-3 border-2 border-indigo-600 dark:border-indigo-500 text-base font-semibold rounded-lg text-indigo-600 dark:text-indigo-400 bg-white dark:bg-gray-800 hover:bg-indigo-50 dark:hover:bg-gray-700 transition-all"
|
||||
>
|
||||
View Public Site
|
||||
Reservas
|
||||
</Link>
|
||||
<Link
|
||||
href="/admin/guests"
|
||||
|
||||
33
app/api/admin/claims/route.ts
Normal file
33
app/api/admin/claims/route.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { desc, eq } from 'drizzle-orm';
|
||||
import { db, itemClaims, wishlistItems, wishlists, guests } from '@/lib/db';
|
||||
import { verifyAdminToken } from '@/lib/auth/tokens';
|
||||
|
||||
export async function GET(request: NextRequest) {
|
||||
if (!verifyAdminToken(request)) {
|
||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
||||
}
|
||||
|
||||
const rows = await db
|
||||
.select({
|
||||
claimId: itemClaims.id,
|
||||
quantity: itemClaims.quantity,
|
||||
note: itemClaims.note,
|
||||
isPurchased: itemClaims.isPurchased,
|
||||
claimedAt: itemClaims.claimedAt,
|
||||
itemId: wishlistItems.id,
|
||||
itemName: wishlistItems.name,
|
||||
itemQuantity: wishlistItems.quantity,
|
||||
wishlistSlug: wishlists.slug,
|
||||
wishlistName: wishlists.name,
|
||||
guestId: guests.id,
|
||||
guestName: guests.name,
|
||||
})
|
||||
.from(itemClaims)
|
||||
.innerJoin(wishlistItems, eq(itemClaims.itemId, wishlistItems.id))
|
||||
.innerJoin(wishlists, eq(wishlistItems.wishlistId, wishlists.id))
|
||||
.innerJoin(guests, eq(itemClaims.guestId, guests.id))
|
||||
.orderBy(desc(itemClaims.claimedAt));
|
||||
|
||||
return NextResponse.json({ success: true, claims: rows });
|
||||
}
|
||||
@@ -1,79 +0,0 @@
|
||||
'use client';
|
||||
|
||||
import { useState } from 'react';
|
||||
|
||||
interface ShareButtonProps {
|
||||
title?: string;
|
||||
text?: string;
|
||||
url?: string;
|
||||
className?: string;
|
||||
}
|
||||
|
||||
export default function ShareButton({
|
||||
title = 'Veja a lista do chá de bebê!',
|
||||
text = 'Dê uma olhada na lista de presentes do chá de bebê.',
|
||||
url,
|
||||
className = ''
|
||||
}: ShareButtonProps) {
|
||||
const [isSupported, setIsSupported] = useState(true);
|
||||
|
||||
const handleShare = async () => {
|
||||
// Use current URL if not provided
|
||||
const shareUrl = url || window.location.href;
|
||||
|
||||
// Check if Web Share API is supported
|
||||
if (!navigator.share) {
|
||||
setIsSupported(false);
|
||||
// Fallback: copy to clipboard
|
||||
try {
|
||||
await navigator.clipboard.writeText(shareUrl);
|
||||
alert('Link copiado!');
|
||||
} catch (err) {
|
||||
console.error('Failed to copy:', err);
|
||||
alert('Não foi possível compartilhar');
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
await navigator.share({
|
||||
title,
|
||||
text,
|
||||
url: shareUrl,
|
||||
});
|
||||
} catch (err: any) {
|
||||
// User cancelled or share failed
|
||||
if (err.name !== 'AbortError') {
|
||||
console.error('Error sharing:', err);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
if (!isSupported && typeof window !== 'undefined' && !navigator.clipboard) {
|
||||
// Don't show button if neither share nor clipboard is supported
|
||||
return null;
|
||||
}
|
||||
|
||||
return (
|
||||
<button
|
||||
onClick={handleShare}
|
||||
className={`inline-flex items-center px-6 py-3 border-2 border-indigo-600 dark:border-indigo-500 text-base font-semibold rounded-lg text-indigo-600 dark:text-indigo-400 bg-white dark:bg-gray-800 hover:bg-indigo-50 dark:hover:bg-gray-700 transition-all cursor-pointer ${className}`}
|
||||
title="Compartilhar"
|
||||
>
|
||||
<svg
|
||||
className="w-5 h-5 mr-2"
|
||||
fill="none"
|
||||
stroke="currentColor"
|
||||
viewBox="0 0 24 24"
|
||||
>
|
||||
<path
|
||||
strokeLinecap="round"
|
||||
strokeLinejoin="round"
|
||||
strokeWidth={2}
|
||||
d="M8.684 13.342C8.886 12.938 9 12.482 9 12c0-.482-.114-.938-.316-1.342m0 2.684a3 3 0 110-2.684m0 2.684l6.632 3.316m-6.632-6l6.632-3.316m0 0a3 3 0 105.367-2.684 3 3 0 00-5.367 2.684zm0 9.316a3 3 0 105.368 2.684 3 3 0 00-5.368-2.684z"
|
||||
/>
|
||||
</svg>
|
||||
Compartilhar
|
||||
</button>
|
||||
);
|
||||
}
|
||||
23
lib/api.ts
23
lib/api.ts
@@ -96,6 +96,29 @@ export const guestsApi = {
|
||||
},
|
||||
};
|
||||
|
||||
export interface AdminClaim {
|
||||
claimId: string;
|
||||
quantity: number;
|
||||
note: string | null;
|
||||
isPurchased: boolean;
|
||||
claimedAt: string;
|
||||
itemId: string;
|
||||
itemName: string;
|
||||
itemQuantity: number;
|
||||
wishlistSlug: string;
|
||||
wishlistName: string;
|
||||
guestId: string;
|
||||
guestName: string;
|
||||
}
|
||||
|
||||
export const claimsApi = {
|
||||
async list() {
|
||||
const response = await fetch(`${API_BASE_URL}/admin/claims`, { credentials: 'include' });
|
||||
const data = await handleResponse<{ success: true; claims: AdminClaim[] }>(response);
|
||||
return data.claims;
|
||||
},
|
||||
};
|
||||
|
||||
// Wishlist types
|
||||
export interface Wishlist {
|
||||
id: string;
|
||||
|
||||
Reference in New Issue
Block a user