"use client"; import React, { useState } from 'react'; import { XMarkIcon, PlusIcon, ExclamationTriangleIcon } from '@heroicons/react/24/outline'; import { authenticatedApi } 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 <= 100; // 1-100 GB, whole numbers only }; 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 authenticatedApi.post(`/subscriptions/${subscriptionId}/sim/top-up`, requestBody); onSuccess(); } catch (error: any) { 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

{/* Amount Input */}
setGbAmount(e.target.value)} placeholder="Enter amount in GB" min="1" max="100" 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 - 100 GB, whole numbers)

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

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

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