1
1
'use client' ;
2
2
import React , { useEffect , useState } from 'react' ;
3
3
import { useSession } from 'next-auth/react' ;
4
- import { useParams } from 'next/navigation' ;
4
+ import { useRouter , useParams } from 'next/navigation' ;
5
5
import { PostTypes } from '@/types/postTypes' ;
6
6
import Post from '@/components/Post' ;
7
7
import fetchOnePost from './actions/fetchOnePost' ;
@@ -18,19 +18,22 @@ const PostPage = () => {
18
18
const [ post , setPost ] = useState < PostTypes | null > ( null ) ;
19
19
// セッションの取得
20
20
const session = useSession ( ) ;
21
+ const router = useRouter ( ) ;
21
22
22
23
// 投稿IDから投稿をFetchする
23
24
useEffect ( ( ) => {
24
25
const getPost = async ( ) => {
26
+ if ( ! postId ) return ;
25
27
if ( session . status === 'loading' ) return ;
26
28
const data = await fetchOnePost ( {
27
29
postId : postId as string ,
28
30
iconUrl : session . data ?. user ?. image ?? '' ,
29
31
} ) ;
32
+ if ( ! data ) router . push ( '/post-not-found' ) ;
30
33
setPost ( data ) ;
31
34
} ;
32
35
getPost ( ) ;
33
- } , [ postId , session . data ?. user ?. image , session . status ] ) ;
36
+ } , [ postId , session . data ?. user ?. image , session . status , router ] ) ;
34
37
35
38
return (
36
39
< div >
@@ -44,7 +47,7 @@ const PostPage = () => {
44
47
transition = { { duration : 0.5 } }
45
48
className = 'mx-auto max-w-sm pt-5 lg:max-w-lg'
46
49
>
47
- { post && < Post post = { post } /> }
50
+ { post && < Post post = { post } onDelete = { ( ) => router . push ( '/' ) } /> }
48
51
</ motion . div >
49
52
) }
50
53
</ AnimatePresence >
0 commit comments