import { useState, useEffect } from 'react'; import type { Account } from '@family-budget/shared'; import { getAccounts, updateAccount } from '../api/accounts'; export function AccountsList() { const [accounts, setAccounts] = useState([]); const [loading, setLoading] = useState(true); const [editingId, setEditingId] = useState(null); const [editAlias, setEditAlias] = useState(''); useEffect(() => { setLoading(true); getAccounts() .then(setAccounts) .catch(() => {}) .finally(() => setLoading(false)); }, []); const handleEdit = (account: Account) => { setEditingId(account.id); setEditAlias(account.alias || ''); }; const handleSave = async (id: number) => { try { const updated = await updateAccount(id, { alias: editAlias.trim(), }); setAccounts((prev) => prev.map((a) => (a.id === id ? updated : a)), ); setEditingId(null); } catch { // error handled globally } }; if (loading) { return
Загрузка...
; } return (
{accounts.map((a) => ( ))} {accounts.length === 0 && ( )}
Банк Номер счёта Валюта Алиас
{a.bank} {a.accountNumberMasked} {a.currency} {editingId === a.id ? ( setEditAlias(e.target.value)} maxLength={50} autoFocus onKeyDown={(e) => { if (e.key === 'Enter') handleSave(a.id); if (e.key === 'Escape') setEditingId(null); }} /> ) : ( a.alias || ( не задан ) )} {editingId === a.id ? (
) : ( )}
Нет счетов. Импортируйте выписку.
); }