'use client' import { useState, useEffect } from 'react' import Image from 'next/image' import { useI18n } from '@/lib/i18n' 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] = useState('usdtsol') // Fixed to USDT (SOL) only 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('') 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 changes // USDT (SOL) rate is approximately 0.9 CHF per USDT if (pointsToRedeem && !isNaN(parseFloat(pointsToRedeem))) { const points = parseFloat(pointsToRedeem) const chfValue = points / pointsToCryptoChf const usdtRate = 0.9 // CHF per USDT (SOL) setEstimatedCrypto(chfValue / usdtRate) } else { setEstimatedCrypto(0) } }, [pointsToRedeem, 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)} USDT

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

{redemptionDetails.message}

)}
) : (
{t('redeemPoints.currentBalance')}
Referral Points {currentPoints.toFixed(2)} {t('redeemPoints.points')}
USDT (SOL)
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={{ flex: 1, padding: '12px', background: 'var(--bg-soft)', border: '1px solid var(--border)', borderRadius: '8px', fontSize: '14px', color: 'var(--text)', }} required />
{pointsNum > 0 && (
{t('redeemPoints.estimatedValue')}:
{chfValue.toFixed(2)} CHF ≈ {estimatedCrypto.toFixed(8)} USDT
)} {error && (
{error}
)}
)}
) }