Admin: gestión de opciones de oro (home_goldprice)
- lib/admin-gold.ts: list/create/delete sobre home_goldprice (id, gold_amount, price). - API: POST /api/admin/gold, DELETE /api/admin/gold/[id] (403 sin admin). - UI: /admin/gold con AdminGoldManager (alta ordenada por cantidad + borrado), enlace desde el índice del admin. - i18n es/en (Admin): manageGold, gold, goldAmount, goldPrice, goldUnit, noGold. Verificado: build OK, /admin/gold 307→login sin sesión, APIs 403 sin admin, columnas confirmadas en la migración Django. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
import { getTranslations, setRequestLocale } from 'next-intl/server'
|
||||
import { redirect } from '@/i18n/navigation'
|
||||
import { getSession } from '@/lib/session'
|
||||
import { isAdmin } from '@/lib/admin'
|
||||
import { listGoldOptions } from '@/lib/admin-gold'
|
||||
import { AdminGoldManager } from '@/components/AdminGoldManager'
|
||||
|
||||
export const dynamic = 'force-dynamic'
|
||||
|
||||
export default async function AdminGoldPage({ params }: { params: Promise<{ locale: string }> }) {
|
||||
const { locale } = await params
|
||||
setRequestLocale(locale)
|
||||
const t = await getTranslations('Admin')
|
||||
const session = await getSession()
|
||||
if (!session.bnetId) redirect({ href: '/login', locale })
|
||||
if (!(await isAdmin(session))) redirect({ href: '/', locale })
|
||||
const options = await listGoldOptions()
|
||||
return (
|
||||
<main className="mx-auto max-w-3xl px-4 py-8">
|
||||
<h1 className="mb-6 text-2xl font-bold text-amber-500">{t('gold')}</h1>
|
||||
<AdminGoldManager initial={options} />
|
||||
</main>
|
||||
)
|
||||
}
|
||||
@@ -32,6 +32,11 @@ export default async function AdminPage({ params }: { params: Promise<{ locale:
|
||||
{t('manageVotes')}
|
||||
</Link>
|
||||
</li>
|
||||
<li>
|
||||
<Link href="/admin/gold" className="text-sky-400 hover:underline">
|
||||
{t('manageGold')}
|
||||
</Link>
|
||||
</li>
|
||||
</ul>
|
||||
</main>
|
||||
)
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
import { getSession } from '@/lib/session'
|
||||
import { isAdmin } from '@/lib/admin'
|
||||
import { deleteGoldOption } from '@/lib/admin-gold'
|
||||
|
||||
export async function DELETE(_request: Request, { params }: { params: Promise<{ id: string }> }) {
|
||||
const session = await getSession()
|
||||
if (!(await isAdmin(session))) return Response.json({ success: false, error: 'forbidden' }, { status: 403 })
|
||||
const { id } = await params
|
||||
await deleteGoldOption(Number(id))
|
||||
return Response.json({ success: true })
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
import { getSession } from '@/lib/session'
|
||||
import { isAdmin } from '@/lib/admin'
|
||||
import { createGoldOption } from '@/lib/admin-gold'
|
||||
|
||||
export async function POST(request: Request) {
|
||||
const session = await getSession()
|
||||
if (!(await isAdmin(session))) return Response.json({ success: false, error: 'forbidden' }, { status: 403 })
|
||||
let b: Record<string, unknown> = {}
|
||||
try { b = await request.json() } catch { return Response.json({ success: false, error: 'invalidRequest' }, { status: 400 }) }
|
||||
const goldAmount = Number(b.goldAmount)
|
||||
const price = Number(b.price)
|
||||
if (!(goldAmount > 0) || !(price >= 0)) return Response.json({ success: false, error: 'emptyError' })
|
||||
const id = await createGoldOption(goldAmount, price)
|
||||
return Response.json({ success: true, id })
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
'use client'
|
||||
|
||||
import { useState } from 'react'
|
||||
import { useTranslations } from 'next-intl'
|
||||
import type { GoldOption } from '@/lib/admin-gold'
|
||||
|
||||
export function AdminGoldManager({ initial }: { initial: GoldOption[] }) {
|
||||
const t = useTranslations('Admin')
|
||||
const [opts, setOpts] = useState(initial)
|
||||
const [form, setForm] = useState({ goldAmount: '', price: '' })
|
||||
const [busy, setBusy] = useState(false)
|
||||
|
||||
function upd(k: string, v: string) {
|
||||
setForm((f) => ({ ...f, [k]: v }))
|
||||
}
|
||||
|
||||
async function create(e: React.FormEvent) {
|
||||
e.preventDefault()
|
||||
const goldAmount = Number(form.goldAmount)
|
||||
const price = Number(form.price)
|
||||
if (busy || !(goldAmount > 0) || !(price >= 0)) return
|
||||
setBusy(true)
|
||||
try {
|
||||
const res = await fetch('/api/admin/gold', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
credentials: 'same-origin',
|
||||
body: JSON.stringify({ goldAmount, price }),
|
||||
})
|
||||
const data: { success?: boolean; id?: number } = await res.json()
|
||||
if (data.success && data.id) {
|
||||
setOpts(
|
||||
[...opts, { id: data.id, gold_amount: goldAmount, price }].sort((a, b) => a.gold_amount - b.gold_amount),
|
||||
)
|
||||
setForm({ goldAmount: '', price: '' })
|
||||
}
|
||||
} finally {
|
||||
setBusy(false)
|
||||
}
|
||||
}
|
||||
|
||||
async function remove(id: number) {
|
||||
if (!confirm(t('confirmDelete'))) return
|
||||
const res = await fetch(`/api/admin/gold/${id}`, { method: 'DELETE', credentials: 'same-origin' })
|
||||
if ((await res.json()).success) setOpts(opts.filter((o) => o.id !== id))
|
||||
}
|
||||
|
||||
const field = 'nw-input'
|
||||
return (
|
||||
<div>
|
||||
<form onSubmit={create} className="mb-8 grid gap-3 nw-card sm:grid-cols-[1fr_1fr_auto] sm:items-end">
|
||||
<label className="text-sm">
|
||||
<span className="mb-1 block text-nw-muted">{t('goldAmount')}</span>
|
||||
<input type="number" min="1" value={form.goldAmount} onChange={(e) => upd('goldAmount', e.target.value)} placeholder="10000" className={field} />
|
||||
</label>
|
||||
<label className="text-sm">
|
||||
<span className="mb-1 block text-nw-muted">{t('goldPrice')}</span>
|
||||
<input type="number" min="0" step="0.01" value={form.price} onChange={(e) => upd('price', e.target.value)} placeholder="4.99" className={field} />
|
||||
</label>
|
||||
<button type="submit" disabled={busy} className="nw-btn disabled:opacity-60">
|
||||
{busy ? t('creating') : t('create')}
|
||||
</button>
|
||||
</form>
|
||||
{opts.length === 0 ? (
|
||||
<p className="text-amber-200/70">{t('noGold')}</p>
|
||||
) : (
|
||||
<ul className="divide-y divide-amber-900/40">
|
||||
{opts.map((o) => (
|
||||
<li key={o.id} className="flex items-center justify-between gap-4 py-3">
|
||||
<div>
|
||||
<p className="font-semibold text-amber-300">{o.gold_amount.toLocaleString()} {t('goldUnit')}</p>
|
||||
<p className="text-xs text-amber-200/50">{o.price.toFixed(2)} €</p>
|
||||
</div>
|
||||
<button onClick={() => remove(o.id)} className="rounded border border-red-900/60 px-3 py-1 text-sm text-red-400 hover:bg-red-950/40">
|
||||
{t('delete')}
|
||||
</button>
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
import type { RowDataPacket, ResultSetHeader } from 'mysql2'
|
||||
import { db, DB } from './db'
|
||||
|
||||
export interface GoldOption {
|
||||
id: number
|
||||
gold_amount: number
|
||||
price: number
|
||||
}
|
||||
|
||||
export async function listGoldOptions(): Promise<GoldOption[]> {
|
||||
const [rows] = await db(DB.default).query<RowDataPacket[]>(
|
||||
'SELECT id, gold_amount, price FROM home_goldprice ORDER BY gold_amount',
|
||||
)
|
||||
return rows.map((r) => ({ id: r.id, gold_amount: r.gold_amount, price: Number(r.price) }))
|
||||
}
|
||||
|
||||
export async function createGoldOption(goldAmount: number, price: number): Promise<number> {
|
||||
const [res] = await db(DB.default).query<ResultSetHeader>(
|
||||
'INSERT INTO home_goldprice (gold_amount, price) VALUES (?, ?)',
|
||||
[goldAmount, price],
|
||||
)
|
||||
return res.insertId
|
||||
}
|
||||
|
||||
export async function deleteGoldOption(id: number): Promise<void> {
|
||||
await db(DB.default).query('DELETE FROM home_goldprice WHERE id = ?', [id])
|
||||
}
|
||||
@@ -326,7 +326,13 @@
|
||||
"voteUrl": "URL",
|
||||
"voteImage": "Image URL",
|
||||
"votePoints": "VP per vote",
|
||||
"noVotes": "No vote sites."
|
||||
"noVotes": "No vote sites.",
|
||||
"manageGold": "Manage gold options",
|
||||
"gold": "Gold options",
|
||||
"goldAmount": "Gold amount",
|
||||
"goldPrice": "Price (€)",
|
||||
"goldUnit": "gold",
|
||||
"noGold": "No gold options."
|
||||
},
|
||||
"Vote": {
|
||||
"title": "Vote for us",
|
||||
|
||||
@@ -326,7 +326,13 @@
|
||||
"voteUrl": "URL",
|
||||
"voteImage": "URL de la imagen",
|
||||
"votePoints": "PV por voto",
|
||||
"noVotes": "No hay sitios de voto."
|
||||
"noVotes": "No hay sitios de voto.",
|
||||
"manageGold": "Gestionar opciones de oro",
|
||||
"gold": "Opciones de oro",
|
||||
"goldAmount": "Cantidad de oro",
|
||||
"goldPrice": "Precio (€)",
|
||||
"goldUnit": "oro",
|
||||
"noGold": "No hay opciones de oro."
|
||||
},
|
||||
"Vote": {
|
||||
"title": "Votar por nosotros",
|
||||
|
||||
Reference in New Issue
Block a user