Discussion.tsx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import { useEffect, useState } from "react";
  2. import { ArrowLeftOutlined } from "@ant-design/icons";
  3. import DiscussionTopic from "./DiscussionTopic";
  4. import DiscussionListCard, { type TResType } from "./DiscussionListCard";
  5. import type { IComment } from "./DiscussionItem";
  6. import { countChange } from "../../reducers/discussion";
  7. import { Button, Space, Typography } from "antd";
  8. import store from "../../store";
  9. const { Text } = Typography;
  10. export interface IAnswerCount {
  11. id: string;
  12. count: number;
  13. }
  14. interface IWidget {
  15. resId?: string;
  16. resType?: TResType;
  17. showTopicId?: string;
  18. focus?: string;
  19. type?: TDiscussionType;
  20. showStudent?: boolean;
  21. onTopicReady?: Function;
  22. }
  23. const DiscussionWidget = ({
  24. resId,
  25. resType,
  26. showTopicId,
  27. showStudent = false,
  28. focus,
  29. type = "discussion",
  30. onTopicReady,
  31. }: IWidget) => {
  32. const [childrenDrawer, setChildrenDrawer] = useState(false);
  33. const [topicId, setTopicId] = useState<string>();
  34. const [topic, setTopic] = useState<IComment>();
  35. const [answerCount, setAnswerCount] = useState<IAnswerCount>();
  36. const [topicTitle, setTopicTitle] = useState<string>();
  37. useEffect(() => {
  38. if (showTopicId) {
  39. setChildrenDrawer(true);
  40. setTopicId(showTopicId);
  41. } else {
  42. setChildrenDrawer(false);
  43. }
  44. }, [showTopicId]);
  45. useEffect(() => {
  46. setChildrenDrawer(false);
  47. }, [resId]);
  48. const showChildrenDrawer = (comment: IComment) => {
  49. console.debug("discussion comment", comment);
  50. setChildrenDrawer(true);
  51. if (comment.id) {
  52. setTopicId(comment.id);
  53. setTopic(undefined);
  54. } else {
  55. setTopicId(undefined);
  56. setTopic(comment);
  57. }
  58. };
  59. return (
  60. <>
  61. {childrenDrawer ? (
  62. <div>
  63. <Space>
  64. <Button
  65. shape="circle"
  66. icon={<ArrowLeftOutlined />}
  67. onClick={() => setChildrenDrawer(false)}
  68. />
  69. <Text strong style={{ fontSize: 16 }}>
  70. {topic ? topic.title : topicTitle}
  71. </Text>
  72. </Space>
  73. <DiscussionTopic
  74. resType={resType}
  75. topicId={topicId}
  76. topic={topic}
  77. focus={focus}
  78. hideTitle
  79. onItemCountChange={(count: number, parent: string) => {
  80. setAnswerCount({ id: parent, count: count });
  81. }}
  82. onTopicReady={(value: IComment) => {
  83. setTopicTitle(value.title);
  84. if (typeof onTopicReady !== "undefined") {
  85. onTopicReady(value);
  86. }
  87. }}
  88. onTopicDelete={() => {
  89. setChildrenDrawer(false);
  90. }}
  91. onConvert={(_value: TDiscussionType) => {
  92. setChildrenDrawer(false);
  93. }}
  94. />
  95. </div>
  96. ) : (
  97. <DiscussionListCard
  98. resId={resId}
  99. resType={resType}
  100. type={type}
  101. showStudent={showStudent}
  102. onSelect={(
  103. _e: React.MouseEvent<HTMLSpanElement, MouseEvent>,
  104. comment: IComment
  105. ) => showChildrenDrawer(comment)}
  106. onReply={(comment: IComment) => showChildrenDrawer(comment)}
  107. onReady={() => {}}
  108. changedAnswerCount={answerCount}
  109. onItemCountChange={(count: number) => {
  110. store.dispatch(
  111. countChange({
  112. count: count,
  113. resId: resId,
  114. resType: resType,
  115. })
  116. );
  117. }}
  118. />
  119. )}
  120. </>
  121. );
  122. };
  123. export default DiscussionWidget;