"use client"; import { useState } from "react"; import { toast } from "sonner"; import { CheckIcon, XIcon } from "lucide-react"; import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert"; import { Badge } from "@/components/ui/badge"; import { Button } from "@/components/ui/button"; import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle, } from "@/components/ui/card"; import { approveTransfer, rejectTransfer } from "@/app/(frontend)/transfers/actions"; import { TransferNoteDialog } from "@/components/frontend/transfers/TransferNoteDialog"; import { TRANSFER_DECISION_LABELS, TRANSFER_STATUS_LABELS, TRANSFER_STATUS_VARIANTS, assignmentLabel, formatTransferDate, personLabel, type TransferRecord, type TransferSide, } from "@/components/frontend/transfers/transferTypes"; interface TransferApprovalCardProps { readonly transfer: TransferRecord; readonly side: TransferSide; readonly onUpdated: () => Promise; } export function TransferApprovalCard({ transfer, side, onUpdated }: TransferApprovalCardProps) { const [rejectOpen, setRejectOpen] = useState(false); const [busy, setBusy] = useState<"approve" | null>(null); const [error, setError] = useState(null); const sideLabel = side === "from" ? "source" : "destination"; const otherSide = side === "from" ? "to" : "from"; const otherDecision = otherSide === "from" ? transfer.fromLeaderDecision : transfer.toLeaderDecision; async function handleApprove() { setBusy("approve"); setError(null); try { const result = await approveTransfer(transfer.id, side); if (!result.success) { setError(result.error ?? "Could not approve the transfer request."); return; } toast.success("Transfer request approved"); await onUpdated(); } catch (caughtError) { setError( caughtError instanceof Error ? caughtError.message : "Could not approve the transfer request.", ); } finally { setBusy(null); } } async function handleReject(reason: string) { const result = await rejectTransfer(transfer.id, side, reason); if (result.success) void onUpdated(); return result; } return (
Transfer #{transfer.id} {personLabel(transfer.requester)} · requested {formatTransferDate(transfer.createdAt)}
{TRANSFER_STATUS_LABELS[transfer.status]}
From {assignmentLabel(transfer.fromAssignment)}
To {assignmentLabel(transfer.toAssignment)}
Your {sideLabel} approval is required. Other side: {TRANSFER_DECISION_LABELS[otherDecision]}
{transfer.requestNote && (
Request note

{transfer.requestNote}

)} {error && ( Action failed {error} )}
); }