From fee216fc19b5c39cf11b025d4fc00a2a5e8bce70 Mon Sep 17 00:00:00 2001 From: romanskijdev Date: Thu, 15 Oct 2026 11:06:01 +0300 Subject: [PATCH] Added 'create node' button to flow tree --- src/pages/flowNodes/FlowTreePage.tsx | 85 +++++++++++++++++++ .../flowNodes/components/SortableFlowTree.tsx | 21 ++++- 2 files changed, 104 insertions(+), 2 deletions(-) diff --git a/src/pages/flowNodes/FlowTreePage.tsx b/src/pages/flowNodes/FlowTreePage.tsx index 60c65b9..85f2c64 100644 --- a/src/pages/flowNodes/FlowTreePage.tsx +++ b/src/pages/flowNodes/FlowTreePage.tsx @@ -29,6 +29,10 @@ import CategoryCreateModal from "@/pages/flowNodes/components/CategoryCreateModa import FlowNodeEditModal from "@/pages/flowNodes/components/FlowNodeEditModal.tsx"; import SortableFlowTree from "@/pages/flowNodes/components/SortableFlowTree.tsx"; import TreeHealthPanel from "@/pages/flowNodes/components/TreeHealthPanel.tsx"; +import {notifications} from "@mantine/notifications"; +import apiClient from "@/api/client.ts"; +import {flowNodesApi} from "@/api/flowNodes.ts"; +import {topicsApi} from "@/api/topics.ts"; export default function FlowTreePage() { const [selectedCategoryId, setSelectedCategoryId] = useState(null) @@ -122,6 +126,86 @@ export default function FlowTreePage() { }) } + const handleConfigureTopic = async (node: ApprovalFlowNode) => { + if (!selectedCategoryId || !node.id) return + + const collected: Record = {} + const nodeMap = new Map() + + const walk = (nodes: ApprovalFlowNode[]) => { + for (const n of nodes) { + if (n.id) nodeMap.set(n.id, n) + if (n.children) walk(n.children) + } + } + walk(tree) + + let current = nodeMap.get(node.id) + while (current) { + if (current.data_key && current.data_value) { + collected[current.data_key] = current.data_value + } + if (current.multi_select_key && current.multi_select_value) { + collected[current.multi_select_key] = current.multi_select_value + } + current = current.parent_id ? nodeMap.get(current.parent_id) : undefined + } + + try { + const response = await flowNodesApi.preview(selectedCategoryId, { collected }) + const previewData = response.data + + if (previewData.incomplete) { + notifications.show({ + title: 'Недостаточно данных', + message: 'Невозможно настроить маршрут: на этом уровне не хватает данных для формирования кода темы.', + color: 'orange' + }) + return + } + + // 3. Если темы с таким кодом еще нет, создаем её + if (!previewData.topic_exists) { + await topicsApi.create({ + category_id: selectedCategoryId, + code: previewData.code, + name: `${selectedCategory?.name}: ${node.label} (Узел)`, + is_active: true, + allow_approver_selection: false, + allow_observer_selection: false, + allow_delegation_selection: false + }) + + notifications.show({ + title: 'Успех', + message: 'Тема для этого узла успешно создана в базе.', + color: 'green' + }) + } + + modals.openConfirmModal({ + title: 'Настройка иерархии', + children: ( + + Тема для каскадного согласования готова. Хотите перейти на страницу настройки иерархии прямо сейчас? + + ), + labels: { confirm: 'Перейти', cancel: 'Настроить позже' }, + onConfirm: () => { + navigate('/hierarchies') + }, + }) + + } catch (error) { + console.error('Ошибка при настройке маршрута:', error) + notifications.show({ + title: 'Ошибка', + message: 'Не удалось подготовить маршрут. Проверьте консоль.', + color: 'red' + }) + } + } + const handleDuplicate = (node: ApprovalFlowNode) => duplicateFlowNode.mutate(node.id!) return ( @@ -232,6 +316,7 @@ export default function FlowTreePage() { onAddChild={handleAddChild} onDelete={handleDelete} onDuplicate={handleDuplicate} + onConfigureTopic={handleConfigureTopic} /> )} diff --git a/src/pages/flowNodes/components/SortableFlowTree.tsx b/src/pages/flowNodes/components/SortableFlowTree.tsx index cb5f2b3..e0c741c 100644 --- a/src/pages/flowNodes/components/SortableFlowTree.tsx +++ b/src/pages/flowNodes/components/SortableFlowTree.tsx @@ -23,6 +23,7 @@ import { IconChevronDown, IconChevronRight, IconArrowsMove, + IconRoute, } from '@tabler/icons-react' import type { ApprovalFlowNode } from '@/types/api' import { useReorderNodes } from '@/features/flowNodes/hooks/useFlowNodes' @@ -36,6 +37,7 @@ interface SortableFlowTreeProps { onAddChild: (parent: ApprovalFlowNode) => void onDelete: (node: ApprovalFlowNode) => void onDuplicate: (node: ApprovalFlowNode) => void + onConfigureTopic: (node: ApprovalFlowNode) => void } function nodeIcon(node: ApprovalFlowNode): string { @@ -50,7 +52,7 @@ function nodeKindLabel(node: ApprovalFlowNode): string { return 'Кнопка' } -export default function SortableFlowTree({ tree, onEdit, onAddChild, onDelete, onDuplicate }: SortableFlowTreeProps) { +export default function SortableFlowTree({ tree, onEdit, onAddChild, onDelete, onDuplicate, onConfigureTopic }: SortableFlowTreeProps) { const [collapsed, setCollapsed] = useState>(new Set()) const [activeId, setActiveId] = useState(null) const [overId, setOverId] = useState(null) @@ -150,6 +152,7 @@ export default function SortableFlowTree({ tree, onEdit, onAddChild, onDelete, o onAddChild={onAddChild} onDelete={onDelete} onDuplicate={onDuplicate} + onConfigureTopic={onConfigureTopic} /> ))} @@ -178,9 +181,10 @@ interface SortableRowProps { onAddChild: (parent: ApprovalFlowNode) => void onDelete: (node: ApprovalFlowNode) => void onDuplicate: (node: ApprovalFlowNode) => void + onConfigureTopic: (node: ApprovalFlowNode) => void } -function SortableRow({ node, hasChildren, collapsed, onToggleCollapse, onEdit, onAddChild, onDelete, onDuplicate }: SortableRowProps) { +function SortableRow({ node, hasChildren, collapsed, onToggleCollapse, onEdit, onAddChild, onDelete, onDuplicate, onConfigureTopic }: SortableRowProps) { const isRoot = node.depth === 0 const { attributes, listeners, setNodeRef, transform, transition, isDragging } = useSortable({ id: node.id!, @@ -241,6 +245,19 @@ function SortableRow({ node, hasChildren, collapsed, onToggleCollapse, onEdit, o + {hasChildren && ( + + onConfigureTopic(node)} + > + + + + )} + onAddChild(node)}>