Skip to content

Commit

Permalink
Update post editor
Browse files Browse the repository at this point in the history
  • Loading branch information
thebkht committed Oct 25, 2023
1 parent 3b1931f commit dea7645
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 13 deletions.
6 changes: 5 additions & 1 deletion app/api/posts/[username]/[url]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ export async function GET(
},
include: {
comments: true,
tags: true,
tags: {
include: {
tag: true,
}
},
likes: true,
savedUsers: true,
author: {
Expand Down
20 changes: 11 additions & 9 deletions components/editor/post-editor-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ export function PostEditorForm(props: { post: any }) {
const sessionUser = useSession().data?.user as any;
const [user, setUser] = useState<any | null>(null)
const router = useRouter();
const [markdownContent, setMarkdownContent] = useState<string>('');
setMarkdownContent(props.post?.content)

useEffect(() => {
async function fetchData() {
Expand Down Expand Up @@ -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(),
})


Expand All @@ -90,7 +92,10 @@ const defaultValues: Partial<PostFormValues> = {
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<PostFormValues>({
Expand Down Expand Up @@ -166,7 +171,7 @@ const defaultValues: Partial<PostFormValues> = {
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',
});

Expand All @@ -189,10 +194,7 @@ const defaultValues: Partial<PostFormValues> = {
validateUrl(value);
// Update the form field value
form.setValue('url', value);
}

const [markdownContent, setMarkdownContent] = useState<string>('');

}

async function handleContentChange(value: string) {
form.setValue('content', value); // Update the form field value
Expand Down Expand Up @@ -224,7 +226,7 @@ const defaultValues: Partial<PostFormValues> = {
}

function handleDescriptionChange(e: React.ChangeEvent<HTMLTextAreaElement>) {
form.setValue('description', e.target.value);
form.setValue('subtitle', e.target.value);
}

return (
Expand Down Expand Up @@ -388,7 +390,7 @@ const defaultValues: Partial<PostFormValues> = {

<FormField
control={form.control}
name="description"
name="subtitle"
render={({ field }) => (
<FormItem>
<FormLabel>Post Description</FormLabel>
Expand Down
7 changes: 4 additions & 3 deletions components/navbar/editor-nav.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down

0 comments on commit dea7645

Please sign in to comment.