import { useState } from "react"; import { useLocation, useNavigate } from "react-router-dom"; import { useQuery } from "@tanstack/react-query"; import { Delete } from "lucide-react"; import { toast } from "sonner"; import { topup, getCommission } from "@/api/driver"; import { keypadReduce, withCommission } from "@/lib/amount"; import { formatMoney } from "@/lib/format"; import { Segment } from "@/components/Segment"; import { Spinner } from "@/components/Spinner"; const QUICK = ["500", "1000", "2000"]; const KEYS = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "·", "0", "back"]; export function TopupPage() { const nav = useNavigate(); const loc = useLocation(); const bucket = (loc.state as { bucket?: string } | null)?.bucket ?? "Долг аренда"; const [raw, setRaw] = useState(""); const [method, setMethod] = useState("qr"); const [busy, setBusy] = useState(false); const { data: commission } = useQuery({ queryKey: ["commission"], queryFn: getCommission }); const rate = commission?.rate ?? 0.052; // fallback only while loading const base = raw ? parseInt(raw, 10) : 0; const { commission: fee, total } = withCommission(base, rate); function press(k: string) { if (k === "·") return; setRaw((r) => keypadReduce(r, k)); } async function pay() { if (base <= 0) return; setBusy(true); try { const r = await topup(bucket, base, method as "qr" | "card"); nav(`/pay/${r.order_id}`, { state: { payUrl: r.pay_url, total: r.total, bucket, base } }); } catch { toast.error("Не удалось создать платёж"); } finally { setBusy(false); } } return (
{formatMoney(base)}
к оплате {formatMoney(total)} (комиссия {formatMoney(fee)})