CommentListCard.tsx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import { useState, useEffect } from "react";
  2. import { useIntl } from "react-intl";
  3. import { Card, message, Typography } from "antd";
  4. import { get } from "../../request";
  5. import { ICommentListResponse } from "../api/Comment";
  6. import CommentCreate from "./CommentCreate";
  7. import { IComment } from "./CommentItem";
  8. import CommentList from "./CommentList";
  9. import { IAnswerCount } from "./CommentBox";
  10. export type TResType = "article" | "channel" | "chapter" | "sentence" | "wbw";
  11. interface IWidget {
  12. resId?: string;
  13. resType?: TResType;
  14. topicId?: string;
  15. changedAnswerCount?: IAnswerCount;
  16. onSelect?: Function;
  17. onItemCountChange?: Function;
  18. }
  19. const CommentListCardWidget = ({
  20. resId,
  21. resType,
  22. topicId,
  23. onSelect,
  24. changedAnswerCount,
  25. onItemCountChange,
  26. }: IWidget) => {
  27. const intl = useIntl();
  28. const [data, setData] = useState<IComment[]>([]);
  29. useEffect(() => {
  30. console.log("changedAnswerCount", changedAnswerCount);
  31. const newData = [...data].map((item) => {
  32. const newItem = item;
  33. if (newItem.id && changedAnswerCount?.id === newItem.id) {
  34. newItem.childrenCount = changedAnswerCount.count;
  35. }
  36. return newItem;
  37. });
  38. setData(newData);
  39. }, [changedAnswerCount]);
  40. useEffect(() => {
  41. let url: string = "";
  42. if (typeof topicId !== "undefined") {
  43. url = `/v2/discussion?view=question-by-topic&id=${topicId}`;
  44. }
  45. if (typeof resId !== "undefined") {
  46. url = `/v2/discussion?view=question&id=${resId}`;
  47. }
  48. if (url === "") {
  49. return;
  50. }
  51. get<ICommentListResponse>(url)
  52. .then((json) => {
  53. console.log(json);
  54. if (json.ok) {
  55. console.log(intl.formatMessage({ id: "flashes.success" }));
  56. const discussions: IComment[] = json.data.rows.map((item) => {
  57. return {
  58. id: item.id,
  59. resId: item.res_id,
  60. resType: item.res_type,
  61. user: item.editor,
  62. title: item.title,
  63. content: item.content,
  64. childrenCount: item.children_count,
  65. createdAt: item.created_at,
  66. updatedAt: item.updated_at,
  67. };
  68. });
  69. setData(discussions);
  70. } else {
  71. message.error(json.message);
  72. }
  73. })
  74. .catch((e) => {
  75. message.error(e.message);
  76. });
  77. }, [intl, resId, topicId]);
  78. if (typeof resId === "undefined" && typeof topicId === "undefined") {
  79. return (
  80. <Typography.Paragraph>
  81. 该资源尚未创建,不能发表讨论。
  82. </Typography.Paragraph>
  83. );
  84. }
  85. return (
  86. <div>
  87. <Card title="讨论" extra={"More"}>
  88. {data.length > 0 ? (
  89. <CommentList
  90. onSelect={(
  91. e: React.MouseEvent<HTMLSpanElement, MouseEvent>,
  92. comment: IComment
  93. ) => {
  94. if (typeof onSelect !== "undefined") {
  95. onSelect(e, comment);
  96. }
  97. }}
  98. data={data}
  99. />
  100. ) : undefined}
  101. {resId && resType ? (
  102. <CommentCreate
  103. contentType="markdown"
  104. resId={resId}
  105. resType={resType}
  106. onCreated={(e: IComment) => {
  107. const newData = JSON.parse(JSON.stringify(e));
  108. console.log("create", e);
  109. if (typeof onItemCountChange !== "undefined") {
  110. onItemCountChange(data.length + 1);
  111. }
  112. setData([...data, newData]);
  113. }}
  114. />
  115. ) : undefined}
  116. </Card>
  117. </div>
  118. );
  119. };
  120. export default CommentListCardWidget;