import { useState, useEffect } from 'react'; import type { CategoryRule } from '@family-budget/shared'; import { getCategoryRules, updateCategoryRule, applyRule, } from '../api/rules'; import { formatDate } from '../utils/format'; export function RulesList() { const [rules, setRules] = useState([]); const [loading, setLoading] = useState(true); const [applyingId, setApplyingId] = useState(null); const [applyResult, setApplyResult] = useState<{ id: number; count: number; } | null>(null); useEffect(() => { setLoading(true); getCategoryRules() .then(setRules) .catch(() => {}) .finally(() => setLoading(false)); }, []); const handleToggle = async (rule: CategoryRule) => { try { const updated = await updateCategoryRule(rule.id, { isActive: !rule.isActive, }); setRules((prev) => prev.map((r) => (r.id === rule.id ? updated : r)), ); } catch { // error handled globally } }; const handleApply = async (id: number) => { setApplyingId(id); try { const resp = await applyRule(id); setApplyResult({ id, count: resp.applied }); setTimeout(() => setApplyResult(null), 4000); } catch { // error handled globally } finally { setApplyingId(null); } }; if (loading) { return
Загрузка...
; } return (
{rules.map((r) => ( ))} {rules.length === 0 && ( )}
Шаблон Категория Приоритет Подтверждение Создано Активно
{r.pattern} {r.categoryName} {r.priority} {r.requiresConfirmation ? 'Да' : 'Нет'} {formatDate(r.createdAt)}
{r.isActive && ( )} {applyResult?.id === r.id && ( Применено: {applyResult.count} )}
Нет правил
); }