PaperDepositWithdrawModal.tsx 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. 'use client';
  2. import { useCallback, useEffect, useState } from 'react';
  3. import { fetchApi } from '@/lib/utils/client';
  4. import { Dialog, DialogContent, DialogHeader, DialogTitle } from '@/components/ui/dialog';
  5. import { formatTokenNumber } from '@/lib/utils/paper';
  6. import type { PaperAccountResponse, PaperDepositResponse, PaperWithdrawResponse } from '@/types/paper';
  7. // 지갑 토큰(Reward+Airdrop) 합산용 최소 타입 — GET /api/wallet
  8. interface WalletBalanceRow {
  9. type: string;
  10. amount: number;
  11. }
  12. interface WalletResponse {
  13. balances: WalletBalanceRow[];
  14. }
  15. type Props = {
  16. mode: 'deposit'|'withdraw';
  17. account: PaperAccountResponse|null;
  18. onClose: () => void;
  19. onDone: () => void;
  20. };
  21. const TOKEN_TYPES = ['Reward', 'Airdrop'];
  22. export default function PaperDepositWithdrawModal({ mode, account, onClose, onDone }: Props)
  23. {
  24. const isDeposit = mode === 'deposit';
  25. const [amount, setAmount] = useState('');
  26. const [all, setAll] = useState(false);
  27. const [walletToken, setWalletToken] = useState<number|null>(null);
  28. const [busy, setBusy] = useState(false);
  29. const [error, setError] = useState<string|null>(null);
  30. // 입금 모드: 지갑 가용 토큰 조회
  31. useEffect(() => {
  32. if (!isDeposit) {
  33. return;
  34. }
  35. let cancelled = false;
  36. (async () => {
  37. const res = await fetchApi<WalletResponse>('/api/wallet', { silent: true });
  38. if (cancelled) {
  39. return;
  40. }
  41. if (res.success && res.data) {
  42. const sum = res.data.balances
  43. .filter(c => TOKEN_TYPES.includes(c.type))
  44. .reduce((acc, c) => acc + c.amount, 0);
  45. setWalletToken(sum);
  46. }
  47. else {
  48. setWalletToken(null);
  49. }
  50. })();
  51. return () => {
  52. cancelled = true;
  53. };
  54. }, [isDeposit]);
  55. const submit = useCallback(async () => {
  56. setError(null);
  57. const numeric = Number(amount);
  58. if (!isDeposit && all) {
  59. // 전액 출금 — 금액 불필요
  60. }
  61. else if (!Number.isInteger(numeric) || numeric <= 0) {
  62. setError('1 이상의 정수 토큰 금액을 입력해 주세요.');
  63. return;
  64. }
  65. setBusy(true);
  66. const endpoint = isDeposit ? '/api/paper/account/deposit' : '/api/paper/account/withdraw';
  67. const body = isDeposit
  68. ? { tokenAmount: numeric }
  69. : (all ? { all: true } : { tokenAmount: numeric });
  70. const res = await fetchApi<PaperDepositResponse|PaperWithdrawResponse>(endpoint, {
  71. method: 'POST',
  72. body,
  73. silent: true
  74. });
  75. setBusy(false);
  76. if (res.success && res.data) {
  77. onDone();
  78. }
  79. else {
  80. setError(res.message || `${isDeposit ? '입금' : '출금'}에 실패했습니다.`);
  81. }
  82. }, [amount, all, isDeposit, onDone]);
  83. return (
  84. <Dialog open onOpenChange={(open) => { if (!open) onClose(); }}>
  85. <DialogContent>
  86. <DialogHeader>
  87. <DialogTitle>{isDeposit ? '토큰 입금' : '토큰 출금'}</DialogTitle>
  88. </DialogHeader>
  89. <div className='paper-modal__body'>
  90. {isDeposit ? (
  91. <p className='paper-modal__balance'>
  92. 지갑 가용 토큰 <strong>{walletToken === null ? '—' : formatTokenNumber(walletToken)}</strong>
  93. </p>
  94. ) : (
  95. <p className='paper-modal__balance'>
  96. 계좌 가용 토큰 <strong>{formatTokenNumber(account?.token ?? 0)}</strong>
  97. </p>
  98. )}
  99. {!isDeposit && (
  100. <label className='paper-modal__all'>
  101. <input
  102. type='checkbox'
  103. checked={all}
  104. onChange={e => setAll(e.target.checked)}
  105. />
  106. 가용 토큰 전액 출금
  107. </label>
  108. )}
  109. {!(!isDeposit && all) && (
  110. <div className='paper-modal__field'>
  111. <span className='paper-modal__label'>{isDeposit ? '입금' : '출금'} 토큰</span>
  112. <input
  113. className='paper-modal__input'
  114. type='number'
  115. min={1}
  116. step={1}
  117. inputMode='numeric'
  118. value={amount}
  119. onChange={e => setAmount(e.target.value)}
  120. placeholder='토큰 수량'
  121. autoFocus
  122. />
  123. </div>
  124. )}
  125. <p className='paper-modal__hint'>
  126. {isDeposit
  127. ? '입금한 토큰만큼 좌수가 발행되며, 지갑 토큰이 차감됩니다.'
  128. : '출금한 토큰만큼 좌수가 소각되며, 지갑 토큰으로 환급됩니다.'}
  129. </p>
  130. {error && <p className='paper-modal__error' role='alert'>{error}</p>}
  131. <div className='paper-modal__footer'>
  132. <button type='button' className='paper__btn' onClick={onClose} disabled={busy}>
  133. 취소
  134. </button>
  135. <button type='button' className='paper__btn paper__btn--primary' onClick={submit} disabled={busy}>
  136. {busy ? '처리 중...' : (isDeposit ? '입금' : '출금')}
  137. </button>
  138. </div>
  139. </div>
  140. </DialogContent>
  141. </Dialog>
  142. );
  143. }