import { CloseOutlined, DeleteOutlined } from '@ant-design/icons'; import { css, cx } from '@emotion/css'; import { ISchema, useForm } from '@formily/react'; import { Registry } from '@nocobase/utils'; import { Button, message, Modal, Tag } from 'antd'; import React, { useContext } from 'react'; import { useTranslation } from 'react-i18next'; import { SchemaComponent, useActionContext, useAPIClient, useCollection, useCompile, useRecord, useRequest, useResourceActionContext } from '../..'; import { nodeBlockClass, nodeCardClass, nodeClass, nodeHeaderClass, nodeMetaClass, nodeTitleClass } from '../style'; import { AddButton, useFlowContext } from '../WorkflowCanvas'; import calculation from './calculation'; import condition from './condition'; import create from './create'; import destroy from './destroy'; import parallel from './parallel'; import query from './query'; import update from './update'; export interface Instruction { title: string; type: string; group: string; options?: { label: string; value: any; key: string }[]; fieldset: { [key: string]: ISchema }; view?: ISchema; scope?: { [key: string]: any }; components?: { [key: string]: any }; render?(props): React.ReactElement; endding?: boolean; getter?(node: any): React.ReactElement; }; export const instructions = new Registry(); instructions.register('query', query); instructions.register('create', create); instructions.register('update', update); instructions.register('destroy', destroy); instructions.register('condition', condition); instructions.register('parallel', parallel); instructions.register('calculation', calculation); function useUpdateAction() { const { t } = useTranslation(); const form = useForm(); const api = useAPIClient(); const ctx = useActionContext(); const { refresh } = useResourceActionContext(); const data = useNodeContext(); const { workflow } = useFlowContext(); return { async run() { if (workflow.executed) { message.error(t('Node in executed workflow cannot be modified')); return; } await form.submit(); await api.resource('flow_nodes', data.id).update({ filterByTk: data.id, values: { title: form.values.title, config: form.values.config } }); ctx.setVisible(false); refresh(); }, }; }; const NodeContext = React.createContext(null); export function useNodeContext() { return useContext(NodeContext); } export function Node({ data }) { const instruction = instructions.get(data.type); return (
{instruction.render ? instruction.render(data) : } {!instruction.endding ? : (
) }
); } export function RemoveButton() { const { t } = useTranslation(); const api = useAPIClient(); const { workflow } = useFlowContext(); const resource = api.resource('workflows.nodes', workflow.id); const current = useNodeContext(); const { nodes, onNodeRemoved } = useFlowContext(); async function onRemove() { async function onOk() { const { data: { data: node } } = await resource.destroy({ filterByTk: current.id }); onNodeRemoved(node); } const hasBranches = !nodes.find(item => item.upstream === current && item.branchIndex != null); const message = hasBranches ? t('Are you sure you want to delete it?') : t('This node contains branches, deleting will also be preformed to them, are you sure?'); Modal.confirm({ title: t('Delete'), content: message, onOk }); } return workflow.executed ? null : (