2026-11-23 21:22:46 +00:00
"use client" ;
import { useCallback , useEffect , useState } from "react" ;
import { api , errorText } from "@/lib/api" ;
import { formatDate } from "@/lib/astro" ;
import type { AdminGift } from "@/lib/types" ;
const huf = ( n : number ) => ` ${ n . toLocaleString ( "hu-HU" ) } Ft` ;
2026-11-24 07:01:40 +00:00
const STORNO : Record < string , string > = {
pending : "sztornó folyamatban" ,
done : "sztornózva" ,
failed : "sztornó hiba" ,
};
2026-11-23 21:22:46 +00:00
/** The gifts with their payment, invoice and storno, and the refund button. */
export function AdminGifts() {
const [ gifts , setGifts ] = useState < AdminGift [] | null >( null );
const [ q , setQ ] = useState ( "" );
const [ busy , setBusy ] = useState < string | null >( null );
const [ error , setError ] = useState < string | null >( null );
const [ done , setDone ] = useState < string | null >( null );
const load = useCallback ( async ( search : string ) => {
try {
2026-11-24 07:01:40 +00:00
const r = await api < { gifts : AdminGift [] } > (
`/admin/gifts ${ search ? `?q= ${ encodeURIComponent ( search ) } ` : "" } ` ,
);
2026-11-23 21:22:46 +00:00
setGifts ( r . gifts );
} catch ( e ) {
setError ( errorText ( e ));
}
}, []);
useEffect (() => {
const t = setTimeout (() => load ( q . trim ()), 250 );
return () => clearTimeout ( t );
}, [ q , load ]);
async function refund ( g : AdminGift ) {
2026-11-24 07:01:40 +00:00
const late = g . within_withdrawal
? ""
: "\n\nFigyelem: a 14 napos elállási idő már letelt." ;
const money = g . checkout ? . amount_huf
? ` ( ${ huf ( g . checkout . amount_huf ) } )`
: "" ;
2026-11-23 21:22:46 +00:00
if (
! window . confirm (
`Visszatéríted a(z) „ ${ g . package . name } ” ajándékot ${ money } ? A ${ g . code } kód érvénytelen lesz, a Stripe visszautalja az árát, a számla sztornózódik. ${ late } ` ,
)
)
return ;
setBusy ( g . id );
setError ( null );
setDone ( null );
try {
await api ( `/admin/gifts/ ${ g . id } /refund` , { method : "POST" , body : {} });
setDone ( `Visszatérítve: ${ g . code } .` );
} catch ( e ) {
setError ( errorText ( e ));
} finally {
setBusy ( null );
await load ( q . trim ());
}
}
return (
< section className = "card" >
< div className = "field" >
< label htmlFor = "gift-search" > Keresés </ label >
< input
id = "gift-search"
className = "inp"
value = { q }
onChange = {( e ) => setQ ( e . target . value )}
placeholder = "Kód, a vásárló vagy a címzett e-mail-címe, a címzett neve"
/>
</ div >
{ error && (
< p className = "msg msg-error" role = "alert" >
{ error }
</ p >
)}
{ done && (
< p className = "msg msg-ok" role = "status" >
{ done }
</ p >
)}
{ gifts === null ? (
< p className = "muted" > Betöltés … </ p >
) : gifts . length === 0 ? (
< p className = "muted" > Nincs ilyen ajándék .</ p >
) : (
< div className = "table-wrap" >
< table className = "promo-table" >
< thead >
< tr >
< th > Kód </ th >
< th > Csomag </ th >
< th > Vásárló → címzett </ th >
< th > Fizetve </ th >
< th > Á llapot </ th >
< th > Számla </ th >
< th />
</ tr >
</ thead >
< tbody >
{ gifts . map (( g ) => (
< tr key = { g . id }>
< td >
< code className = "promo-code" >{ g . code }</ code >
</ td >
< td >
{ g . package . name ?? g . package . id }
2026-11-24 07:01:40 +00:00
{ g . checkout ? . amount_huf ? (
< span className = "hint" >{ huf ( g . checkout . amount_huf )}</ span >
) : null }
{ g . checkout && g . checkout . live === false && (
< span className = "hint" > teszt fizetés </ span >
)}
2026-11-23 21:22:46 +00:00
</ td >
< td >
2026-11-24 07:01:40 +00:00
{ g . buyer_email ?? (
< span className = "muted" > törölt fiók </ span >
)}
2026-11-23 21:22:46 +00:00
< span className = "hint" >
→ { g . recipient_name ?? "?" }
2026-11-24 07:01:40 +00:00
{ g . recipient_email
? ` ( ${ g . recipient_email } )`
: " (maga adja át)" }
2026-11-23 21:22:46 +00:00
</ span >
</ td >
< td className = "num" >{ formatDate ( g . paid_at , true )}</ td >
< td >
{ g . status === "redeemed" ? (
<>
2026-11-24 07:01:40 +00:00
beváltva { " " }
{ g . redeemed_at ? formatDate ( g . redeemed_at , true ) : "" }
{ g . redeemed_by && (
< span className = "hint" >{ g . redeemed_by }</ span >
)}
2026-11-23 21:22:46 +00:00
</>
) : g . status === "refunded" ? (
<>
{ g . refund_pending ? (
2026-11-24 07:01:40 +00:00
< span className = "msg-error" >
visszatérítés félbemaradt
</ span >
2026-11-23 21:22:46 +00:00
) : (
"visszatérítve"
)}{ " " }
{ g . refunded_at ? formatDate ( g . refunded_at , true ) : "" }
2026-11-24 07:01:40 +00:00
{ g . refund_pending && g . refund_error && (
< span className = "hint" >{ g . refund_error }</ span >
)}
2026-11-23 21:22:46 +00:00
</>
) : (
<>
< span className = "promo-open" > beváltatlan </ span >
2026-11-24 07:01:40 +00:00
< span className = "hint" >
{ g . within_withdrawal
? "14 napon belül"
: "14 nap letelt" }
</ span >
2026-11-23 21:22:46 +00:00
</>
)}
</ td >
< td >
{ g . invoice ? (
<>
2026-11-24 07:01:40 +00:00
{ g . invoice . number ??
( g . invoice . status === "pending"
? "kiállítás alatt"
: g . invoice . status )}
2026-11-23 21:22:46 +00:00
{ g . storno && (
2026-11-24 07:01:40 +00:00
< span
className = {
g . storno . status === "failed"
? "hint msg-error"
: "hint"
}
>
2026-11-23 21:22:46 +00:00
{ STORNO [ g . storno . status ] ?? g . storno . status }
{ g . storno . number ? `: ${ g . storno . number } ` : "" }
{ g . storno . error ? ` – ${ g . storno . error } ` : "" }
</ span >
)}
</>
) : (
< span className = "muted" > – </ span >
)}
</ td >
< td >
{( g . status === "open" || g . refund_pending ) && (
< button
type = "button"
className = "btn btn-sm"
onClick = {() => refund ( g )}
disabled = { busy !== null }
>
2026-11-24 07:01:40 +00:00
{ busy === g . id
? "Folyamatban…"
: g . status === "refunded"
? "Újrapróbálás"
: "Visszatérítés" }
2026-11-23 21:22:46 +00:00
</ button >
)}
</ td >
</ tr >
))}
</ tbody >
</ table >
</ div >
)}
</ section >
);
}