Skip to content

Commit

Permalink
[report] load json embed in script tag
Browse files Browse the repository at this point in the history
  • Loading branch information
barroco committed Aug 17, 2023
1 parent 9d1812b commit c27f44a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
8 changes: 4 additions & 4 deletions reports/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import "./App.css";
function App() {
// FIXME use the report from the config
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const configuration = JSON.parse((window as any)["interuss"] || "{}");
const configuration = JSON.parse(document.getElementById("interuss_report_json")?.innerHTML || "{}");
console.log("Configuration:", configuration);

const { loading, report, nav } = useReport(configuration);
const { loading, error, report, nav } = useReport(configuration);
if (loading) {
return <div>Loading report...</div>;
}
if (!report) {
return <div>Report not found</div>;
if (!report || error) {
return !error ? <div>Report not found</div> : <div>{error}</div>;
}
const router = createHashRouter(nav);
return <RouterProvider router={router} />;
Expand Down
24 changes: 18 additions & 6 deletions reports/src/capability/useReport.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type UseReportProps = {

type UseReportReturn = {
loading: boolean;
error?: string;
report?: ReportsReportTestRunReport;
nav: RouteObject[];
};
Expand All @@ -20,7 +21,8 @@ export const useReport = ({
report: _report,
}: UseReportProps): UseReportReturn => {
const [loading, setLoading] = useState<boolean>(true);
const [report, setReport] = useState<ReportsReportTestRunReport>();
const [error, setError] = useState<string>();
const [report, setReport] = useState<ReportsReportTestRunReport | undefined>(_report);

useEffect(() => {
if (_report) {
Expand All @@ -30,10 +32,20 @@ export const useReport = ({
}

const fetchReport = async () => {
const res = await fetch(reportUrl);
const json = await res.json();
setReport(json as ReportsReportTestRunReport);
setLoading(false);
try {
const res = await fetch(reportUrl);
if (res.status === 404) {
throw new Error("Report not found")
}
const json = await res.json();
setReport(json as ReportsReportTestRunReport);

} catch (err) {
console.error(err);
setError(JSON.stringify(err));
} finally {
setLoading(false);
}
};
fetchReport();
}, []);
Expand All @@ -46,5 +58,5 @@ export const useReport = ({
() => (parsedReport ? getNavFromCapability(parsedReport.capability) : []),
[parsedReport]
);
return { loading, report, nav };
return {loading, error, report, nav};
};

0 comments on commit c27f44a

Please sign in to comment.