CommentTopicChildren.tsx 2.6 KB

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