import { useState, useEffect } from "react" import { financeApi } from "../../api/finance" const fmt = (n) => Number(n).toLocaleString("ru-RU") + " ₽" const formatDate = (d) => { const dt = new Date(d) return dt.toLocaleDateString("ru-RU", { day: "numeric", month: "long" }) } export default function TransactionList({ onAdd, month, year }) { const [transactions, setTransactions] = useState([]) const [categories, setCategories] = useState([]) const [loading, setLoading] = useState(true) const [filter, setFilter] = useState("all") const [catFilter, setCatFilter] = useState(null) const [search, setSearch] = useState("") useEffect(() => { setLoading(true) Promise.all([ financeApi.listCategories(), financeApi.listTransactions({ month, year, limit: 100, }), ]) .then(([cats, txs]) => { setCategories(cats || []) setTransactions(txs || []) }) .catch(console.error) .finally(() => setLoading(false)) }, [month, year]) const filtered = transactions.filter((t) => { if (filter !== "all" && t.type !== filter) return false if (catFilter && t.category_id !== catFilter) return false if (search && !t.description.toLowerCase().includes(search.toLowerCase())) return false return true }) const grouped = filtered.reduce((acc, t) => { const d = t.date.slice(0, 10) ;(acc[d] = acc[d] || []).push(t) return acc }, {}) const handleDelete = async (id) => { if (!confirm("Удалить транзакцию?")) return await financeApi.deleteTransaction(id) setTransactions((txs) => txs.filter((t) => t.id !== id)) } if (loading) { return (
{[1, 2, 3, 4].map((i) => (
))}
) } return (
setSearch(e.target.value)} />
{[ ["all", "Все"], ["income", "Доходы"], ["expense", "Расходы"], ].map(([k, l]) => ( ))}
{categories.map((c) => ( ))}
{Object.keys(grouped).length === 0 ? (
🔍

Ничего не найдено

) : ( Object.entries(grouped).map(([date, txs]) => (

{formatDate(date)}

{txs.map((t) => (
handleDelete(t.id)} > {t.category_emoji}

{t.description || t.category_name}

{t.category_emoji} {t.category_name}

{t.type === "income" ? "+" : "-"} {fmt(t.amount)}
))}
)) )}
) }