Skip to content

Commit 933cb66

Browse files
committed
feat: remove added image (cont)
1 parent b8867ce commit 933cb66

File tree

4 files changed

+27
-60
lines changed

4 files changed

+27
-60
lines changed

apps/web/@/molecules/post-form/index.tsx

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import Image from "next/image"
66
import Link from "next/link"
77

88
import { zodResolver } from "@hookform/resolvers/zod"
9-
import { Image as ImageType, Prisma } from "database"
9+
import { Image as ImageType, Prisma, TPostItem } from "database"
1010
import { Upload as UploadIcon, X } from "lucide-react"
1111
import { useSession } from "next-auth/react"
1212
import { useTranslations } from "next-intl"
@@ -18,18 +18,17 @@ import z from "zod"
1818
import { handleCreateUpdatePost } from "@/actions/protect/postAction"
1919
import APP_ROUTES from "@/constants/routes"
2020
import InputTitle from "@/molecules/input-title"
21-
import { TPostItem } from "@/types/posts"
2221

2322
import Upload from "../upload"
24-
import AssetManagement from "../upload/AssetsManagement"
2523

2624
const Editor = dynamic(() => import("../editor-js"), { ssr: false })
2725

2826
const PostForm = ({ post: postData }: { post?: TPostItem }) => {
2927
const { title = "", content = "", tagOnPost = [], id: postId } = postData || {}
28+
3029
const t = useTranslations()
3130
const session = useSession()
32-
const [image, setImage] = useState<ImageType | null>(null)
31+
const [image, setImage] = useState<ImageType | null>(postData?.Image)
3332

3433
const userId = session?.data?.user?.id
3534

@@ -81,7 +80,18 @@ const PostForm = ({ post: postData }: { post?: TPostItem }) => {
8180
}
8281

8382
const onSubmit = async (data) =>
84-
await handleCreateUpdatePost({ postId: postId as string, data, userId })
83+
await handleCreateUpdatePost({
84+
postId: postId as string,
85+
data: {
86+
...data,
87+
Image: {
88+
connect: {
89+
id: image?.id,
90+
},
91+
},
92+
},
93+
userId,
94+
})
8595

8696
return (
8797
<div className="w-full">

apps/web/@/types/posts.ts

Lines changed: 0 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,5 @@
11
import { PostOnUserType, Prisma } from "database"
22

3-
export const postSelect = {
4-
id: true,
5-
title: true,
6-
content: true,
7-
createdAt: true,
8-
updatedAt: true,
9-
slug: true,
10-
postStatus: true,
11-
totalLike: true,
12-
totalFollow: true,
13-
author: {
14-
select: {
15-
id: true,
16-
name: true,
17-
email: true,
18-
image: true,
19-
},
20-
},
21-
postOnUser: {
22-
select: {
23-
type: true,
24-
userId: true,
25-
postId: true,
26-
},
27-
},
28-
_count: {
29-
select: {
30-
comments: true,
31-
postOnUser: true,
32-
},
33-
},
34-
tagOnPost: {
35-
select: {
36-
tag: {
37-
select: {
38-
id: true,
39-
name: true,
40-
slug: true,
41-
},
42-
},
43-
},
44-
},
45-
} satisfies Prisma.PostSelect
46-
47-
const getPostItem = Prisma.validator<Prisma.PostDefaultArgs>()({
48-
select: postSelect,
49-
})
50-
51-
export type TPostItem = Prisma.PostGetPayload<typeof getPostItem>
52-
533
export type TCreatePostInput = Prisma.PostCreateInput & {
544
tags: {
555
value: string
Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
import { Metadata } from "next"
22

3-
import { getPostById } from "@/actions/protect/posts"
3+
import { getPost } from "database"
4+
45
import PostForm from "@/molecules/post-form"
56

67
export async function generateMetadata({ params }): Promise<Metadata> {
7-
const post = await getPostById(params?.postId as string)
8+
const post = await getPost({ postIdOrSlug: params?.postId as string })
89

910
return {
10-
title: post?.title,
11+
title: post?.data?.title,
1112
description: "", // post?.content.slice(0, 160),
1213
}
1314
}
1415

1516
export default async function Page({ params }: { params: { postId: string } }) {
16-
const post = await getPostById(params?.postId as string)
17+
const post = await getPost({ postIdOrSlug: params?.postId as string })
1718

18-
return <PostForm post={post} />
19+
return <PostForm post={post?.data} />
1920
}

packages/database/src/posts/selects.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ export const postSelect = {
1010
postStatus: true,
1111
totalLike: true,
1212
totalFollow: true,
13+
Image: {
14+
select: {
15+
id: true,
16+
url: true,
17+
},
18+
},
1319
author: {
1420
select: {
1521
id: true,

0 commit comments

Comments
 (0)