diff --git a/tone-page-web/app/(with-header-footer)/blog/[id]/components/BlogCommentTool.tsx b/tone-page-web/app/(with-header-footer)/blog/[id]/components/BlogCommentTool.tsx
index e73cd04..46f01e3 100644
--- a/tone-page-web/app/(with-header-footer)/blog/[id]/components/BlogCommentTool.tsx
+++ b/tone-page-web/app/(with-header-footer)/blog/[id]/components/BlogCommentTool.tsx
@@ -3,26 +3,28 @@
import { Button } from "@/components/ui/button";
import { Textarea } from "@/components/ui/textarea";
import { BlogApi } from "@/lib/api";
+import { BlogComment } from "@/lib/types/blogComment";
import { Send } from "lucide-react";
import { useState } from "react";
import { toast } from "sonner";
-export function BlogCommentTool({ blogId }: { blogId: string }) {
+export function BlogCommentTool({ blogId, onInsertComment }: { blogId: string, onInsertComment: (b: BlogComment) => void }) {
const [comment, setComment] = useState('');
const submit = async () => {
+ if (comment.trim().length === 0) return;
const res = await BlogApi.createComment(blogId, comment);
if (res) {
toast.success('发布成功');
setComment('');
- // 提交界面刷新
+ onInsertComment(res);
}
}
return (
diff --git a/tone-page-web/app/(with-header-footer)/blog/[id]/components/BlogComments.tsx b/tone-page-web/app/(with-header-footer)/blog/[id]/components/BlogComments.tsx
index cd28b08..ef764ee 100644
--- a/tone-page-web/app/(with-header-footer)/blog/[id]/components/BlogComments.tsx
+++ b/tone-page-web/app/(with-header-footer)/blog/[id]/components/BlogComments.tsx
@@ -1,23 +1,34 @@
import useSWR from "swr";
import { BlogCommentTool } from "./BlogCommentTool";
import { BlogApi } from "@/lib/api";
+import { BlogComment } from "@/lib/types/blogComment";
export function BlogComments({ blogId }: { blogId: string }) {
- const { data, isLoading, error } = useSWR(
+ const { data, isLoading, error, mutate } = useSWR(
`/api/blog/${blogId}/comments`,
() => BlogApi.getComments(blogId),
)
+ const insertComment = async (newOne: BlogComment) => {
+ await mutate(
+ (comments) => {
+ if (!comments) return [newOne];
+ return [newOne, ...comments]
+ },
+ { revalidate: false }
+ )
+ }
+
return (
data &&
评论 {data.length}
-
+
{
data.filter(d => !d.parentId)
.map(d => (
-
{d.user ? d.user : '匿名'}
+
{d.user ? d.user.nickname : '匿名'}
{d.content}
{new Date(d.createdAt).toLocaleString()}
@@ -30,7 +41,7 @@ export function BlogComments({ blogId }: { blogId: string }) {
{
data.filter(c => c.parentId === d.id).map(c => (
-
{c.user ? c.user : '匿名'}
+
{c.user ? c.user.nickname : '匿名'}
{c.content}
{new Date().toLocaleString()}
diff --git a/tone-page-web/lib/api/blog/createComment.ts b/tone-page-web/lib/api/blog/createComment.ts
index e4655b8..f2f9e5b 100644
--- a/tone-page-web/lib/api/blog/createComment.ts
+++ b/tone-page-web/lib/api/blog/createComment.ts
@@ -1,14 +1,8 @@
+import { BlogComment } from "@/lib/types/blogComment";
import fetcher from "../fetcher";
export async function createComment(blogId: string, content: string) {
- return fetcher<{
- blogId: string;
- content: string;
- createdAt: string
- deletedAt: null; // 原则上能看到就是null
- id: string;
- parentId: string | null;
- }>(`/api/blog/${blogId}/comment`, {
+ return fetcher
(`/api/blog/${blogId}/comment`, {
method: 'POST',
body: JSON.stringify({ content }),
});
diff --git a/tone-page-web/lib/api/blog/getComments.ts b/tone-page-web/lib/api/blog/getComments.ts
index ea2f72a..d76d885 100644
--- a/tone-page-web/lib/api/blog/getComments.ts
+++ b/tone-page-web/lib/api/blog/getComments.ts
@@ -1,13 +1,6 @@
+import { BlogComment } from "@/lib/types/blogComment";
import fetcher from "../fetcher";
export async function getComments(blogId: string) {
- return fetcher<{
- blogId: string;
- content: string;
- createdAt: string;
- deletedAt: string | null;// 原则上能看到就是null,
- id: string;
- parentId: string | null; // 如果是回复,则有parentId
- user: null;// TODO需要完善
- }[]>(`/api/blog/${blogId}/comments`, { method: 'GET' });
+ return fetcher(`/api/blog/${blogId}/comments`, { method: 'GET' });
}
\ No newline at end of file
diff --git a/tone-page-web/lib/types/blogComment.ts b/tone-page-web/lib/types/blogComment.ts
new file mode 100644
index 0000000..2168761
--- /dev/null
+++ b/tone-page-web/lib/types/blogComment.ts
@@ -0,0 +1,11 @@
+import { User } from "./user";
+
+export interface BlogComment {
+ id: string;
+ blogId: string;
+ content: string;
+ createdAt: string;
+ deletedAt: string | null;
+ parentId: string | null;
+ user: User | null;
+}
\ No newline at end of file