'use client'; import { useState } from 'react'; import type { BroadcastInfo } from '@/types/broadcast'; interface DonationModalProps { isOpen: boolean; onClose: () => void; broadcast: BroadcastInfo; onDonationComplete?: (donationData: { amount: number; message: string; isAnonymous: boolean; username: string; }) => void; } export default function DonationModal({ isOpen, onClose, broadcast, onDonationComplete }: DonationModalProps) { const [selectedAmount, setSelectedAmount] = useState(null); const [customAmount, setCustomAmount] = useState(''); const [message, setMessage] = useState(''); const [isAnonymous, setIsAnonymous] = useState(false); const presetAmounts = [1000, 3000, 5000, 10000, 20000, 50000]; if (!isOpen) return null; const handleDonation = () => { const amount = selectedAmount || parseInt(customAmount); // 금액 유효성 검사 강화 if (!amount || isNaN(amount) || amount < 1000) { alert('최소 후원 금액은 1,000원입니다.'); return; } // 1,000원 단위로 반올림 (999원 입력 시 1,000원으로 조정) const roundedAmount = Math.max(1000, Math.round(amount / 1000) * 1000); if (roundedAmount !== amount) { alert(`입력하신 금액이 ${roundedAmount.toLocaleString()}원으로 조정되었습니다.`); } // 후원 처리 로직 (실제로는 결제 API 연동) const donationData = { broadcastId: broadcast.id, channel: broadcast.channel, amount: roundedAmount, message: message.trim(), isAnonymous, timestamp: new Date().toISOString() }; console.log('후원 정보:', donationData); // 채팅창에 후원 메시지 추가 if (onDonationComplete) { onDonationComplete({ amount: roundedAmount, message: message.trim() || '후원해주셔서 감사합니다!', isAnonymous, username: isAnonymous ? '익명' : '나' }); } alert(`${roundedAmount.toLocaleString()}원 후원이 완료되었습니다! 감사합니다.`); onClose(); // 폼 초기화 setSelectedAmount(null); setCustomAmount(''); setMessage(''); setIsAnonymous(false); }; return (
{/* 헤더 */}

후원하기

{broadcast.channel}님을 응원해주세요!

{/* 금액 선택 */}

후원 금액 선택

{presetAmounts.map((amount) => ( ))}
{/* 직접 입력 */}
{ const value = e.target.value; // 1,000원 단위로만 입력 허용 if (value === '' || (parseInt(value) >= 1000 && parseInt(value) % 1000 === 0)) { setCustomAmount(value); setSelectedAmount(null); } }} placeholder="1,000원 이상 (천원 단위)" min="1000" step="1000" className="w-full bg-gray-700 text-white px-3 py-2 rounded-lg focus:outline-none focus:ring-2 focus:ring-yellow-500" />
* 1,000원 단위로만 입력 가능합니다
{/* 후원 메시지 */}

후원 메시지 (선택)