From 2c682bcc125d282910f659a44f79815c554ae2b8 Mon Sep 17 00:00:00 2001 From: ryanwong Date: Mon, 18 Nov 2024 13:30:35 -0500 Subject: [PATCH] set default values --- src/components/ConfigPanel.tsx | 159 ++++++++++++++++++++++++++------- 1 file changed, 127 insertions(+), 32 deletions(-) diff --git a/src/components/ConfigPanel.tsx b/src/components/ConfigPanel.tsx index c211dc4..1002bcc 100644 --- a/src/components/ConfigPanel.tsx +++ b/src/components/ConfigPanel.tsx @@ -15,6 +15,84 @@ interface Field { validation?: string; } +const getDefaultDataForType = (type: string) => { + const baseData = { + label: type.charAt(0).toUpperCase() + type.slice(1).replace("-", " ") + }; + + switch (type) { + case "variable": + return { + ...baseData, + name: "", + type: "string", + defaultValue: "" + }; + + case "url": + return { + ...baseData, + method: "GET", + path: "", + fields: [], + queryFields: [] + }; + + case "auth": + return { + ...baseData, + authType: "bearer", + tokenVar: "" + }; + + case "output": + return { + ...baseData, + outputType: "definition", + statusCode: 200, + fields: [], + responseRaw: "" + }; + + case "logic": + return { + ...baseData, + code: "" + }; + + case "db-find": + case "db-query": + return { + ...baseData, + model: "", + operation: "findMany", + query: "", + resultVar: "result" + }; + + case "db-insert": + return { + ...baseData, + model: "", + variables: "", + resultVar: "result" + }; + + case "db-update": + case "db-delete": + return { + ...baseData, + model: "", + idField: "id", + variables: "", + resultVar: "result" + }; + + default: + return baseData; + } +}; + export function ConfigPanel({ node, onClose, onUpdateNode }: ConfigPanelProps) { const { models, updateNode } = useFlowStore(); const [newField, setNewField] = useState({ name: "", type: "string" }); @@ -23,6 +101,23 @@ export function ConfigPanel({ node, onClose, onUpdateNode }: ConfigPanelProps) { type: "string", }); + useEffect(() => { + if (node) { + // Initialize node data with defaults if not already set + const defaultData = getDefaultDataForType(node.type); + const newData = { + ...defaultData, + ...node.data // This will override defaults with any existing data + }; + + // Only update if the data is different + if (JSON.stringify(newData) !== JSON.stringify(node.data)) { + onUpdateNode(node.id, newData); + updateNode(node.id, newData); + } + } + }, [node?.id, node?.type]); + useEffect(() => { console.log("ConfigPanel re-rendered with node:", node); }, [node]); @@ -118,7 +213,7 @@ export function ConfigPanel({ node, onClose, onUpdateNode }: ConfigPanelProps) { @@ -147,7 +242,7 @@ export function ConfigPanel({ node, onClose, onUpdateNode }: ConfigPanelProps) {