171 lines
6.7 KiB
TypeScript
171 lines
6.7 KiB
TypeScript
import { useEffect, useRef, useState } from 'react'
|
|
import type { ReportedPaymentDto } from '@gruperly/shared'
|
|
import { Check, ChevronRight, FileText, Loader2, X } from 'lucide-react'
|
|
import { Avatar, Button, Input, Label } from '../../components/ui'
|
|
import { formatPrice } from '../../lib/format'
|
|
|
|
const UNDO_MS = 6000
|
|
|
|
type Outcome = 'confirmed' | 'rejected'
|
|
|
|
type ReportedPaymentCardProps = {
|
|
report: ReportedPaymentDto
|
|
onOpenReceipt: () => void
|
|
onConfirm: () => Promise<unknown>
|
|
onReject: (reason: string | null) => Promise<unknown>
|
|
onUndo: () => Promise<unknown>
|
|
/** The undo window closed: the card leaves the list. */
|
|
onSettled: () => void
|
|
}
|
|
|
|
function feeName(fee: ReportedPaymentDto['fees'][number]): string {
|
|
if (fee.description) return fee.description
|
|
const month = new Date(fee.dueDate).toLocaleDateString('es-AR', { month: 'long', timeZone: 'UTC' })
|
|
return month.charAt(0).toUpperCase() + month.slice(1)
|
|
}
|
|
|
|
export function ReportedPaymentCard({ report, onOpenReceipt, onConfirm, onReject, onUndo, onSettled }: ReportedPaymentCardProps) {
|
|
const [outcome, setOutcome] = useState<Outcome | null>(null)
|
|
const [rejecting, setRejecting] = useState(false)
|
|
const [reason, setReason] = useState('')
|
|
const [busy, setBusy] = useState(false)
|
|
const [failed, setFailed] = useState(false)
|
|
const timer = useRef<ReturnType<typeof setTimeout> | null>(null)
|
|
|
|
useEffect(() => () => {
|
|
if (timer.current) clearTimeout(timer.current)
|
|
}, [])
|
|
|
|
const run = async (action: () => Promise<unknown>, next: Outcome | null) => {
|
|
setBusy(true)
|
|
setFailed(false)
|
|
try {
|
|
await action()
|
|
setOutcome(next)
|
|
setRejecting(false)
|
|
if (timer.current) clearTimeout(timer.current)
|
|
if (next) timer.current = setTimeout(onSettled, UNDO_MS)
|
|
} catch {
|
|
setFailed(true)
|
|
} finally {
|
|
setBusy(false)
|
|
}
|
|
}
|
|
|
|
const amount = formatPrice(report.totalAmount, report.currency)
|
|
const reportedAt = new Date(report.createdAt).toLocaleString('es-AR', {
|
|
day: 'numeric',
|
|
month: 'short',
|
|
hour: '2-digit',
|
|
minute: '2-digit',
|
|
hourCycle: 'h23',
|
|
})
|
|
|
|
if (outcome) {
|
|
return (
|
|
<li role="status" className="flex items-center justify-between gap-3 rounded-2xl border border-border bg-surface p-4">
|
|
<p className="flex min-w-0 items-center gap-2 text-sm text-primary">
|
|
{outcome === 'confirmed' ? (
|
|
<Check className="size-5 shrink-0 text-success" aria-hidden="true" />
|
|
) : (
|
|
<X className="size-5 shrink-0 text-danger-fg" aria-hidden="true" />
|
|
)}
|
|
<span className="truncate">
|
|
{outcome === 'confirmed' ? 'Confirmado' : 'Rechazado'}: {report.attendee.fullName} · {amount}
|
|
</span>
|
|
</p>
|
|
<Button variant="outline" className="h-11 shrink-0" onClick={() => void run(onUndo, null)} disabled={busy}>
|
|
Deshacer
|
|
</Button>
|
|
</li>
|
|
)
|
|
}
|
|
|
|
return (
|
|
<li className="rounded-2xl border border-border bg-surface p-4">
|
|
<div className="flex items-start gap-3">
|
|
<Avatar name={report.attendee.fullName} className="size-10 rounded-full bg-accent-soft text-accent-fg" />
|
|
<div className="min-w-0 flex-1">
|
|
<p className="truncate font-semibold text-primary">{report.attendee.fullName}</p>
|
|
<p className="truncate text-xs text-secondary">
|
|
{report.group.name} · {reportedAt}
|
|
</p>
|
|
</div>
|
|
<p className="shrink-0 text-base font-bold text-primary">{amount}</p>
|
|
</div>
|
|
|
|
<button
|
|
type="button"
|
|
onClick={onOpenReceipt}
|
|
disabled={!report.receipt.available}
|
|
className="mt-3 flex min-h-14 w-full items-center gap-3 rounded-xl bg-primary-soft p-2 text-left hover:bg-accent-soft disabled:opacity-60"
|
|
>
|
|
<span className="flex size-12 shrink-0 items-center justify-center overflow-hidden rounded-lg bg-surface">
|
|
{report.receipt.thumbnailUrl ? (
|
|
<img src={report.receipt.thumbnailUrl} alt="" className="size-full object-cover" />
|
|
) : (
|
|
<FileText className="size-5 text-foreground" aria-hidden="true" />
|
|
)}
|
|
</span>
|
|
<span className="min-w-0 flex-1">
|
|
<span className="block text-sm font-medium text-primary">
|
|
{report.receipt.available ? 'Ver comprobante' : 'Comprobante eliminado'}
|
|
</span>
|
|
<span className="block truncate text-xs text-secondary">{report.fees.map(feeName).join(', ')}</span>
|
|
</span>
|
|
<ChevronRight className="size-4 shrink-0 text-foreground" aria-hidden="true" />
|
|
</button>
|
|
|
|
{report.note ? (
|
|
<p className="mt-3 whitespace-pre-line break-words rounded-xl border border-border px-3 py-2 text-sm text-primary">
|
|
<span className="block text-xs font-medium text-secondary">Nota del alumno</span>
|
|
{report.note}
|
|
</p>
|
|
) : null}
|
|
|
|
{rejecting ? (
|
|
<form
|
|
className="mt-3 space-y-2"
|
|
onSubmit={(event) => {
|
|
event.preventDefault()
|
|
void run(() => onReject(reason.trim() || null), 'rejected')
|
|
}}
|
|
>
|
|
<Label htmlFor={`reject-${report.id}`}>Motivo (opcional, lo ve el alumno)</Label>
|
|
<Input id={`reject-${report.id}`} autoFocus maxLength={300} value={reason} onChange={(event) => setReason(event.target.value)} className="h-11" />
|
|
<div className="flex gap-2">
|
|
<Button variant="ghost" className="h-11 flex-1" onClick={() => setRejecting(false)}>
|
|
Cancelar
|
|
</Button>
|
|
<Button type="submit" variant="outline" className="h-11 flex-1" disabled={busy}>
|
|
{busy ? <Loader2 className="size-4 animate-spin" aria-hidden="true" /> : null}
|
|
Rechazar pago
|
|
</Button>
|
|
</div>
|
|
</form>
|
|
) : (
|
|
<div className="mt-3 flex gap-2">
|
|
<Button variant="outline" className="h-11 flex-1" onClick={() => setRejecting(true)} disabled={busy}>
|
|
Rechazar
|
|
</Button>
|
|
<Button
|
|
// Light fill, `primary` text (the green on its soft tint misses 4.5:1);
|
|
// the green stays on the check, next to the word.
|
|
className="h-11 flex-1 border border-success/40 bg-success-soft font-semibold text-primary hover:bg-success-soft/70"
|
|
onClick={() => void run(onConfirm, 'confirmed')}
|
|
disabled={busy}
|
|
>
|
|
{busy ? <Loader2 className="size-4 animate-spin" aria-hidden="true" /> : <Check className="size-4 text-success" strokeWidth={3} aria-hidden="true" />}
|
|
Confirmar
|
|
</Button>
|
|
</div>
|
|
)}
|
|
{failed ? (
|
|
<p role="alert" className="mt-2 text-sm text-danger-fg">
|
|
No pudimos guardar el cambio. Probá de nuevo.
|
|
</p>
|
|
) : null}
|
|
</li>
|
|
)
|
|
}
|