54 lines
1.4 KiB
TypeScript
54 lines
1.4 KiB
TypeScript
import React from "react";
|
|
import { Page } from './_util';
|
|
import { useParams } from 'react-router-dom';
|
|
import { LegalMarkdown } from '../components/legal-markdown';
|
|
|
|
export default function LegalPage() {
|
|
const { page } = useParams();
|
|
const [loading, setLoading] = React.useState(true);
|
|
const [title, setTitle] = React.useState('');
|
|
const [body, setBody] = React.useState('');
|
|
|
|
React.useEffect(() => {
|
|
if (!page) {
|
|
return;
|
|
}
|
|
const controller = new AbortController();
|
|
|
|
async function loadLegal() {
|
|
try {
|
|
setLoading(true);
|
|
const res = await fetch(`/api/v1/legal/${encodeURIComponent(page)}?lang=de`, {
|
|
headers: { 'Cache-Control': 'no-store' },
|
|
signal: controller.signal,
|
|
});
|
|
if (!res.ok) {
|
|
throw new Error('failed');
|
|
}
|
|
const data = await res.json();
|
|
setTitle(data.title || '');
|
|
setBody(data.body_markdown || '');
|
|
} catch (error) {
|
|
if (!controller.signal.aborted) {
|
|
console.error('Failed to load legal page', error);
|
|
setTitle('');
|
|
setBody('');
|
|
}
|
|
} finally {
|
|
if (!controller.signal.aborted) {
|
|
setLoading(false);
|
|
}
|
|
}
|
|
}
|
|
|
|
loadLegal();
|
|
return () => controller.abort();
|
|
}, [page]);
|
|
|
|
return (
|
|
<Page title={title || `Rechtliches: ${page}` }>
|
|
{loading ? <p>Laedt...</p> : <LegalMarkdown markdown={body} />}
|
|
</Page>
|
|
);
|
|
}
|