Skip to content

Commit 10e7893

Browse files
committed
feat: refactor tag page
1 parent a0996bb commit 10e7893

File tree

13 files changed

+145
-147
lines changed

13 files changed

+145
-147
lines changed

apps/web/@/messages/en.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"category": "Category",
88
"posts": "Posts",
99
"total_post_plural": "Total {total} {total, plural, =0 {post} =1 {post} other {posts}}",
10+
"post_plural": "{total} {total, plural, =0 {post} =1 {post} other {posts}}",
1011
"post": "{total, plural, =0 {post} =1 {post} other {posts}}",
1112
"pages": "Pages",
1213
"page": "Page",

apps/web/@/molecules/posts/post-detail/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { TPostItem } from "database"
44
import { Typography } from "ui"
55

66
import APP_ROUTES from "@/constants/routes"
7-
import TagList from "@/molecules/tag/tag-list"
7+
import TagListMeta from "@/molecules/tag/tag-list-meta"
88
import PostMeta from "@/molecules/user/posts/post-meta"
99
import { generatePath } from "@/utils/generatePath"
1010

@@ -37,7 +37,7 @@ export default function PostDetail({ post }: PostDetailProps) {
3737

3838
<PostMeta post={post} />
3939

40-
<TagList
40+
<TagListMeta
4141
tags={post?.tagOnPost}
4242
classes={{
4343
container: "mt-4",

apps/web/@/molecules/posts/post-item/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { useTranslations } from "next-intl"
66
import { Typography } from "ui"
77

88
import APP_ROUTES from "@/constants/routes"
9-
import TagList from "@/molecules/tag/tag-list"
9+
import TagListMeta from "@/molecules/tag/tag-list-meta"
1010
import { generatePath } from "@/utils/generatePath"
1111

1212
import CommentButton from "./comment-button"
@@ -34,7 +34,7 @@ export default function PostItem({ post }: { post: TPostItem }) {
3434

3535
<PostMeta post={post} />
3636

37-
<TagList
37+
<TagListMeta
3838
tags={post?.tagOnPost}
3939
classes={{
4040
container: "mt-2",

apps/web/@/molecules/posts/post-list/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export default function PostList({ getPostParams = {} }: TPostListProps) {
4242
loading={isLoading}
4343
nextPage={loadPosts}
4444
>
45-
{posts?.map((post) => (
45+
{posts.map((post) => (
4646
<PostItem
4747
key={post.id}
4848
post={post}

apps/web/@/molecules/tag/tag-badge/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import React from "react"
22
import Link from "next/link"
33

4+
import { TTagItem } from "database"
45
import { Badge } from "ui"
56

67
import APP_ROUTES from "@/constants/routes"
7-
import { TTagItem } from "@/types/tags"
88
import { generatePath } from "@/utils/generatePath"
99

1010
interface TagBadgeProps {

apps/web/@/molecules/tag/tag-detail/index.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import Link from "next/link"
22

3-
import { Tags } from "database"
3+
import { TTagItem } from "database"
44
import { Tag } from "lucide-react"
55
import { Button, Card, CardContent, CardHeader } from "ui"
66

77
export type TagDetailProp = {
8-
tag: Tags
8+
tag: TTagItem
99
}
1010

1111
const TagDetail = ({ tag }: TagDetailProp) => {
@@ -24,7 +24,7 @@ const TagDetail = ({ tag }: TagDetailProp) => {
2424
</h1>
2525
<div className="mt-4 flex w-full flex-1 divide-x">
2626
<div className="flex flex-1 flex-col items-center justify-center">
27-
<div className="font-bold text-slate-800">{tag?.totalPost || 0}</div>
27+
<div className="font-bold text-slate-800">{tag?._count?.tagOnPost || 0}</div>
2828
<div className="text-gray-400 hover:underline">
2929
<Link href={`/tags/${tag.id}`}>posts</Link>
3030
</div>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import Link from "next/link"
2+
3+
import { TTagItem } from "database"
4+
import { useTranslations } from "next-intl"
5+
import { Card, CardContent, CardHeader, Typography } from "ui"
6+
7+
export default function TagItem({ tag }: { tag: TTagItem }) {
8+
const t = useTranslations("common")
9+
10+
return (
11+
<Link
12+
href={`/tags/${tag?.id}`}
13+
key={tag?.id}
14+
>
15+
<Card className="px-6 py-4 sm:col-span-1">
16+
<CardHeader className="flex-row justify-between overflow-hidden p-0">
17+
<Typography
18+
variant="h3"
19+
className="mb-1 break-words text-lg hover:underline"
20+
>
21+
#{tag?.name}
22+
</Typography>
23+
</CardHeader>
24+
<CardContent className="p-0">
25+
{tag?.description && <Typography variant="span">{tag?.description}</Typography>}
26+
<div>
27+
<Typography
28+
variant="span"
29+
className="font-semibold"
30+
>
31+
{t("post_plural", { total: tag?._count?.tagOnPost || 0 })}
32+
</Typography>
33+
</div>
34+
</CardContent>
35+
</Card>
36+
</Link>
37+
)
38+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import React from "react"
2+
3+
import { TTagItem } from "database"
4+
import { cn } from "ui"
5+
6+
import TagBadge from "../tag-badge"
7+
8+
export type TagListProps = {
9+
tags: TTagItem[]
10+
classes?: {
11+
container?: string
12+
}
13+
}
14+
15+
export default function TagListMeta({
16+
tags,
17+
classes = {
18+
container: "",
19+
},
20+
}: TagListProps) {
21+
return (
22+
<div className={cn(classes?.container)}>
23+
{tags?.length > 0 &&
24+
tags?.map((tag) => (
25+
<TagBadge
26+
key={tag?.id}
27+
tag={tag}
28+
/>
29+
))}
30+
</div>
31+
)
32+
}

apps/web/@/molecules/tag/tag-list/index.tsx

Lines changed: 49 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,54 @@
1-
import React from "react"
2-
3-
import { cn } from "ui"
4-
5-
import TagBadge from "../tag-badge"
6-
7-
export type TagListProps = {
8-
tags: {
9-
tag: {
10-
id: string
11-
name: string
12-
slug: string
13-
}
14-
}[]
15-
classes?: {
16-
container?: string
17-
}
18-
}
1+
"use client"
2+
3+
import React, { useCallback, useState } from "react"
4+
import { useParams } from "next/navigation"
5+
6+
import { getTags, TTagItem } from "database"
7+
8+
import InfiniteScroll from "@/molecules/infinite-scroll"
9+
10+
import TagItem from "../tag-item"
11+
12+
const TagList: React.FC = () => {
13+
const searchParams = useParams()
14+
const [isLoading, setIsLoading] = useState(false)
15+
const [tags, setTags] = useState<TTagItem[]>([])
16+
const [page, setPage] = useState(1)
17+
const [hasNextPage, setHasNextPage] = useState(true)
18+
19+
const loadTags = useCallback(async () => {
20+
if (!hasNextPage) return
21+
22+
setIsLoading(true)
23+
const { data } = await getTags({
24+
...searchParams,
25+
page,
26+
})
27+
28+
setTags((prev) => [...prev, ...data?.data])
29+
setHasNextPage(data?.totalPages > page)
30+
setIsLoading(false)
31+
setPage((prev) => prev + 1)
32+
}, [searchParams, page])
1933

20-
const TagList: React.FC<TagListProps> = ({
21-
tags,
22-
classes = {
23-
container: "",
24-
},
25-
}) => {
2634
return (
27-
<div className={cn(classes?.container)}>
28-
{tags?.length > 0 &&
29-
tags?.map(({ tag }) => (
30-
<TagBadge
31-
key={tag?.id}
32-
tag={tag}
33-
/>
34-
))}
35-
</div>
35+
<InfiniteScroll
36+
loading={isLoading}
37+
nextPage={loadTags}
38+
>
39+
<div className="mt-4">
40+
{tags?.length > 0 ? (
41+
<div className="grid grid-cols-2 gap-4 md:grid-cols-3 lg:grid-cols-4">
42+
{tags.map((tag) => (
43+
<TagItem
44+
key={tag.id}
45+
tag={tag}
46+
/>
47+
))}
48+
</div>
49+
) : null}
50+
</div>
51+
</InfiniteScroll>
3652
)
3753
}
3854

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default function Loading() {
2+
return <div>Loading...</div>
3+
}

0 commit comments

Comments
 (0)