"use client"; import React, { useEffect, useMemo, useState } from "react"; import { apiClient } from "@/lib/api"; interface SimFeatureTogglesProps { subscriptionId: number; voiceMailEnabled?: boolean; callWaitingEnabled?: boolean; internationalRoamingEnabled?: boolean; networkType?: string; // '4G' | '5G' onChanged?: () => void; embedded?: boolean; // when true, render without outer card wrappers } export function SimFeatureToggles({ subscriptionId, voiceMailEnabled, callWaitingEnabled, internationalRoamingEnabled, networkType, onChanged, embedded = false, }: SimFeatureTogglesProps) { // Initial values const initial = useMemo( () => ({ vm: !!voiceMailEnabled, cw: !!callWaitingEnabled, ir: !!internationalRoamingEnabled, nt: networkType === "5G" ? "5G" : "4G", }), [voiceMailEnabled, callWaitingEnabled, internationalRoamingEnabled, networkType] ); // Working values const [vm, setVm] = useState(initial.vm); const [cw, setCw] = useState(initial.cw); const [ir, setIr] = useState(initial.ir); const [nt, setNt] = useState<"4G" | "5G">(initial.nt as "4G" | "5G"); const [loading, setLoading] = useState(false); const [error, setError] = useState(null); const [success, setSuccess] = useState(null); useEffect(() => { setVm(initial.vm); setCw(initial.cw); setIr(initial.ir); setNt(initial.nt as "4G" | "5G"); }, [initial.vm, initial.cw, initial.ir, initial.nt]); const reset = () => { setVm(initial.vm); setCw(initial.cw); setIr(initial.ir); setNt(initial.nt as "4G" | "5G"); setError(null); setSuccess(null); }; const applyChanges = async () => { setLoading(true); setError(null); setSuccess(null); try { const featurePayload: { voiceMailEnabled?: boolean; callWaitingEnabled?: boolean; internationalRoamingEnabled?: boolean; networkType?: "4G" | "5G"; } = {}; if (vm !== initial.vm) featurePayload.voiceMailEnabled = vm; if (cw !== initial.cw) featurePayload.callWaitingEnabled = cw; if (ir !== initial.ir) featurePayload.internationalRoamingEnabled = ir; if (nt !== initial.nt) featurePayload.networkType = nt; if (Object.keys(featurePayload).length > 0) { await apiClient.POST("/api/subscriptions/{id}/sim/features", { params: { path: { id: subscriptionId } }, body: featurePayload, }); } setSuccess("Changes submitted successfully"); onChanged?.(); } catch (e: unknown) { setError(e instanceof Error ? e.message : "Failed to submit changes"); } finally { setLoading(false); setTimeout(() => setSuccess(null), 3000); } }; return (
{/* Service Options */}
{/* Voice Mail */}
Voice Mail
¥300/month
{/* Call Waiting */}
Call Waiting
¥300/month
{/* International Roaming */}
International Roaming
Global connectivity
Network Type
Choose your preferred connectivity
Voice, network, and plan changes must be requested at least 30 minutes apart. If you just changed another option, you may need to wait before submitting.
setNt("4G")} className="h-4 w-4 text-blue-600 focus:ring-blue-500 border-gray-300" />
setNt("5G")} className="h-4 w-4 text-blue-600 focus:ring-blue-500 border-gray-300" />

5G connectivity for enhanced speeds

{/* Notes and Actions */}

Important Notes

  • Changes will take effect instantaneously (approx. 30min)
  • May require smartphone device restart after changes are applied
  • Voice, network, and plan changes must be requested at least 30 minutes apart.
  • Changes to Voice Mail / Call Waiting must be requested before the 25th of the month
{success && (

{success}

)} {error && (

{error}

)}
); }