From dea7645fb876dd28c0fedc8ed4f5008182ff0eda Mon Sep 17 00:00:00 2001 From: Bakhtiyor Ganijon Date: Thu, 26 Oct 2023 00:54:54 +0900 Subject: [PATCH] Update post editor --- app/api/posts/[username]/[url]/route.ts | 6 +++++- components/editor/post-editor-form.tsx | 20 +++++++++++--------- components/navbar/editor-nav.tsx | 7 ++++--- 3 files changed, 20 insertions(+), 13 deletions(-) diff --git a/app/api/posts/[username]/[url]/route.ts b/app/api/posts/[username]/[url]/route.ts index 7754cdf1..a1410324 100644 --- a/app/api/posts/[username]/[url]/route.ts +++ b/app/api/posts/[username]/[url]/route.ts @@ -35,7 +35,11 @@ export async function GET( }, include: { comments: true, - tags: true, + tags: { + include: { + tag: true, + } + }, likes: true, savedUsers: true, author: { diff --git a/components/editor/post-editor-form.tsx b/components/editor/post-editor-form.tsx index b22afb3a..4d7e0d2d 100644 --- a/components/editor/post-editor-form.tsx +++ b/components/editor/post-editor-form.tsx @@ -43,6 +43,8 @@ export function PostEditorForm(props: { post: any }) { const sessionUser = useSession().data?.user as any; const [user, setUser] = useState(null) const router = useRouter(); + const [markdownContent, setMarkdownContent] = useState(''); + setMarkdownContent(props.post?.content) useEffect(() => { async function fetchData() { @@ -78,7 +80,7 @@ export function PostEditorForm(props: { post: any }) { ) .optional(), url: z.string(), - description: z.string().max(280).optional(), + subtitle: z.string().max(280).optional(), }) @@ -90,7 +92,10 @@ const defaultValues: Partial = { visibility: props.post?.visibility, coverImage: props.post?.coverimage || '', url: props.post?.url, - description: props.post?.description, + subtitle: props.post?.subtitle, + tags: props.post?.tags?.map((tag: any) => ({ + value: tag.tag?.name, + })), } const form = useForm({ @@ -166,7 +171,7 @@ const defaultValues: Partial = { async function validateUrl(value: string) { try { // Check if the url is already taken - const result = await fetch(`/api/posts/validate-url?url=${value}&authorId=${user?.userid}`, { + const result = await fetch(`/api/posts/validate-url?url=${value}&authorId=${user?.id}`, { method: 'GET', }); @@ -189,10 +194,7 @@ const defaultValues: Partial = { validateUrl(value); // Update the form field value form.setValue('url', value); - } - - const [markdownContent, setMarkdownContent] = useState(''); - + } async function handleContentChange(value: string) { form.setValue('content', value); // Update the form field value @@ -224,7 +226,7 @@ const defaultValues: Partial = { } function handleDescriptionChange(e: React.ChangeEvent) { - form.setValue('description', e.target.value); + form.setValue('subtitle', e.target.value); } return ( @@ -388,7 +390,7 @@ const defaultValues: Partial = { ( Post Description diff --git a/components/navbar/editor-nav.tsx b/components/navbar/editor-nav.tsx index 37c791a4..b122bd1d 100644 --- a/components/navbar/editor-nav.tsx +++ b/components/navbar/editor-nav.tsx @@ -8,9 +8,10 @@ import { AvatarImage, } from "@/components/ui/avatar" -import { useSession } from "next-auth/react"; +import { getSession, useSession } from "next-auth/react"; import { useEffect, useState } from "react"; import { getUserByUsername } from "../get-user"; +import { getSessionUser } from "../get-session-user"; export default function EditorNavbar() { const user = useSession().data?.user as any; @@ -19,8 +20,8 @@ export default function EditorNavbar() { useEffect(() => { async function fetchData() { try { - const userData = await getUserByUsername(user?.name); - setUsername(userData.username); + const userData = await getSessionUser(); + setUsername(userData?.username); } catch (error) { // Handle errors console.error('Error:', error);