visuddhinanda 3 lat temu
rodzic
commit
bd6ae24a4f

+ 91 - 0
dashboard/src/components/comment/CommentTopicChildren.tsx

@@ -0,0 +1,91 @@
+import { List, message } from "antd";
+import { useEffect, useState } from "react";
+import { useIntl } from "react-intl";
+import { get } from "../../request";
+import { ICommentListResponse } from "../api/Comment";
+import CommentCreate from "./CommentCreate";
+
+import CommentItem, { IComment } from "./CommentItem";
+
+interface IWidget {
+  topicId?: string;
+  onItemCountChange?: Function;
+}
+const Widget = ({ topicId, onItemCountChange }: IWidget) => {
+  const intl = useIntl();
+  const [data, setData] = useState<IComment[]>();
+  useEffect(() => {
+    if (typeof topicId === "undefined") {
+      return;
+    }
+    get<ICommentListResponse>(`/v2/discussion?view=answer&id=${topicId}`)
+      .then((json) => {
+        console.log(json);
+        if (json.ok) {
+          console.log(intl.formatMessage({ id: "flashes.success" }));
+          const discussions: IComment[] = json.data.rows.map((item) => {
+            return {
+              id: item.id,
+              resId: item.res_id,
+              resType: item.res_type,
+              user: {
+                id: item.editor?.id ? item.editor.id : "null",
+                nickName: item.editor?.nickName ? item.editor.nickName : "null",
+                realName: item.editor?.userName ? item.editor.userName : "null",
+                avatar: item.editor?.avatar ? item.editor.avatar : "null",
+              },
+              title: item.title,
+              content: item.content,
+              createdAt: item.created_at,
+              updatedAt: item.updated_at,
+            };
+          });
+          setData(discussions);
+        } else {
+          message.error(json.message);
+        }
+      })
+      .catch((e) => {
+        message.error(e.message);
+      });
+  }, [topicId]);
+  return (
+    <div>
+      <List
+        pagination={{
+          onChange: (page) => {
+            console.log(page);
+          },
+          pageSize: 10,
+        }}
+        itemLayout="horizontal"
+        dataSource={data}
+        renderItem={(item) => (
+          <List.Item>
+            <CommentItem data={item} />
+          </List.Item>
+        )}
+      />
+      <CommentCreate
+        parent={topicId}
+        onCreated={(e: IComment) => {
+          console.log("create", e);
+          const newData = JSON.parse(JSON.stringify(e));
+          let count = 0;
+          if (typeof data === "undefined") {
+            count = 1;
+            setData([newData]);
+          } else {
+            count = data.length + 1;
+            setData([...data, newData]);
+          }
+          if (typeof onItemCountChange !== "undefined") {
+            onItemCountChange(count, e.parent);
+          }
+        }}
+      />
+    </div>
+  );
+};
+
+export default Widget;

+ 0 - 30
dashboard/src/components/comment/CommentTopicHead.tsx

@@ -1,30 +0,0 @@
-import { Typography, Space } from "antd";
-import TimeShow from "../general/TimeShow";
-
-import { IComment } from "./CommentItem";
-
-const { Title, Text } = Typography;
-
-interface IWidget {
-  data?: IComment;
-}
-const Widget = ({ data }: IWidget) => {
-  return (
-    <div>
-      <Title editable level={1} style={{ margin: 0 }}>
-        {data?.title}
-      </Title>
-      <div>
-        <Text type="secondary">
-          <Space>
-            {" "}
-            {data?.user.nickName}{" "}
-            <TimeShow time={data?.createdAt} title="创建" />
-          </Space>
-        </Text>
-      </div>
-    </div>
-  );
-};
-
-export default Widget;

+ 68 - 0
dashboard/src/components/comment/CommentTopicInfo.tsx

@@ -0,0 +1,68 @@
+import { Typography, Space, message } from "antd";
+import { useEffect, useState } from "react";
+import { get } from "../../request";
+import { ICommentResponse } from "../api/Comment";
+import TimeShow from "../general/TimeShow";
+
+import { IComment } from "./CommentItem";
+
+const { Title, Text } = Typography;
+
+interface IWidget {
+  topicId?: string;
+}
+const Widget = ({ topicId }: IWidget) => {
+  const [data, setData] = useState<IComment>();
+  useEffect(() => {
+    if (typeof topicId === "undefined") {
+      return;
+    }
+    get<ICommentResponse>(`/v2/discussion/${topicId}`)
+      .then((json) => {
+        console.log(json);
+        if (json.ok) {
+          console.log("flashes.success");
+          const item = json.data;
+          const discussion: IComment = {
+            id: item.id,
+            resId: item.res_id,
+            resType: item.res_type,
+            user: {
+              id: item.editor?.id ? item.editor.id : "null",
+              nickName: item.editor?.nickName ? item.editor.nickName : "null",
+              realName: item.editor?.userName ? item.editor.userName : "null",
+              avatar: item.editor?.avatar ? item.editor.avatar : "null",
+            },
+            title: item.title,
+            content: item.content,
+            createdAt: item.created_at,
+            updatedAt: item.updated_at,
+          };
+          setData(discussion);
+        } else {
+          message.error(json.message);
+        }
+      })
+      .catch((e) => {
+        message.error(e.message);
+      });
+  }, [topicId]);
+  return (
+    <div>
+      <Title editable level={1} style={{ margin: 0 }}>
+        {data?.title}
+      </Title>
+      <div>
+        <Text type="secondary">
+          <Space>
+            {" "}
+            {data?.user.nickName}{" "}
+            <TimeShow time={data?.createdAt} title="创建" />
+          </Space>
+        </Text>
+      </div>
+    </div>
+  );
+};
+
+export default Widget;

+ 0 - 30
dashboard/src/components/comment/CommentTopicList.tsx

@@ -1,30 +0,0 @@
-import { List } from "antd";
-
-import CommentItem, { IComment } from "./CommentItem";
-
-interface IWidget {
-  data: IComment[];
-}
-const Widget = ({ data }: IWidget) => {
-  return (
-    <div>
-      <List
-        pagination={{
-          onChange: (page) => {
-            console.log(page);
-          },
-          pageSize: 10,
-        }}
-        itemLayout="horizontal"
-        dataSource={data}
-        renderItem={(item) => (
-          <List.Item>
-            <CommentItem data={item} />
-          </List.Item>
-        )}
-      />
-    </div>
-  );
-};
-
-export default Widget;