import React, { useState, useEffect } from 'react'; import axios from 'axios'; interface IUser { id: number; username: string; email: string; } interface ICheck { id: number; userId: number; amount: number; status: 'pending' | 'approved' | 'rejected'; fileUrl: string; createdAt: string; user?: IUser; } const API_URL = 'http://localhost:5000/api/check'; const CheckVerification: React.FC = () => { const [checks, setChecks] = useState([]); const [loading, setLoading] = useState(true); const [actionLoading, setActionLoading] = useState(null); const [error, setError] = useState(''); useEffect(() => { const fetchChecks = async (): Promise => { setLoading(true); setError(''); try { const token = localStorage.getItem('access_token'); const res = await axios.get(API_URL, { headers: { Authorization: `Bearer ${token}` }, withCredentials: true, }); setChecks(res.data); } catch { setError('Ошибка загрузки чеков'); setChecks([]); } setLoading(false); }; fetchChecks(); }, []); const handleAction = async (checkId: number, action: 'approve' | 'reject'): Promise => { setActionLoading(checkId); setError(''); try { const token = localStorage.getItem('access_token'); await axios.post(`${API_URL}/${action}`, { checkId }, { headers: { Authorization: `Bearer ${token}` }, withCredentials: true, }); setChecks((prevChecks: ICheck[]) => prevChecks.map((c: ICheck) => c.id === checkId ? { ...c, status: action === 'approve' ? 'approved' : 'rejected' } : c)); // Если подтверждение — обновить баланс пользователя if (action === 'approve') { try { const token = localStorage.getItem('access_token'); const headers = { Authorization: `Bearer ${token}` }; const userRes = await axios.get('http://localhost:5000/api/auth/me', { headers }); // Глобально обновить userData через типизированное событие (для Dashboard) window.dispatchEvent(new CustomEvent('userDataUpdate', { detail: { user: userRes.data.user, balance: userRes.data.user.balance ?? 0, servers: userRes.data.user.servers ?? [], tickets: userRes.data.user.tickets ?? [], } })); } catch (error) { console.error('Ошибка обновления userData:', error); } } } catch { setError('Ошибка действия'); } setActionLoading(null); }; return (

Проверка чеков

{loading ? (

Загрузка чеков...

) : error ? (

{error}

) : checks.length === 0 ? (

Нет чеков для проверки.

) : (
{checks.map((check: ICheck) => (
Пользователь: {check.user?.username || check.user?.email}
Сумма: ₽{check.amount}
Статус: {check.status === 'pending' ? 'На проверке' : check.status === 'approved' ? 'Подтверждён' : 'Отклонён'}
Дата: {new Date(check.createdAt).toLocaleString()}
Чек {check.status === 'pending' && ( <> )}
))}
)}
); }; export default CheckVerification;