import React, { useEffect, useState } from 'react'; export default function Transactions() { const [transactions, setTransactions] = useState([]); const [type, setType] = useState('expense'); const [amount, setAmount] = useState(''); const [category, setCategory] = useState(''); const [description, setDescription] = useState(''); const [date, setDate] = useState(new Date().toISOString().slice(0, 10)); const fetchTxs = async () => { const token = localStorage.getItem('token'); const res = await fetch('/api/transactions', { headers: { 'Authorization': `Bearer ${token}` } }); if (res.ok) setTransactions(await res.json()); }; useEffect(() => { fetchTxs(); }, []); const handleSubmit = async (e) => { e.preventDefault(); const token = localStorage.getItem('token'); await fetch('/api/transactions', { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${token}` }, body: JSON.stringify({ type, amount, category, description, date }) }); setAmount(''); setCategory(''); setDescription(''); fetchTxs(); }; const handleDelete = async (id) => { const token = localStorage.getItem('token'); await fetch(`/api/transactions/${id}`, { method: 'DELETE', headers: { 'Authorization': `Bearer ${token}` } }); fetchTxs(); }; return (
| Дата | Тип | Категория | Описание | Сумма | Действия |
|---|---|---|---|---|---|
| {tx.date} | {tx.type === 'income' ? Доход : Расход} | {tx.category} | {tx.description} | {tx.type === 'income' ? '+' : '-'}{tx.amount} ₽ |