1
+ import { useRouter } from "next/router" ;
2
+ import { useState } from "react" ;
3
+ import { parseCookies } from "nookies" ;
4
+ import Navbar from "@/components/Navbar" ;
5
+
6
+ export default function Post ( ) {
7
+ const router = useRouter ( ) ;
8
+ const [ title , setTitle ] = useState ( "" ) ;
9
+ const [ content , setContent ] = useState ( "" ) ;
10
+
11
+ const handleKeyDown = ( e : any ) => {
12
+ // Reset field height
13
+ e . target . style . height = 'inherit' ;
14
+
15
+ // Get the computed styles for the element
16
+ const computed = window . getComputedStyle ( e . target ) ;
17
+
18
+ // Calculate the height
19
+ const height = parseInt ( computed . getPropertyValue ( 'border-top-width' ) , 10 )
20
+ + parseInt ( computed . getPropertyValue ( 'padding-top' ) , 10 )
21
+ + e . target . scrollHeight
22
+ + parseInt ( computed . getPropertyValue ( 'padding-bottom' ) , 10 )
23
+ + parseInt ( computed . getPropertyValue ( 'border-bottom-width' ) , 10 ) ;
24
+
25
+ e . target . style . height = `${ height } px` ;
26
+ }
27
+
28
+ const handleSubmit = async ( e : React . FormEvent < HTMLFormElement > ) => {
29
+ e . preventDefault ( ) ;
30
+ const cookies = parseCookies ( )
31
+
32
+ const response = await fetch ( "/api/createPosts" , {
33
+ method : "POST" ,
34
+ headers : {
35
+ "Content-Type" : "application/json" ,
36
+ "Authorization" : cookies . token
37
+ } ,
38
+ body : JSON . stringify ( { title, content} ) ,
39
+ } ) ;
40
+ if ( response . ok ) {
41
+ router . push ( "/" )
42
+ }
43
+ }
44
+ return (
45
+ < div className = { "w-full min-h-screen h-fit flex flex-col" } >
46
+ < div >
47
+ < Navbar />
48
+ </ div >
49
+ < div className = { "w-full h-full p-8 flex flex-col gap-10" } >
50
+ < div className = { "flex flex-col gap-2" } >
51
+ < h1 className = { "text-4xl md:text-7xl font-semibold" } > Create a new post</ h1 >
52
+ < h1 className = { "text-[#909099] md:ml-1" } > Say something dank to the world</ h1 >
53
+ </ div >
54
+ < form className = { "w-full md:w-1/2 flex flex-col gap-5" } onSubmit = { handleSubmit } >
55
+ < div className = { "flex flex-col gap-2" } >
56
+ < label htmlFor = "title" > Title</ label >
57
+ < input
58
+ className = { "text-white px-4 py-2 outline-0 bg-transparent" }
59
+ required = { true }
60
+ placeholder = { "Why did the chicken cross the road?" }
61
+ type = "text" id = "title" value = { title }
62
+ onChange = { ( e ) => setTitle ( e . target . value ) } />
63
+ </ div >
64
+ < div className = { "flex flex-col gap-2" } >
65
+ < label htmlFor = "content" > Content</ label >
66
+ < textarea
67
+ className = { "text-white px-4 py-2 outline-0 bg-transparent w-full h-auto resize-none" }
68
+ required = { true }
69
+ placeholder = { "To get to the other side" }
70
+ id = "content" value = { content }
71
+ onChange = { ( e ) => setContent ( e . target . value ) }
72
+ onKeyDown = { handleKeyDown }
73
+ />
74
+ </ div >
75
+ < div className = { "flex flex-col gap-2 absolute bottom-2 left-2 right-2" } >
76
+ < button className = { "px-4 py-2 rounded-lg bg-white text-black font-semibold" } > Create
77
+ Post
78
+ </ button >
79
+ </ div >
80
+ </ form >
81
+ </ div >
82
+ </ div >
83
+ )
84
+ }
0 commit comments