1
1
"use client"
2
- import svPost from "@/services/svPost" ;
3
- import svCmt from "@/services/svCmt" ;
2
+ import { svPost } from "@/services/svPost" ;
4
3
import Link from "next/link" ;
5
4
import Comment from "@/components/Comment" ;
6
5
import Parser from 'html-react-parser'
@@ -9,37 +8,26 @@ import { getRelativeTime } from "@/utils/getRelativeTime";
9
8
import { GoTrash } from "react-icons/go" ;
10
9
import { IPost , IComment } from "@/types" ;
11
10
import LikeButton from "@/components/LikeButton" ;
12
- import svLike from "@/services/svLike" ;
11
+ // import svLike from "@/services/svLike";
13
12
import { useEffect , useState } from "react" ;
13
+ import usePost from "@/hooks/usePost" ;
14
+ import useComments from "@/hooks/useComments" ;
15
+ import useLikes from "@/hooks/useLikes" ;
14
16
type Props = {
15
17
params : { id : string } ;
16
18
} ;
17
19
export default function Post ( { params } : Props ) {
18
- const [ info , setInfo ] = useState < IPost > ( )
19
- const [ likes , setLikes ] = useState < number | null > ( null )
20
- const [ comment , setComments ] = useState < IComment [ ] | undefined > ( )
21
20
const id : string = params . id ;
22
- const { getPost, delPost } = svPost ( ) ;
23
- const { getComment} = svCmt ( ) ;
24
- const { getLike} = svLike ( )
25
- useEffect ( ( ) => {
26
- async function fetching ( ) {
27
- let res = await getPost ( id ) ;
28
- setInfo ( res )
29
- res = await getLike ( id ) ;
30
- setLikes ( res ) ;
31
- res = await getComment ( id ) ;
32
- setComments ( res ) ;
33
- }
34
- fetching ( )
35
- } , [ ] )
36
- const author_url = "/user/" + info ?. author . id ;
21
+ const { post, isLoadingInfo} = usePost ( id )
22
+ const { Comments, isLoadingComments} = useComments ( id )
23
+ const { Likes, isLoadingLikes} = useLikes ( id )
24
+ const { delPost} = svPost ( )
25
+ const author_url = "/user/" + post ?. author . id ;
37
26
const handleDelPost = async ( ) => {
38
27
let token : string | null = null
39
28
if ( typeof window !== 'undefined' )
40
29
token = localStorage . getItem ( 'jwt' )
41
30
await delPost ( id , token ) ;
42
-
43
31
}
44
32
return (
45
33
< >
@@ -48,22 +36,22 @@ export default function Post({ params }: Props) {
48
36
< div className = "pl-10 flex flex-row justify-between" >
49
37
< div className = "w-auto" >
50
38
51
- < Link href = { author_url } className = "font-semibold" > { info ?. author . username } </ Link >
52
- { " " } { info ?. created_at ? getRelativeTime ( info ?. created_at ) : null }
39
+ < Link href = { author_url } className = "font-semibold" > { post ?. author . username } </ Link >
40
+ { " " } { post ?. created_at ? getRelativeTime ( post ?. created_at ) : null }
53
41
</ div >
54
42
< button onClick = { handleDelPost } className = "mr-8 text-red-600 text-semibold" > < GoTrash /> </ button >
55
43
</ div >
56
- < div className = "pl-2 text-2xl font-bold" > { info ?. title } </ div >
57
- < div className = "p-2" > { info ?. content ? Parser ( info ?. content ) : null } </ div >
44
+ < div className = "pl-2 text-2xl font-bold" > { post ?. title } </ div >
45
+ < div className = "p-2" > { post ?. content ? Parser ( post ?. content ) : null } </ div >
58
46
< div className = "flex flex-row" >
59
- < LikeButton initialLikes = { likes } post_id = { id } />
60
- < div className = "w-auto mt-[0.35rem] ml-5 border-2 rounded-full px-2 border-black" > Phản hồi: { comment ?. length } </ div >
47
+ < LikeButton initialLikes = { Likes } post_id = { id } />
48
+ < div className = "w-auto mt-[0.35rem] ml-5 border-2 rounded-full px-2 border-black" > Phản hồi: { Comments ?. length } </ div >
61
49
</ div >
62
50
< CmtForm postId = { id } />
63
51
</ div >
64
52
< div className = "border-2 border-slate-950 p-2 text-black rounded shadow h-full w-1/2 space-y-5" >
65
53
< a className = "font-bold text-2xl" > Bình Loạn</ a >
66
- { comment ?. map ( ( element : IComment ) => (
54
+ { Comments ?. map ( ( element : IComment ) => (
67
55
< Comment
68
56
key = { element . id }
69
57
id = { element . id }
0 commit comments