feat(nodeToolbarComponent): add functionality to download node as JSON file

feat(reactflowUtils): add functions to create a flow component and download a node as a JSON file

The changes in `nodeToolbarComponent` include importing the `downloadNode` function from `reactflowUtils` and adding a case in the switch statement to call the `downloadNode` function when the action is "SaveAll". This allows the user to download the node as a JSON file.

In `reactflowUtils`, two new functions are added. The `createFlowComponent` function takes in node data and creates a flow component with the necessary structure. The `downloadNode` function takes a flow component and converts it into a JSON file that can be downloaded by the user.
This commit is contained in:
anovazzi1 2023-09-22 15:18:52 -03:00
commit 9f7f6c84aa
3 changed files with 40 additions and 2 deletions

View file

@ -11,6 +11,10 @@ import {
import { TabsContext } from "../../../../contexts/tabsContext"; import { TabsContext } from "../../../../contexts/tabsContext";
import EditNodeModal from "../../../../modals/EditNodeModal"; import EditNodeModal from "../../../../modals/EditNodeModal";
import { nodeToolbarPropsType } from "../../../../types/components"; import { nodeToolbarPropsType } from "../../../../types/components";
import {
createFlowComponent,
downloadNode,
} from "../../../../utils/reactflowUtils";
import { classNames } from "../../../../utils/utils"; import { classNames } from "../../../../utils/utils";
export default function NodeToolbarComponent({ export default function NodeToolbarComponent({
@ -62,7 +66,8 @@ export default function NodeToolbarComponent({
updateNodeInternals(data.id); updateNodeInternals(data.id);
break; break;
case "SaveAll": case "SaveAll":
console.log("SaveAll"); downloadNode(createFlowComponent(data));
break;
} }
}; };

View file

@ -7,6 +7,7 @@ export type FlowType = {
data: ReactFlowJsonObject | null; data: ReactFlowJsonObject | null;
description: string; description: string;
style?: FlowStyleType; style?: FlowStyleType;
isNode?: boolean;
}; };
export type NodeType = { export type NodeType = {
id: string; id: string;

View file

@ -8,7 +8,7 @@ import {
} from "reactflow"; } from "reactflow";
import { specialCharsRegex } from "../constants/constants"; import { specialCharsRegex } from "../constants/constants";
import { APITemplateType } from "../types/api"; import { APITemplateType } from "../types/api";
import { FlowType, NodeType } from "../types/flow"; import { FlowType, NodeDataType, NodeType } from "../types/flow";
import { import {
cleanEdgesType, cleanEdgesType,
unselectAllNodesType, unselectAllNodesType,
@ -365,3 +365,35 @@ export function convertValuesToNumbers(arr) {
return newObj; return newObj;
}); });
} }
export function createFlowComponent(nodeData: NodeDataType): FlowType {
const flowNode: FlowType = {
data: {
edges: [],
nodes: [
{
data: nodeData,
id: nodeData.id,
position: { x: 0, y: 0 },
type: "genericNode",
},
],
viewport: { x: 1, y: 1, zoom: 1 },
},
description: nodeData.node?.description || "",
name: nodeData.node?.display_name || "",
id: nodeData.id || "",
isNode: true,
};
return flowNode;
}
export function downloadNode(NodeFLow: FlowType) {
const element = document.createElement("a");
const file = new Blob([JSON.stringify(NodeFLow)], {
type: "application/json",
});
element.href = URL.createObjectURL(file);
element.download = `${NodeFLow.name}.json`;
element.click();
}