"use client"; import { useState } from "react"; import Link from "next/link"; import { useParams } from "next/navigation"; import { PageLayout } from "@/components/templates/PageLayout"; import { SubCard } from "@/components/molecules/SubCard/SubCard"; import { simActionsService } from "@/features/subscriptions/services/sim-actions.service"; import { AlertBanner } from "@/components/molecules/AlertBanner"; import { DevicePhoneMobileIcon } from "@heroicons/react/24/outline"; export function SimTopUpContainer() { const params = useParams(); const subscriptionId = parseInt(params.id as string); const [gbAmount, setGbAmount] = useState("1"); const [loading, setLoading] = useState(false); const [message, setMessage] = useState(null); const [error, setError] = useState(null); 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; // 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()) { setError("Please enter a whole number between 1 GB and 50 GB"); return; } setLoading(true); setMessage(null); setError(null); try { await simActionsService.topUp(subscriptionId, { quotaMb: getCurrentAmountMb() }); setMessage(`Successfully topped up ${gbAmount} GB for ¥${calculateCost().toLocaleString()}`); } catch (e: unknown) { setError(e instanceof Error ? e.message : "Failed to submit top-up"); } finally { setLoading(false); } }; return ( } title="Top Up Data" description="Add data to your SIM" >
← Back to SIM Management

Add additional data quota to your SIM service. Enter the amount of data you want to add.

{message && (
{message}
)} {error && (
{error}
)}
void handleSubmit(e)} className="space-y-6">
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)

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

Please enter a valid whole number between 1 and 50 GB.

)}
Back
); } export default SimTopUpContainer;