1
1
"use client"
2
- import svPost from "@/ utils/svPost" ;
2
+ import svPost from "../../../ utils/svPost"
3
3
import Link from "next/link" ;
4
4
import Comment from "@/components/Comment" ;
5
5
import Parser from 'html-react-parser'
6
+ import Image from "next/image" ;
7
+ import EagleEmoji from '../../../../public/eagle_emoji.png'
6
8
import CmtForm from "@/components/CmtForm" ;
7
9
import { getRelativeTime } from "@/utils/getRelativeTime" ;
10
+ import axios from "axios" ;
8
11
import { GoTrash } from "react-icons/go" ;
9
- import { IPost , IComment } from "@/types" ;
10
- import LikeButton from "@/components/LikeButton" ;
12
+ import { useEffect } from "react" ;
11
13
type Props = {
12
14
params : { id : string } ;
13
15
} ;
14
- // interface getILike{
15
- // like: number
16
- // }
17
16
export default async function Post ( { params } : Props ) {
18
17
const id : string = params . id ;
19
- const { getPost, getLike, getComment, delPost } = svPost ( ) ;
18
+ const { getPost, getLike, getComment, delPost } = await svPost ( ) ;
19
+ useEffect ( aysnc ( ) = > {
20
+ const info : any = await getPost ( id ) ;
21
+ const likes : any = await getLike ( id ) ;
22
+ const comment : any = await getComment ( id ) ;
23
+ const author_url = "/user/" + info . author . id ;
20
24
21
- const info : IPost = await getPost ( id ) ;
22
- const likes : number = await getLike ( id ) ;
23
- console . log ( likes )
24
- const comment : IComment [ ] = await getComment ( id ) ;
25
- const author_url = "/user/" + info . author . id ;
26
- const handleDelPost = async ( ) => {
27
- let token : string | null = null
25
+ } , [ ] )
26
+ const handleLikeButton = async ( e : React . FormEvent ) => {
27
+ e . preventDefault ( ) ;
28
+ try {
29
+ // Send a POST request to your FastAPI backend
30
+ let token : any
31
+ if ( typeof window !== 'undefined' )
32
+ token = localStorage . getItem ( 'jwt' )
33
+ await axios . post ( `http://spezbe.hungnq.online/likes/?post_id=${ id } ` ,
34
+ {
35
+ headers : {
36
+ 'accept' : 'application/json' ,
37
+ 'Authorization' : `Bearer ${ token } `
38
+ }
39
+ }
40
+ ) ;
41
+
42
+ // Redirect or give feedback upon success
43
+ // alert('Post created successfully!');
44
+ } catch ( error ) {
45
+ console . error ( 'Error posting data:' , error ) ;
46
+ alert ( 'Failed to create like.' ) ;
47
+ }
48
+ } ;
49
+ const handleDelPost = async ( e : React . FormEvent ) => {
50
+ let token : any
28
51
if ( typeof window !== 'undefined' )
29
52
token = localStorage . getItem ( 'jwt' )
30
- await delPost ( id , token ) ;
53
+ const deletePost :any = await delPost ( id , token ) ;
54
+ console . log ( deletePost )
31
55
32
56
}
33
57
return (
@@ -45,22 +69,28 @@ export default async function Post({ params }: Props) {
45
69
< div className = "pl-2 text-2xl font-bold" > { info . title } </ div >
46
70
< div className = "p-2" > { Parser ( info . content ) } </ div >
47
71
< div className = "flex flex-row" >
48
- < LikeButton initialLikes = { likes } post_id = { id } />
72
+ < button onClick = { handleLikeButton } className = "w-auto flex flex-row border-2 border-black rounded-full px-2" >
73
+ < p className = "pt-[0.35rem]" > Ưng</ p >
74
+ < Image
75
+ src = { EagleEmoji }
76
+ alt = "eagle_img"
77
+ width = { 30 }
78
+ height = { 50 } />
79
+ < p className = "pt-[0.35rem]" > : { likes } </ p >
80
+ </ button >
49
81
< div className = "w-auto mt-[0.35rem] ml-5 border-2 rounded-full px-2 border-black" > Phản hồi: { comment . length } </ div >
50
- </ div >
82
+ </ div >
51
83
< CmtForm postId = { id } />
52
84
</ div >
53
85
< div className = "border-2 border-slate-950 p-2 text-black rounded shadow h-full w-1/2 space-y-5" >
54
86
< a className = "font-bold text-2xl" > Bình Loạn</ a >
55
- { comment . map ( ( element : IComment ) => (
87
+ { comment . map ( ( e : any ) => (
56
88
< Comment
57
- key = { element . id }
58
- id = { element . id }
59
- author = { element . author }
60
- content = { element . content }
61
- created_at = { element . created_at }
62
- updated_at = { element . updated_at }
63
- post_id = { element . post_id }
89
+ // author_id = {comment.author}
90
+ id = { e . id }
91
+ author = { e . author }
92
+ content = { e . content }
93
+ date = { e . created_at }
64
94
/>
65
95
) ) }
66
96
</ div >
0 commit comments