import './topup.scss'; import React, { useEffect, useState } from 'react'; import Button from '../../../components/Button'; import ImageIcon from '../../../components/ImageIcon'; import Input from '../../../components/Input'; import Modal from '../../../components/Modal'; import { numberWithCommas } from '../../../utils/helperFunctions'; export interface TopupProp { visible: boolean; toggleVisible: (v: boolean) => void; successCB?: (result: any) => void; failCB?: (error: any) => void; completeCB?: () => void; } const moneylist = [ { label: '2,000', value: 2000 }, { label: '3,000', value: 3000 }, { label: '5,000', value: 5000 }, { label: '10,000', value: 10000 }, { label: '30,000', value: 30000 }, { label: '50,000', value: 50000 }, { label: '100,000', value: 100000 }, ]; enum TopupState { Topup = 0, SelectMoney, InputMoney, Charging, Done, Cancelled, } const Topup: React.FC = (props) => { const { visible, toggleVisible } = props; const [state, setState] = useState(TopupState.Topup); const [money, setMoney] = useState(1000); useEffect(() => { if (visible) { } else { // restore all status here const timer = setTimeout(() => { setState(TopupState.Topup); setMoney(1000); }, 500); return () => clearTimeout(timer); } }, [visible]); const renderTopup = () => { const set = new Set([ TopupState.Topup, TopupState.InputMoney, TopupState.Charging, TopupState.Done, ]); return set.has(state) ? ( <>
チャージ金額を選択
{[TopupState.Topup, TopupState.Charging, TopupState.Done].indexOf( state ) > -1 ? (
{numberWithCommas(money)} 円
) : null} {renderInputMoney()} {renderChargeArea()}
{state === TopupState.Done ? ( renderSuccessToast() ) : ( <>
チャージ方法
クレジットカード
**** 999
本人認証済
)} ) : null; }; const renderMoneyList = () => { return state === TopupState.SelectMoney ? (
    {moneylist.map((item) => { return (
    { setMoney(item.value); setState(TopupState.Topup); }} >
  • {item.label} 円
  • ); })}
) : null; }; const renderInputMoney = () => { return state === TopupState.InputMoney ? (
setMoney(parseInt(e.target.value))} /> 金額 円
) : null; }; const renderChargeArea = () => { return [TopupState.Charging, TopupState.Done].indexOf(state) === -1 ? (
setMoney(1000)} > 1,000
setMoney(3000)} className="money-number margin" > 3,000
setMoney(10000)} className="money-number margin" > 10,000
setState(TopupState.SelectMoney)} > ...
setState(TopupState.InputMoney)} > 入力
) : null; }; const handleBack = () => { if ([TopupState.InputMoney, TopupState.SelectMoney].indexOf(state) > -1) { setState(TopupState.Topup); } else if (state === TopupState.Topup) { toggleVisible && toggleVisible(false); } }; const chargeMoney = async () => { setState(TopupState.Charging); await new Promise((r) => setTimeout(() => r(), 360)); setState(TopupState.Done); await new Promise((r) => setTimeout(() => r(), 200)); toggleVisible(false); }; const renderSuccessToast = () => { return state === TopupState.Done ? (
) : null; }; return (

チャージ

{renderTopup()} {renderMoneyList()}
); }; export default Topup;