import { Box, Button, Typography } from '@mui/material'; import { useEffect, useState } from 'react'; import GradientTextField from '../components/GradientTextField'; import CustomNotification from '../components/Notifications/CustomNotification'; import type { NotificationPosition } from '../components/Notifications/CustomNotification'; import { isNotificationsEnabled, getNotifPositionFromSettings, } from '../utils/notifications'; import { redeemPromoCode } from '../api/promocodes'; import { useNavigate } from 'react-router-dom'; export const PromoRedeem = () => { const navigate = useNavigate(); const [username, setUsername] = useState(''); // будет автозаполнение const [code, setCode] = useState(''); const [loading, setLoading] = useState(false); const [notifOpen, setNotifOpen] = useState(false); const [notifMsg, setNotifMsg] = useState(''); const [notifSeverity, setNotifSeverity] = useState< 'success' | 'info' | 'warning' | 'error' >('info'); const [notifPos, setNotifPos] = useState({ vertical: 'bottom', horizontal: 'center', }); const showNotification = ( message: React.ReactNode, severity: 'success' | 'info' | 'warning' | 'error' = 'info', position: NotificationPosition = getNotifPositionFromSettings(), ) => { if (!isNotificationsEnabled()) return; setNotifMsg(message); setNotifSeverity(severity); setNotifPos(position); setNotifOpen(true); }; // как в Profile.tsx: читаем launcher_config useEffect(() => { try { const savedConfig = localStorage.getItem('launcher_config'); if (savedConfig) { const config = JSON.parse(savedConfig); setUsername(config.username || ''); } } catch { setUsername(''); } }, []); const handleRedeem = async () => { if (!username) { showNotification( 'Не удалось определить никнейм. Войдите в аккаунт заново.', 'warning', ); // по желанию можно отправить в login/profile: // navigate('/login', { replace: true }); return; } if (!code) { showNotification('Введите промокод', 'warning'); return; } setLoading(true); try { const res = await redeemPromoCode(username, code); showNotification( <> Промокод {res.code} успешно активирован! , 'success', ); setCode(''); } catch (e: any) { showNotification(e?.message || 'Ошибка активации промокода', 'error'); } finally { setLoading(false); } }; return ( <> Активация промокода setCode(e.target.value.toUpperCase())} /> setNotifOpen(false)} autoHideDuration={2500} /> ); };