|
1 |
| -import { GetServerSideProps, NextPage } from 'next'; |
2 |
| -import React, { useEffect, useState } from 'react'; |
3 |
| -import { Post } from '../../../src/types/Post'; |
4 |
| -import { getPost } from '../../../src/db/posts'; |
| 1 | +import React from 'react'; |
| 2 | + |
| 3 | +import Head from 'next/head'; |
5 | 4 | import ReactMarkdown from 'react-markdown';
|
6 |
| -import CommentSection from '../../../src/components/Blog/CommentSection'; |
7 |
| -import PostComponent from '../../../src/components/Blog/Post'; |
8 | 5 | import { Breadcrumb } from 'flowbite-react';
|
9 | 6 | import { FaHome } from 'react-icons/fa';
|
10 |
| -import Head from 'next/head'; |
11 |
| -import { createPostComment } from '../../../src/db/comments'; |
| 7 | +import { GetServerSideProps, NextPage } from 'next'; |
12 | 8 |
|
13 |
| -export const getServerSideProps: GetServerSideProps = async ({ query }) => { |
14 |
| - const id = query?.id as string; |
| 9 | +import CommentSection from '@/components/Blog/CommentSection'; |
| 10 | +import PostComponent from '@/components/Blog/Post'; |
| 11 | +import { Post } from '@/types/Post'; |
| 12 | +import { createPostComment } from '@/db/comments'; |
| 13 | +import { getPost } from '@/db/posts'; |
15 | 14 |
|
16 |
| - return { |
17 |
| - props: { |
18 |
| - id, |
19 |
| - }, |
20 |
| - }; |
21 |
| -}; |
| 15 | +export const getServerSideProps: GetServerSideProps = async ({ query }) => { |
| 16 | + try { |
| 17 | + const id = query?.id; |
22 | 18 |
|
23 |
| -const BlogPost: NextPage<{ id: string }> = ({ id }) => { |
24 |
| - const [post, setPost] = useState<Post>(); |
| 19 | + const res = await getPost(id as string); |
| 20 | + const post = res.data; |
25 | 21 |
|
26 |
| - useEffect(() => { |
27 |
| - const fetchPost = async () => { |
28 |
| - const res = await getPost(id as string); |
29 |
| - setPost(JSON.parse(JSON.stringify(res.data))); |
| 22 | + return { |
| 23 | + props: { |
| 24 | + post: JSON.parse(JSON.stringify(post)), |
| 25 | + }, |
30 | 26 | };
|
31 |
| - fetchPost(); |
32 |
| - }, []); |
33 |
| - |
34 |
| - if (!post) return <div>Loading...</div>; |
| 27 | + } catch (error) { |
| 28 | + return { |
| 29 | + notFound: true, |
| 30 | + }; |
| 31 | + } |
| 32 | +}; |
35 | 33 |
|
| 34 | +const BlogPost: NextPage<{ post: Post }> = ({ post }) => { |
36 | 35 | return (
|
37 | 36 | <main className="pb-16 bg-white lg:pb-24 dark:bg-gray-900">
|
38 | 37 | <Head>
|
|
0 commit comments