1
-
2
- "use client"
3
- import React , { useState } from 'react' ;
1
+ "use client" ;
2
+ import React , { useState } from "react" ;
4
3
// import dynamic from 'next/dynamic';
5
- import SunEditor from ' suneditor-react' ;
6
- import ' suneditor/dist/css/suneditor.min.css' ;
7
- import axios from ' axios' ;
8
- import { useRouter } from ' next/navigation' ;
4
+ import SunEditor from " suneditor-react" ;
5
+ import " suneditor/dist/css/suneditor.min.css" ;
6
+ import axios from " axios" ;
7
+ import { useRouter } from " next/navigation" ;
9
8
10
9
// Dynamically import SunEditor to avoid SSR issues
11
10
// const DynamicSunEditor = dynamic(() => import('suneditor-react'), {
12
11
// ssr: false,
13
12
// });
14
13
15
14
type Props = {
16
- postId : string
17
- }
18
- export default function CmtForm ( { postId} : Props ) {
19
- const [ content , setContent ] = useState < string > ( '' ) ;
15
+ postId : string ;
16
+ } ;
17
+ export default function CmtForm ( { postId } : Props ) {
18
+ const [ content , setContent ] = useState < string > ( "" ) ;
20
19
const router = useRouter ( ) ;
21
20
22
21
// Handle form submission
23
22
const handleSubmit = async ( e : React . FormEvent ) => {
24
23
e . preventDefault ( ) ;
25
24
try {
26
25
// Send a POST request to your FastAPI backend
27
- let token : string | null
28
- if ( typeof window !== 'undefined' )
29
- token = localStorage . getItem ( 'jwt' )
30
- if ( token === undefined )
31
- router . push ( '/' )
26
+ let token : string | null = null ;
27
+ if ( typeof window !== "undefined" ) token = localStorage . getItem ( "jwt" ) ;
28
+ if ( token === undefined ) router . push ( "/" ) ;
32
29
// console.log(token)
33
- await axios . post ( `http://localhost:8000/posts/cmt/${ postId } ` ,
30
+ await axios . post (
31
+ `http://localhost:8000/posts/cmt/${ postId } ` ,
34
32
{
35
-
36
- content : content ,
37
- } ,
38
- {
39
- headers : {
40
- 'accept' : 'application/json' ,
41
- 'Authorization' : `Bearer ${ token } `
33
+ content : content ,
34
+ } ,
35
+ {
36
+ headers : {
37
+ accept : "application/json" ,
38
+ Authorization : `Bearer ${ token } ` ,
39
+ } ,
42
40
}
43
- }
44
- ) ;
41
+ ) ;
45
42
46
43
// Redirect or give feedback upon success
47
- alert ( ' Post created successfully!' ) ;
44
+ alert ( " Post created successfully!" ) ;
48
45
} catch ( error ) {
49
- console . error ( ' Error posting data:' , error ) ;
50
- alert ( ' Failed to create post.' ) ;
46
+ console . error ( " Error posting data:" , error ) ;
47
+ alert ( " Failed to create post." ) ;
51
48
}
52
49
} ;
53
50
@@ -63,17 +60,27 @@ export default function CmtForm ({postId}: Props) {
63
60
setOptions = { {
64
61
height : "200" ,
65
62
buttonList : [
66
- [ 'formatBlock' , 'bold' , 'underline' , 'italic' , 'list' , 'link' , 'image' ] ,
63
+ [
64
+ "formatBlock" ,
65
+ "bold" ,
66
+ "underline" ,
67
+ "italic" ,
68
+ "list" ,
69
+ "link" ,
70
+ "image" ,
71
+ ] ,
67
72
] ,
68
73
} }
69
74
/>
70
75
</ div >
71
76
72
- < button type = "submit" className = "bg-blue-500 text-white px-4 py-1 rounded-full left-2/3" >
77
+ < button
78
+ type = "submit"
79
+ className = "bg-blue-500 text-white px-4 py-1 rounded-full left-2/3"
80
+ >
73
81
Tạo bình loạn
74
82
</ button >
75
83
</ form >
76
84
</ div >
77
85
) ;
78
- } ;
79
-
86
+ }
0 commit comments