import React, { useState } from 'react';
import { useReportData } from './DataContext';
import { useNomenclatureData } from '../../contexts/NomenclatureDataContext';
import './ConfigPanel.css';
function ConfigPanel({ apiEndpoint, onApiEndpointChange, isOpen, onClose }) {
const { reportData, isLoading, error, fetchData, setData, clearData } = useReportData();
const { definitions, entries } = useNomenclatureData();
const [localEndpoint, setLocalEndpoint] = useState(apiEndpoint || '');
const [manualDataInput, setManualDataInput] = useState('');
const [showManualInput, setShowManualInput] = useState(false);
const handleFetch = async () => {
if (localEndpoint.trim()) {
onApiEndpointChange(localEndpoint);
await fetchData(localEndpoint);
}
};
const handleSetManualData = () => {
const trimmedInput = manualDataInput.trim();
if (!trimmedInput) {
alert('Please enter some JSON data first');
return;
}
try {
const data = JSON.parse(trimmedInput);
setData(data);
setShowManualInput(false);
setManualDataInput('');
} catch (err) {
alert('Invalid JSON: ' + err.message);
}
};
const handleClear = () => {
clearData();
setLocalEndpoint('');
onApiEndpointChange('');
};
if (!isOpen) return null;
return (
e.stopPropagation()}>
Report Configuration
{/* API Endpoint Section */}
API Endpoint
setLocalEndpoint(e.target.value)}
disabled={isLoading}
/>
{error && (
Error: {error}
)}
{/* Manual Data Input Section */}
{showManualInput && (
)}
{/* Data Preview Section */}
{reportData && (
Current Data
{JSON.stringify(reportData, null, 2)}
)}
{!reportData && !isLoading && !error && (
No data loaded. Enter an API endpoint to fetch data, or use manual input.
)}
);
}
export default ConfigPanel;