diff --git a/Data/Server/WebUI/src/nodes/Data Collection/Node_API_Request.jsx b/Data/Server/WebUI/src/nodes/Data Collection/Node_API_Request.jsx index 3f46a43..eda504d 100644 --- a/Data/Server/WebUI/src/nodes/Data Collection/Node_API_Request.jsx +++ b/Data/Server/WebUI/src/nodes/Data Collection/Node_API_Request.jsx @@ -1,17 +1,17 @@ ////////// PROJECT FILE SEPARATION LINE ////////// CODE AFTER THIS LINE ARE FROM: /Data/WebUI/src/nodes/Data Collection/Node_API_Request.jsx import React, { useState, useEffect, useRef } from "react"; -import { Handle, Position, useReactFlow } from "reactflow"; +import { Handle, Position, useReactFlow, useStore } from "reactflow"; const APIRequestNode = ({ id, data }) => { const { setNodes } = useReactFlow(); + const edges = useStore((state) => state.edges); + if (!window.BorealisValueBus) window.BorealisValueBus = {}; - // Stored URL (actual fetch target) const [url, setUrl] = useState(data?.url || "http://localhost:5000/health"); - // Editable URL text const [editUrl, setEditUrl] = useState(data?.url || "http://localhost:5000/health"); - const [useProxy, setUseProxy] = useState(data?.useProxy || true); + const [useProxy, setUseProxy] = useState(data?.useProxy ?? true); const [body, setBody] = useState(data?.body || ""); const [intervalSec, setIntervalSec] = useState(data?.intervalSec ?? 10); const [error, setError] = useState(null); @@ -24,11 +24,7 @@ const APIRequestNode = ({ id, data }) => { const flag = e.target.checked; setUseProxy(flag); setNodes((nds) => - nds.map((n) => - n.id === id - ? { ...n, data: { ...n.data, useProxy: flag } } - : n - ) + nds.map((n) => (n.id === id ? { ...n, data: { ...n.data, useProxy: flag } } : n)) ); }; @@ -48,26 +44,20 @@ const APIRequestNode = ({ id, data }) => { ); }; - // ... other handlers unchanged ... const handleBodyChange = (e) => { const newBody = e.target.value; setBody(newBody); setNodes((nds) => - nds.map((n) => - n.id === id - ? { ...n, data: { ...n.data, body: newBody } } - : n - ) + nds.map((n) => (n.id === id ? { ...n, data: { ...n.data, body: newBody } } : n)) ); }; + const handleIntervalChange = (e) => { const sec = parseInt(e.target.value, 10) || 1; setIntervalSec(sec); setNodes((nds) => nds.map((n) => - n.id === id - ? { ...n, data: { ...n.data, intervalSec: sec } } - : n + n.id === id ? { ...n, data: { ...n.data, intervalSec: sec } } : n ) ); }; @@ -78,32 +68,38 @@ const APIRequestNode = ({ id, data }) => { const runNodeLogic = async () => { try { setError(null); - let target = url; - if (useProxy) { - target = `/api/proxy?url=${encodeURIComponent(url)}`; + + const inputEdge = edges.find((e) => e.target === id); + const upstreamUrl = inputEdge ? window.BorealisValueBus[inputEdge.source] : null; + if (upstreamUrl && upstreamUrl !== editUrl) { + setEditUrl(upstreamUrl); } + + const resolvedUrl = upstreamUrl || url; + let target = useProxy ? `/api/proxy?url=${encodeURIComponent(resolvedUrl)}` : resolvedUrl; + const options = {}; if (body.trim()) { options.method = "POST"; options.headers = { "Content-Type": "application/json" }; options.body = body; } + const res = await fetch(target, options); setStatusCode(res.status); setStatusText(res.statusText); + if (!res.ok) { - // clear on error resultRef.current = null; window.BorealisValueBus[id] = undefined; setNodes((nds) => nds.map((n) => - n.id === id - ? { ...n, data: { ...n.data, result: undefined } } - : n + n.id === id ? { ...n, data: { ...n.data, result: undefined } } : n ) ); throw new Error(`HTTP ${res.status}`); } + const json = await res.json(); const pretty = JSON.stringify(json, null, 2); if (!cancelled && resultRef.current !== pretty) { @@ -111,9 +107,7 @@ const APIRequestNode = ({ id, data }) => { window.BorealisValueBus[id] = json; setNodes((nds) => nds.map((n) => - n.id === id - ? { ...n, data: { ...n.data, result: pretty } } - : n + n.id === id ? { ...n, data: { ...n.data, result: pretty } } : n ) ); } @@ -130,25 +124,50 @@ const APIRequestNode = ({ id, data }) => { cancelled = true; clearInterval(iv); }; - }, [url, body, intervalSec, useProxy, id, setNodes]); + }, [url, body, intervalSec, useProxy, id, setNodes, edges]); + + const inputEdge = edges.find((e) => e.target === id); + const hasUpstream = Boolean(inputEdge && inputEdge.source); return (
+ +
API Request
- {/* URL input */} - - {/* Proxy toggle */}
{
- {/* body & interval unchanged... */} -