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 onReject: (reason: string | null) => Promise onUndo: () => Promise /** 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(null) const [rejecting, setRejecting] = useState(false) const [reason, setReason] = useState('') const [busy, setBusy] = useState(false) const [failed, setFailed] = useState(false) const timer = useRef | null>(null) useEffect(() => () => { if (timer.current) clearTimeout(timer.current) }, []) const run = async (action: () => Promise, 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 (
  • {outcome === 'confirmed' ? (

  • ) } return (
  • {report.attendee.fullName}

    {report.group.name} · {reportedAt}

    {amount}

    {report.note ? (

    Nota del alumno {report.note}

    ) : null} {rejecting ? (
    { event.preventDefault() void run(() => onReject(reason.trim() || null), 'rejected') }} > setReason(event.target.value)} className="h-11" />
    ) : (
    )} {failed ? (

    No pudimos guardar el cambio. Probá de nuevo.

    ) : null}
  • ) }