DiscussionTopicInfo.tsx 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import { Typography, Space, message } from "antd";
  2. import { useEffect, useState } from "react";
  3. import { get } from "../../request";
  4. import { ICommentResponse } from "../api/Comment";
  5. import TimeShow from "../general/TimeShow";
  6. import { IComment } from "./DiscussionItem";
  7. const { Title, Text } = Typography;
  8. interface IWidget {
  9. topicId?: string;
  10. onReady?: Function;
  11. }
  12. const DiscussionTopicInfoWidget = ({ topicId, onReady }: IWidget) => {
  13. const [data, setData] = useState<IComment>();
  14. useEffect(() => {
  15. if (typeof topicId === "undefined") {
  16. return;
  17. }
  18. get<ICommentResponse>(`/v2/discussion/${topicId}`)
  19. .then((json) => {
  20. console.log(json);
  21. if (json.ok) {
  22. console.log("flashes.success");
  23. const item = json.data;
  24. const discussion: IComment = {
  25. id: item.id,
  26. resId: item.res_id,
  27. resType: item.res_type,
  28. parent: item.parent,
  29. user: item.editor,
  30. title: item.title,
  31. content: item.content,
  32. createdAt: item.created_at,
  33. updatedAt: item.updated_at,
  34. };
  35. setData(discussion);
  36. if (typeof onReady !== "undefined") {
  37. console.log("on ready");
  38. onReady(discussion);
  39. }
  40. } else {
  41. message.error(json.message);
  42. }
  43. })
  44. .catch((e) => {
  45. message.error(e.message);
  46. });
  47. }, [topicId]);
  48. return (
  49. <div>
  50. <Title editable level={5} style={{ margin: 0 }}>
  51. {data?.title}
  52. </Title>
  53. <div>
  54. <Text type="secondary">
  55. <Space>
  56. {data?.user.nickName}
  57. <TimeShow time={data?.createdAt} title="创建" />
  58. </Space>
  59. </Text>
  60. </div>
  61. <div
  62. style={{ maxWidth: 800, overflow: "auto" }}
  63. dangerouslySetInnerHTML={{
  64. __html: data?.content ? data?.content : "",
  65. }}
  66. />
  67. </div>
  68. );
  69. };
  70. export default DiscussionTopicInfoWidget;