"use client"; import React, { useState } from "react"; import { XMarkIcon, PlusIcon, ExclamationTriangleIcon } from "@heroicons/react/24/outline"; import { apiClient } from "@/lib/api"; interface TopUpModalProps { subscriptionId: number; onClose: () => void; onSuccess: () => void; onError: (message: string) => void; } export function TopUpModal({ subscriptionId, onClose, onSuccess, onError }: TopUpModalProps) { const [gbAmount, setGbAmount] = useState("1"); const [loading, setLoading] = useState(false); const getCurrentAmountMb = () => { const gb = parseInt(gbAmount, 10); return isNaN(gb) ? 0 : gb * 1000; }; const isValidAmount = () => { const gb = Number(gbAmount); return Number.isInteger(gb) && gb >= 1 && gb <= 50; // 1-50 GB, whole numbers only (Freebit API limit) }; const calculateCost = () => { const gb = parseInt(gbAmount, 10); return isNaN(gb) ? 0 : gb * 500; // 1GB = 500 JPY }; const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); if (!isValidAmount()) { onError("Please enter a whole number between 1 GB and 100 GB"); return; } setLoading(true); try { const requestBody = { quotaMb: getCurrentAmountMb(), }; await apiClient.POST("/api/subscriptions/{id}/sim/top-up", { params: { path: { id: subscriptionId } }, body: requestBody, }); onSuccess(); } catch (error: unknown) { onError(error instanceof Error ? error.message : "Failed to top up SIM"); } finally { setLoading(false); } }; const handleBackdropClick = (e: React.MouseEvent) => { if (e.target === e.currentTarget) { onClose(); } }; return (
{/* Header */}

Top Up Data

Add data quota to your SIM service

void handleSubmit(e)}> {/* Amount Input */}
setGbAmount(e.target.value)} placeholder="Enter amount in GB" min="1" max="50" step="1" className="w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-blue-500 focus:border-blue-500 pr-12" />
GB

Enter the amount of data you want to add (1 - 50 GB, whole numbers)

{/* Cost Display */}
{gbAmount && !isNaN(parseInt(gbAmount, 10)) ? `${gbAmount} GB` : "0 GB"}
= {getCurrentAmountMb()} MB
¥{calculateCost().toLocaleString()}
(1GB = ¥500)
{/* Validation Warning */} {!isValidAmount() && gbAmount && (

Amount must be a whole number between 1 GB and 50 GB

)} {/* Action Buttons */}
); }