Skip to content

Commit 36a13a5

Browse files
committed
refactor: Refactor ssg for post list, detail page
1 parent faf361a commit 36a13a5

File tree

13 files changed

+97
-99
lines changed

13 files changed

+97
-99
lines changed

prisma/initialize-data/admin.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { assertValue } from '@/lib/utils'
22
import { PrismaClient } from '@prisma/client'
3+
34
import bcrypt from 'bcryptjs'
45

56
export const createDefaultAdmin = async (prisma: PrismaClient) => {

src/app/api/post/[id]/comment/route.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { getServerAuth } from '@/lib/auth'
2-
import { NextResponseData, REVALIDATE_TAGS } from '@/lib/fetch'
2+
import { NextResponseData } from '@/lib/fetch'
33
import { mainPath } from '@/lib/route'
44
import { Comment } from '@prisma/client'
5-
import { revalidatePath, revalidateTag } from 'next/cache'
5+
import { revalidatePath } from 'next/cache'
66
import { NextRequest, NextResponse } from 'next/server'
77
import prisma from '../../../../../../prisma/prisma-client'
88

@@ -117,7 +117,6 @@ export const POST = async (request: NextRequest, { params }: Params) => {
117117
return NextResponse.json({ error: 'Failed to create comment' }, { status: 500 })
118118
}
119119

120-
revalidateTag(REVALIDATE_TAGS.POST_COMMENT)
121120
revalidatePath(mainPath.post.detail(postId))
122121

123122
return NextResponse.json({ comment: createdComment }, { status: 201 })
@@ -169,7 +168,6 @@ export const PATCH = async (request: NextRequest, { params }: Params) => {
169168
return NextResponse.json({ error: 'Failed to edit comment' }, { status: 500 })
170169
}
171170

172-
revalidateTag(REVALIDATE_TAGS.POST_COMMENT)
173171
revalidatePath(mainPath.post.detail(postId))
174172

175173
return NextResponse.json({ comment: editedComment })
@@ -220,7 +218,6 @@ export const DELETE = async (request: NextRequest, { params }: Params) => {
220218
return NextResponse.json({ error: 'Failed to delete comment' }, { status: 500 })
221219
}
222220

223-
revalidateTag(REVALIDATE_TAGS.POST_COMMENT)
224221
revalidatePath(mainPath.post.detail(postId))
225222

226223
return new NextResponse(null, { status: 204 })

src/app/post/[id]/layout.tsx

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,21 @@ interface Props {
99
}>
1010
}
1111

12-
export const generateMetadata = async ({ params }: Props): Promise<Metadata> => {
13-
const postId = (await params)?.id
14-
const responsePost = await serverRequestGetDetailPost({ postId: postId, isPublished: true })
12+
// export const generateMetadata = async ({ params }: Props): Promise<Metadata> => {
13+
// const postId = (await params)?.id
14+
// const responsePost = await serverRequestGetDetailPost({ postId: postId, isPublished: true })
1515

16-
if ('error' in responsePost) {
17-
return {
18-
title: '방은수 블로그',
19-
}
20-
}
16+
// if ('error' in responsePost) {
17+
// return {
18+
// title: '방은수 블로그',
19+
// }
20+
// }
2121

22-
return {
23-
title: responsePost.post.title,
24-
description: responsePost.post.summary,
25-
}
26-
}
22+
// return {
23+
// title: responsePost.post.title,
24+
// description: responsePost.post.summary,
25+
// }
26+
// }
2727

