import { useMutation } from "@tanstack/react-query"; import { useState } from "react"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { formatCents, TOPUP_MAX_CENTS, TOPUP_MIN_CENTS, TOPUP_PRESETS_CENTS, } from "@/lib/drinkkaart"; import { orpc } from "@/utils/orpc"; interface TopUpModalProps { onClose: () => void; } export function TopUpModal({ onClose }: TopUpModalProps) { const [selectedCents, setSelectedCents] = useState(1000); const [customEuros, setCustomEuros] = useState(""); const [useCustom, setUseCustom] = useState(false); const checkoutMutation = useMutation({ ...orpc.drinkkaart.getTopUpCheckoutUrl.mutationOptions(), onSuccess: (data: { checkoutUrl: string }) => { window.location.href = data.checkoutUrl; }, onError: (err: Error) => { console.error("Checkout error:", err); }, }); const amountCents = useCustom ? Math.round((Number.parseFloat(customEuros || "0") || 0) * 100) : selectedCents; const isValidAmount = Number.isInteger(amountCents) && amountCents >= TOPUP_MIN_CENTS && amountCents <= TOPUP_MAX_CENTS; const handleSubmit = () => { if (!isValidAmount) return; checkoutMutation.mutate({ amountCents }); }; return (
{/* Preset amounts */}
{TOPUP_PRESETS_CENTS.map((cents) => ( ))}
{/* Custom amount */}
{ setCustomEuros(e.target.value); setUseCustom(true); }} className="border-white/20 bg-white/10 pl-8 text-white placeholder:text-white/30" />
{useCustom && !isValidAmount && customEuros !== "" && (

Bedrag moet tussen {formatCents(TOPUP_MIN_CENTS)} en{" "} {formatCents(TOPUP_MAX_CENTS)} liggen

)}
Te betalen: {isValidAmount ? formatCents(amountCents) : "—"}
); }