import React, { useState, useEffect } from 'react'; import { FaTimes } from 'react-icons/fa'; import './Graph.css'; export default function Graph({ open, onClose, payload, onError }) { const [graphHtml, setGraphHtml] = useState(""); const [loading, setLoading] = useState(true); const [error, setError] = useState(""); useEffect(() => { // if (open && payload) { if (open) { setLoading(true); setError(""); fetch("/action/graph", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify() // body: JSON.stringify(payload) }) .then(res => res.json()) .then(data => { // If the API returns an error, throw it to be caught below. if (data.error) { throw new Error(data.error); } setGraphHtml(data.result); setLoading(false); }) .catch(err => { console.error("Error fetching graph:", err); const errMsg = err.message || "Error fetching graph."; setError(errMsg); setLoading(false); // Propagate error to parent using the onError callback. if (onError && typeof onError === 'function') { onError(errMsg); } }); } }, [open, onError]); // }, [open, payload, onError]); if (!open) return null; return (
Loading Graph...
{error}
) : ( )}