2828
const DetailPostLayout: FC<PropsWithChildren> = ({ children }) => {
2929
return (

src/app/post/[id]/page.tsx

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { serverRequestGetDetailPost } from '@/entities/post'
1+
import { serverRequestGetDetailPost, serverRequestGetSomePostList } from '@/entities/post'
22
import { UserCommentWidget, DetailPostWidget, PostNavigationWidget } from '@/widgets/post/detail'
33
import type { FC } from 'react'
44

@@ -35,18 +35,18 @@ const DetailPostPage: FC<Props> = async ({ params }) => {
3535

3636
export default DetailPostPage
3737

38-
// export const generateStaticParams = async () => {
39-
// const responsePosts = await serverRequestGetSomePostList({
40-
// curPage: 1,
41-
// perPage: 5,
42-
// isPublished: true,
43-
// })
44-
45-
// if ('error' in responsePosts) {
46-
// return []
47-
// }
48-
49-
// return responsePosts?.posts.map((post) => ({
50-
// id: post?.id || '',
51-
// }))
52-
// }
38+
export const generateStaticParams = async () => {
39+
const responsePosts = await serverRequestGetSomePostList({
40+
curPage: 1,
41+
perPage: 5,
42+
isPublished: true,
43+
})
44+
45+
if ('error' in responsePosts) {
46+
return []
47+
}
48+
49+
return responsePosts?.posts.map((post) => ({
50+
id: post?.id || '',
51+
}))
52+
}

src/entities/post-comment/post-comment.api.client.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ import {
77
} from '@/app/api/post/[id]/comment/route'
88
import { generateRequest } from '@/lib/fetch'
99

10+
export const requestGetCommentList = async ({ postId }: { postId: string }) => {
11+
return generateRequest<undefined, ResponseGetPostCommentListType>({
12+
url: `/post/${postId}/comment`,
13+
})
14+
}
15+
1016
export const requestCreatePostComment = async ({
1117
postId,
1218
comment,

src/entities/post-comment/post-comment.api.server.ts

Lines changed: 0 additions & 13 deletions
This file was deleted.

src/entities/post/post.api.server.ts

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -75,41 +75,41 @@ export const serverRequestGetPostListBySearch = async (keyword: string) => {
7575
})
7676
}
7777

78-
export const serverRequestGetDetailPost = async ({ postId, isPublished }: { postId: string; isPublished: boolean }) => {
79-
if (!postId) {
80-
throw new Error(`Post id must be required`)
81-
}
82-
83-
const params = new URLSearchParams()
84-
params.append('isPublished', isPublished.toString())
85-
86-
return generateRequest<undefined, ResponseGetDetailPostType>({
87-
url: `/post/${postId}?${params.toString()}`,
88-
})
89-
}
90-
9178
// export const serverRequestGetDetailPost = async ({ postId, isPublished }: { postId: string; isPublished: boolean }) => {
9279
// if (!postId) {
9380
// throw new Error(`Post id must be required`)
9481
// }
9582

96-
// try {
97-
// const post = await prisma.post.findFirst({
98-
// where: {
99-
// id: postId,
100-
// ...(isPublished && { order: { not: null } }),
101-
// },
102-
// })
83+
// const params = new URLSearchParams()
84+
// params.append('isPublished', isPublished.toString())
10385

104-
// if (!post) {
105-
// throw new Error(`Post not found`)
106-
// }
86+
// return generateRequest<undefined, ResponseGetDetailPostType>({
87+
// url: `/post/${postId}?${params.toString()}`,
88+
// })
89+
// }
10790

108-
// console.log('success detail post title :', post.title)
91+
export const serverRequestGetDetailPost = async ({ postId, isPublished }: { postId: string; isPublished: boolean }) => {
92+
if (!postId) {
93+
throw new Error(`Post id must be required`)
94+
}
10995

110-
// return { post }
111-
// } catch (error) {
112-
// console.error('❌ serverRequestGetDetailPost error:', error)
113-
// return { error: 'Internal Server Error' }
114-
// }
115-
// }
96+
try {
97+
const post = await prisma.post.findFirst({
98+
where: {
99+
id: postId,
100+
...(isPublished && { order: { not: null } }),
101+
},
102+
})
103+
104+
if (!post) {
105+
throw new Error(`Post not found`)
106+
}
107+
108+
console.log('success detail post title :', post.title)
109+
110+
return { post }
111+
} catch (error) {
112+
console.error('❌ serverRequestGetDetailPost error:', error)
113+
return { error: 'Internal Server Error' }
114+
}
115+
}

src/features/post/detail/comment/comment-list.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ import type { FC } from 'react'
22
import { CommentItem } from './comment-item'
33
import { getServerAuth } from '@/lib/auth'
44
import { PostComment } from '@/entities/post-comment/post-comment.types'
5+
import { useAdminSession } from '@/lib/hooks'
56

67
interface Props {
78
comments: PostComment[]
89
}
910

10-
export const CommentList: FC<Props> = async ({ comments }) => {
11-
const { user } = await getServerAuth()
12-
const currentUserId = user?.['@id']
11+
export const CommentList: FC<Props> = ({ comments }) => {
12+
const { sessionUserId } = useAdminSession()
1313

1414
const reducedComment = comments.reduce<PostComment[]>((acc, cur) => {
1515
if (!cur.parentId) {
@@ -31,7 +31,7 @@ export const CommentList: FC<Props> = async ({ comments }) => {
3131
<CommentItem
3232
key={comment?.id}
3333
comment={comment}
34-
currentUserId={currentUserId}
34+
currentUserId={sessionUserId}
3535
/>
3636
))}
3737
</section>

src/lib/fetch/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
export * from './error-codes'
22
export * from './return-type'
33
export * from './client-instance'
4-
export * from './revalidate-tags'

src/lib/fetch/revalidate-tags.ts

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)