'use client' import { useState, useEffect } from 'react' import { useI18n } from '@/lib/i18n' import { ALLOWED_PAYMENT_CURRENCIES } from '@/lib/payment-currencies' interface RedeemPointsModalProps { isOpen: boolean onClose: () => void currentPoints: number onRedeemSuccess: () => void } export default function RedeemPointsModal({ isOpen, onClose, currentPoints, onRedeemSuccess, }: RedeemPointsModalProps) { const { t } = useI18n() const [pointsToRedeem, setPointsToRedeem] = useState('') const [cryptoCurrency, setCryptoCurrency] = useState('btc') const [walletAddress, setWalletAddress] = useState('') const [loading, setLoading] = useState(false) const [error, setError] = useState('') const [success, setSuccess] = useState(false) const [redemptionDetails, setRedemptionDetails] = useState(null) const [pointsToCryptoChf, setPointsToCryptoChf] = useState(100) const [minRedemptionPoints, setMinRedemptionPoints] = useState(1000) const [estimatedCrypto, setEstimatedCrypto] = useState(0) useEffect(() => { if (isOpen) { fetchRedemptionSettings() // Reset form setPointsToRedeem('') setWalletAddress('') setCryptoCurrency('btc') setError('') setSuccess(false) setRedemptionDetails(null) } }, [isOpen]) const fetchRedemptionSettings = async () => { try { const response = await fetch('/api/referral-points', { credentials: 'include', }) if (response.ok) { const data = await response.json() setPointsToCryptoChf(data.points_to_crypto_chf || data.points_to_chf || 100) setMinRedemptionPoints(data.min_redemption_points || 1000) } } catch (error) { console.error('Error fetching redemption settings:', error) } } useEffect(() => { // Calculate estimated crypto amount when points or currency changes if (pointsToRedeem && !isNaN(parseFloat(pointsToRedeem))) { const points = parseFloat(pointsToRedeem) const chfValue = points / pointsToCryptoChf // Mock exchange rates (should match API) const mockRates: Record = { 'btc': 85000, 'eth': 2500, 'sol': 100, 'xrp': 0.6, 'bnbbsc': 300, 'usdterc20': 0.9, } const rate = mockRates[cryptoCurrency.toLowerCase()] || 1 setEstimatedCrypto(chfValue / rate) } else { setEstimatedCrypto(0) } }, [pointsToRedeem, cryptoCurrency, pointsToCryptoChf]) const handleSubmit = async (e: React.FormEvent) => { e.preventDefault() setError('') setLoading(true) try { const points = parseFloat(pointsToRedeem) if (isNaN(points) || points <= 0) { setError(t('redeemPoints.invalidPoints')) setLoading(false) return } if (points < minRedemptionPoints) { setError(t('redeemPoints.minPoints', { min: minRedemptionPoints })) setLoading(false) return } if (points > currentPoints) { setError(t('redeemPoints.insufficientPoints')) setLoading(false) return } if (!walletAddress.trim()) { setError(t('redeemPoints.invalidWallet')) setLoading(false) return } const response = await fetch('/api/referral-points/redeem', { method: 'POST', headers: { 'Content-Type': 'application/json', }, credentials: 'include', body: JSON.stringify({ points: points, crypto_currency: cryptoCurrency, wallet_address: walletAddress.trim(), }), }) const data = await response.json() if (!response.ok) { setError(data.error || t('redeemPoints.error')) setLoading(false) return } setSuccess(true) setRedemptionDetails(data) onRedeemSuccess() } catch (error: any) { console.error('Error redeeming points:', error) setError(error.message || t('redeemPoints.error')) } finally { setLoading(false) } } if (!isOpen) return null const pointsNum = parseFloat(pointsToRedeem) || 0 const chfValue = pointsNum / pointsToCryptoChf return (
e.stopPropagation()} >

{t('redeemPoints.title')}

{success ? (
✓ {t('redeemPoints.success')}
{redemptionDetails && (

{t('redeemPoints.redemptionId')}: #{redemptionDetails.redemption_id}

{t('redeemPoints.pointsRedeemed')}: {redemptionDetails.points_redeemed.toFixed(2)}

{t('redeemPoints.cryptoAmount')}: {redemptionDetails.crypto_amount.toFixed(8)} {redemptionDetails.crypto_currency.toUpperCase()}

{t('redeemPoints.newBalance')}: {redemptionDetails.new_balance.toFixed(2)} {t('redeemPoints.points')}

{redemptionDetails.message}

)}
) : (
{t('redeemPoints.currentBalance')}
⭐ {currentPoints.toFixed(2)} {t('redeemPoints.points')}
setWalletAddress(e.target.value)} placeholder={t('redeemPoints.walletAddressPlaceholder')} style={{ width: '100%', padding: '12px', background: 'var(--bg-soft)', border: '1px solid var(--border)', borderRadius: '8px', fontSize: '14px', color: 'var(--text)', marginBottom: '16px', fontFamily: 'monospace', }} required /> setPointsToRedeem(e.target.value)} min={minRedemptionPoints} max={currentPoints} step="1" placeholder={minRedemptionPoints.toString()} style={{ width: '100%', padding: '12px', background: 'var(--bg-soft)', border: '1px solid var(--border)', borderRadius: '8px', fontSize: '14px', color: 'var(--text)', marginBottom: '16px', }} required /> {pointsNum > 0 && (
{t('redeemPoints.estimatedValue')}:
{chfValue.toFixed(2)} CHF ≈ {estimatedCrypto.toFixed(8)} {cryptoCurrency.toUpperCase()}
)} {error && (
{error}
)}
)}
) }