CommentTopicChildren.tsx 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import { List, message } from "antd";
  2. import { useEffect, useState } from "react";
  3. import { useIntl } from "react-intl";
  4. import { get } from "../../request";
  5. import { ICommentListResponse } from "../api/Comment";
  6. import CommentCreate from "./CommentCreate";
  7. import CommentItem, { IComment } from "./CommentItem";
  8. interface IWidget {
  9. topicId?: string;
  10. onItemCountChange?: Function;
  11. }
  12. const CommentTopicChildrenWidget = ({
  13. topicId,
  14. onItemCountChange,
  15. }: IWidget) => {
  16. const intl = useIntl();
  17. const [data, setData] = useState<IComment[]>();
  18. useEffect(() => {
  19. if (typeof topicId === "undefined") {
  20. return;
  21. }
  22. get<ICommentListResponse>(`/v2/discussion?view=answer&id=${topicId}`)
  23. .then((json) => {
  24. console.log(json);
  25. if (json.ok) {
  26. console.log(intl.formatMessage({ id: "flashes.success" }));
  27. const discussions: IComment[] = json.data.rows.map((item) => {
  28. return {
  29. id: item.id,
  30. resId: item.res_id,
  31. resType: item.res_type,
  32. user: item.editor,
  33. title: item.title,
  34. content: item.content,
  35. createdAt: item.created_at,
  36. updatedAt: item.updated_at,
  37. };
  38. });
  39. setData(discussions);
  40. } else {
  41. message.error(json.message);
  42. }
  43. })
  44. .catch((e) => {
  45. message.error(e.message);
  46. });
  47. }, [intl, topicId]);
  48. return (
  49. <div>
  50. <List
  51. pagination={{
  52. onChange: (page) => {
  53. console.log(page);
  54. },
  55. pageSize: 10,
  56. }}
  57. itemLayout="horizontal"
  58. dataSource={data}
  59. renderItem={(item) => (
  60. <List.Item>
  61. <CommentItem data={item} />
  62. </List.Item>
  63. )}
  64. />
  65. <CommentCreate
  66. contentType="markdown"
  67. parent={topicId}
  68. onCreated={(e: IComment) => {
  69. console.log("create", e);
  70. const newData = JSON.parse(JSON.stringify(e));
  71. let count = 0;
  72. if (typeof data === "undefined") {
  73. count = 1;
  74. setData([newData]);
  75. } else {
  76. count = data.length + 1;
  77. setData([...data, newData]);
  78. }
  79. if (typeof onItemCountChange !== "undefined") {
  80. onItemCountChange(count, e.parent);
  81. }
  82. }}
  83. />
  84. </div>
  85. );
  86. };
  87. export default CommentTopicChildrenWidget;