Skip to content

Commit a36972f

Browse files
committed
feat: refactor author page
1 parent beb0c53 commit a36972f

File tree

8 files changed

+88
-32
lines changed

8 files changed

+88
-32
lines changed

apps/web/@/molecules/follower/user-profile/index.tsx

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,23 @@
11
import Link from "next/link"
2+
import { notFound } from "next/navigation"
23

4+
import { getUser } from "database"
35
import { getTranslations } from "next-intl/server"
46
import { Avatar, AvatarFallback, AvatarImage, Card, CardContent, CardHeader, Typography } from "ui"
57

6-
import APP_APIS from "@/constants/apis"
7-
import { TUserItem } from "@/types/users"
8-
import { generatePath } from "@/utils/generatePath"
9-
108
import FollowButton from "./follow-button"
119

1210
export type UserProfileProps = {
1311
authorId: string
1412
}
1513

1614
export async function UserProfile({ authorId }: UserProfileProps) {
17-
// const rawAuthor = await fetch(
18-
// `${process.env.NEXT_PUBLIC_FRONTEND_URL}${generatePath(APP_APIS.protected.user.GET, {
19-
// userId: authorId,
20-
// })}`,
21-
// {
22-
// method: "GET",
23-
// cache: "no-cache",
24-
// headers: {
25-
// "Content-Type": "application/json",
26-
// },
27-
// }
28-
// )
29-
const author: TUserItem = await rawAuthor?.json()
3015
const t = await getTranslations()
16+
const { data: author, error } = await getUser({ userId: authorId })
17+
18+
if (error) {
19+
return notFound()
20+
}
3121

3222
return (
3323
<div className="col-span-4">
@@ -58,13 +48,13 @@ export async function UserProfile({ authorId }: UserProfileProps) {
5848
</Typography>
5949
<div className="mt-4 flex w-full flex-1 divide-x">
6050
<div className="flex flex-1 flex-col items-center justify-center">
61-
<div className="font-bold">{author?._count?.post}</div>
51+
<div className="font-bold">{author?.totalPost}</div>
6252
<div className="hover:underline">
6353
<Link href={`/author/${author?.id}`}>{t("common.posts")}</Link>
6454
</div>
6555
</div>
6656
<div className="flex flex-1 flex-col items-center justify-center">
67-
<div className="font-bold">{author?._count?.followers}</div>
57+
<div className="font-bold">{author?.totalFollower}</div>
6858
<div className="hover:underline">
6959
<Link href={`/author/${author?.id}/followers`}>{t("common.followers")}</Link>
7060
</div>
Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,28 @@
1+
import { getUser } from "database"
2+
13
import UserProfile from "@/molecules/follower/user-profile"
24
import PostList from "@/molecules/posts/post-list"
35

4-
export const generateMetadata = async ({ params, searchParams }) => {
6+
export const generateMetadata = async ({ params }) => {
7+
const { data: author, error } = await getUser({ userId: params?.authorId })
8+
59
return {
6-
title: "Author",
7-
description: "",
10+
title: author?.name,
11+
description: author?.bio,
812
}
913
}
1014

1115
export default async function Page({ params, searchParams }) {
1216
return (
1317
<div className="grid grid-cols-12 gap-10">
14-
{/* <UserProfile authorId={params?.authorId} /> */}
15-
<div className="col-span-8 rounded-md">
16-
<PostList
17-
getPostParams={{
18-
authorId: params?.authorId,
19-
...searchParams,
20-
}}
21-
/>
22-
</div>
18+
<UserProfile authorId={params?.authorId} />
19+
<PostList
20+
containerClassName="mt-0 col-span-8"
21+
getPostParams={{
22+
authorId: params?.authorId,
23+
...searchParams,
24+
}}
25+
/>
2326
</div>
2427
)
2528
}

packages/database/prisma/schema.prisma

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,9 @@ model User {
181181
comment Comment[]
182182
commentOnUser CommentOnUser[]
183183
images Image[]
184+
185+
createdAt DateTime @default(now())
186+
updatedAt DateTime @updatedAt
184187
}
185188

186189
model Follower {

packages/database/src/index.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import {
2222
} from "./shared/type"
2323
import { createTag, getTag, getTags, getTopTags } from "./tags/queries"
2424
import type { TTagItem, TTagListItem } from "./tags/selects"
25+
import { getUser } from "./users/queries"
26+
import { TUserDetail } from "./users/selects"
2527

2628
export * from "@prisma/client"
2729
export default prisma
@@ -43,6 +45,9 @@ export {
4345
FilterValues,
4446
PeriodValues,
4547

48+
// Users
49+
getUser,
50+
4651
// Images
4752
getImages,
4853
createImage,
@@ -69,6 +74,9 @@ export type {
6974
ImageOrderBys,
7075
TImage,
7176

77+
// Users
78+
TUserDetail,
79+
7280
// Shared
7381
IActionReturn,
7482
IGetListResponse,

packages/database/src/tags/queries.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export const getTags = async ({
4040
}
4141

4242
try {
43-
const [data, total]: [Tags[], number] = await Promise.all([
43+
const [data, total] = await Promise.all([
4444
prisma.tags.findMany(tagQuery),
4545
prisma.tags.count({
4646
where: tagQuery.where,
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import prisma from "../prisma"
2+
import { IActionReturn } from "../shared/type"
3+
import { TUserDetail, userDetailSelect } from "./selects"
4+
import { TGetUserProps } from "./type"
5+
6+
export const getUser = async ({ userId }: TGetUserProps): Promise<IActionReturn<TUserDetail>> => {
7+
try {
8+
const data = await prisma.user.findFirst({
9+
where: {
10+
id: userId,
11+
},
12+
select: userDetailSelect,
13+
})
14+
15+
if (!data) {
16+
throw {
17+
error: "NOT_FOUND",
18+
}
19+
}
20+
21+
return {
22+
data,
23+
}
24+
} catch (error) {
25+
throw {
26+
error,
27+
data: null,
28+
}
29+
}
30+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { Prisma } from "@prisma/client"
2+
3+
export const userDetailSelect = {
4+
id: true,
5+
name: true,
6+
email: true,
7+
image: true,
8+
bio: true,
9+
address: true,
10+
website: true,
11+
totalFollower: true,
12+
totalFollowing: true,
13+
totalPost: true,
14+
totalView: true,
15+
} satisfies Prisma.UserSelect
16+
17+
export type TUserDetail = Prisma.UserGetPayload<{
18+
select: typeof userDetailSelect
19+
}>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export type TGetUserProps = {
2+
userId: string
3+
}

0 commit comments

Comments
 (0)