11"use client"
22
33import React , { useState } from "react"
4+ import dynamic from "next/dynamic"
45import Link from "next/link"
5- import { useParams } from "next/navigation"
6+ import { useParams , useRouter } from "next/navigation"
67
78import { zodResolver } from "@hookform/resolvers/zod"
89import { createPost , Prisma , updatePost } from "database"
@@ -11,20 +12,21 @@ import { useTranslations } from "next-intl"
1112import { Controller , useForm } from "react-hook-form"
1213import AsyncCreatableSelect from "react-select/async-creatable"
1314import { toast } from "react-toastify"
14- import { Button , buttonVariants , cn , Label , LoadingButton } from "ui"
15+ import { buttonVariants , cn , Label , LoadingButton } from "ui"
1516import z from "zod"
1617
1718import APP_ROUTES from "@/constants/routes"
1819import InputTitle from "@/molecules/input-title"
1920import { TPostItem } from "@/types/posts"
2021
21- import Editor from "../editor-js"
22+ const Editor = dynamic ( ( ) => import ( "../editor-js" ) , { ssr : false } )
2223
2324const PostForm = ( { post : postData } : { post ?: TPostItem } ) => {
2425 const { title = "" , content = "" , tagOnPost = [ ] } = postData || { }
2526 const t = useTranslations ( )
2627 const session = useSession ( )
2728 const [ pending , setPending ] = useState ( false )
29+ const router = useRouter ( )
2830
2931 const userId = session ?. data ?. user ?. id
3032
@@ -68,24 +70,27 @@ const PostForm = ({ post: postData }: { post?: TPostItem }) => {
6870 } )
6971
7072 const handleSubmitPost = async ( data ) => {
73+ let newPostId = postId as string
7174 try {
7275 setPending ( true )
7376 if ( postId ) {
7477 await updatePost ( postId as string , data , userId )
7578 toast . success ( t ( "common.post_created" ) )
7679 } else {
77- await createPost ( data , userId )
80+ const post = await createPost ( data , userId )
81+ newPostId = post . data . id
7882 toast . success ( t ( "common.post_updated" ) )
7983 }
8084 } catch ( error ) {
8185 toast . error ( error . message )
8286 } finally {
87+ router . push ( APP_ROUTES . POST . replace ( ":postId" , newPostId ) )
8388 setPending ( false )
8489 }
8590 }
8691
8792 const promiseOptions = async ( inputValue : string ) => {
88- const rawData = await fetch ( " /api/protected/tags?search=" + inputValue )
93+ const rawData = await fetch ( ` /api/protected/tags?search=${ inputValue } ` )
8994 const tags = await rawData . json ( )
9095
9196 return tags . map ( ( tag ) => ( {
@@ -117,26 +122,27 @@ const PostForm = ({ post: postData }: { post?: TPostItem }) => {
117122 )}
118123 </div>
119124 </div> */ }
120- { JSON . stringify ( errors ) }
121125 < form
122126 className = "mb-4 w-full"
123127 onSubmit = { handleSubmit ( handleSubmitPost ) }
124128 >
125129 < div className = "grid grid-cols-4 gap-8" >
126130 < div className = "col-span-3 mb-4 w-full rounded-md" >
127131 < div className = "w-full" >
128- < Controller
129- name = "title"
130- control = { control }
131- render = { ( { field } ) => (
132- < InputTitle
133- placeholder = { t ( "common.untitled" ) }
134- { ...field }
135- />
136- ) }
137- />
132+ < div className = "px-[58px]" >
133+ < Controller
134+ name = "title"
135+ control = { control }
136+ render = { ( { field } ) => (
137+ < InputTitle
138+ placeholder = { t ( "common.untitled" ) }
139+ { ...field }
140+ />
141+ ) }
142+ />
143+ </ div >
138144
139- < div className = "mt-3 rounded " >
145+ < div className = "mt-3" >
140146 < Controller
141147 name = "content"
142148 control = { control }
0 commit comments