fix?
This commit is contained in:
+55
-20
@@ -8,38 +8,73 @@ import { BlogT2 } from '../page';
|
|||||||
|
|
||||||
export default function Blog({ params }: { params: Promise<{ id: string }> }) {
|
export default function Blog({ params }: { params: Promise<{ id: string }> }) {
|
||||||
const [blog, setBlog] = useState<BlogT2 | null>(null);
|
const [blog, setBlog] = useState<BlogT2 | null>(null);
|
||||||
|
const [error, setError] = useState<string | null>(null);
|
||||||
|
|
||||||
useState(() => {
|
useEffect(() => {
|
||||||
params.then(({id}) => {
|
let isMounted = true;
|
||||||
fetch(`/api/${id}`).then((res) => {
|
params
|
||||||
res.json().then((data) => {
|
.then(({ id }) => {
|
||||||
setBlog(data);
|
return fetch(`/api/${id}`);
|
||||||
}).catch((err) => {
|
})
|
||||||
console.error(err);
|
.then((res) => {
|
||||||
});
|
if (!res.ok) {
|
||||||
|
throw new Error('Failed to fetch blog');
|
||||||
|
}
|
||||||
|
return res.json();
|
||||||
|
})
|
||||||
|
.then((data) => {
|
||||||
|
if (isMounted) setBlog(data);
|
||||||
|
})
|
||||||
|
.catch((err) => {
|
||||||
|
if (isMounted) setError(err.message);
|
||||||
|
console.error(err);
|
||||||
});
|
});
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
if (blog !== null) {
|
return () => {
|
||||||
|
isMounted = false;
|
||||||
|
};
|
||||||
|
}, [params]);
|
||||||
|
|
||||||
|
if (error) {
|
||||||
return (
|
return (
|
||||||
<main>
|
<main>
|
||||||
<a href="/blog"><h1 className='name'>Leo's blog</h1></a>
|
<a href="/blog"><h1 className="name">Leo's blog</h1></a>
|
||||||
<div className='blog_container'>
|
<div className="blog_container">
|
||||||
<h2 className='blog_title'>{blog.title}</h2>
|
<p className="error">Error loading blog: {error}</p>
|
||||||
<p className='date'>{new Date(blog.date).toLocaleString('default', { month: 'long', day: 'numeric', year: 'numeric' })}</p>
|
|
||||||
<div className='markdown'><MarkdownRenderer>{blog.markdown.replaceAll('\n-\n', '\n<br/>\n')}</MarkdownRenderer></div>
|
|
||||||
</div>
|
</div>
|
||||||
</main>
|
</main>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
else
|
|
||||||
|
if (blog !== null) {
|
||||||
return (
|
return (
|
||||||
<main>
|
<main>
|
||||||
<a href="/blog"><h1 className='name'>Leo's blog</h1></a>
|
<a href="/blog"><h1 className="name">Leo's blog</h1></a>
|
||||||
<div className='blog_container'>
|
<div className="blog_container">
|
||||||
<LoadingBlogRead/>
|
<h2 className="blog_title">{blog.title}</h2>
|
||||||
|
<p className="date">
|
||||||
|
{new Date(blog.date).toLocaleString('default', {
|
||||||
|
month: 'long',
|
||||||
|
day: 'numeric',
|
||||||
|
year: 'numeric',
|
||||||
|
})}
|
||||||
|
</p>
|
||||||
|
<div className="markdown">
|
||||||
|
<MarkdownRenderer>
|
||||||
|
{blog.markdown.replaceAll('\n-\n', '\n<br/>\n')}
|
||||||
|
</MarkdownRenderer>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</main>
|
</main>
|
||||||
);
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<main>
|
||||||
|
<a href="/blog"><h1 className="name">Leo's blog</h1></a>
|
||||||
|
<div className="blog_container">
|
||||||
|
<LoadingBlogRead />
|
||||||
|
</div>
|
||||||
|
</main>
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user