SentWbw.tsx 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. import { Button, List, Space, message } from "antd";
  2. import { useEffect, useState } from "react";
  3. import { ReloadOutlined } from "@ant-design/icons";
  4. import { get } from "../../../request";
  5. import { ISentenceWbwListResponse } from "../../api/Corpus";
  6. import { ISentence, IWidgetSentEditInner, SentEditInner } from "../SentEdit";
  7. import { useAppSelector } from "../../../hooks";
  8. import { courseInfo, memberInfo } from "../../../reducers/current-course";
  9. import { courseUser } from "../../../reducers/course-user";
  10. import User, { IUser } from "../../auth/User";
  11. interface IWidget {
  12. book: number;
  13. para: number;
  14. wordStart: number;
  15. wordEnd: number;
  16. channelsId?: string[];
  17. reload?: boolean;
  18. wbwProgress?: boolean;
  19. onReload?: Function;
  20. }
  21. const SentWbwWidget = ({
  22. book,
  23. para,
  24. wordStart,
  25. wordEnd,
  26. channelsId,
  27. reload = false,
  28. wbwProgress = false,
  29. onReload,
  30. }: IWidget) => {
  31. const [sentData, setSentData] = useState<IWidgetSentEditInner[]>([]);
  32. const [answer, setAnswer] = useState<ISentence>();
  33. const [loading, setLoading] = useState<boolean>(false);
  34. const course = useAppSelector(courseInfo);
  35. const courseMember = useAppSelector(memberInfo);
  36. const myCourse = useAppSelector(courseUser);
  37. let isCourse: boolean = false;
  38. if (myCourse && course) {
  39. isCourse = true;
  40. }
  41. const load = () => {
  42. let url = `/v2/wbw-sentence?view=sent-can-read`;
  43. url += `&book=${book}&para=${para}&wordStart=${wordStart}&wordEnd=${wordEnd}`;
  44. console.debug("wbw sentence load", myCourse, course);
  45. if (myCourse && course) {
  46. url += `&course=${course.courseId}`;
  47. if (myCourse.role === "student") {
  48. //学生,仅列出答案channel
  49. url += `&channels=${course.channelId}`;
  50. } else if (courseMember) {
  51. //管理者,助教,列出学生作业
  52. console.debug("course member", courseMember);
  53. /*
  54. const channels = courseMember
  55. .filter((value) => typeof value.channel_id === "string")
  56. .map((item) => item.channel_id);
  57. url += `&channels=${channels.join(",")}`;
  58. */
  59. }
  60. } else {
  61. if (channelsId && channelsId.length > 0) {
  62. url += `&exclude=${channelsId[0]}`;
  63. }
  64. }
  65. setLoading(true);
  66. console.info("wbw sentence api request", url);
  67. get<ISentenceWbwListResponse>(url)
  68. .then((json) => {
  69. console.info("wbw sentence api response", json);
  70. if (json.ok) {
  71. console.debug("wbw sentence course", course);
  72. if (course && myCourse && myCourse.role !== "student") {
  73. setSentData(
  74. json.data.rows.filter((value) =>
  75. value.translation
  76. ? value.translation[0].channel.id !== course.channelId
  77. : true
  78. )
  79. );
  80. } else {
  81. setSentData(json.data.rows);
  82. }
  83. if (myCourse && course) {
  84. const answerData = json.data.rows.find((value) =>
  85. value.origin
  86. ? value.origin[0].channel.id === course?.channelId
  87. : false
  88. );
  89. if (answerData?.origin) {
  90. setAnswer(answerData.origin[0]);
  91. console.debug("answer", answerData.origin[0]);
  92. }
  93. }
  94. } else {
  95. message.error(json.message);
  96. }
  97. })
  98. .finally(() => {
  99. setLoading(false);
  100. if (reload && typeof onReload !== "undefined") {
  101. onReload();
  102. }
  103. });
  104. };
  105. useEffect(() => {
  106. load();
  107. }, []);
  108. //没交作业的人
  109. let nonWbwUser: IUser[] = [];
  110. const isCourseAnswer = myCourse && course && myCourse.role !== "student";
  111. if (isCourseAnswer && courseMember) {
  112. const hasWbwUsers = sentData.map((item) =>
  113. item.translation ? item.translation[0].studio : undefined
  114. );
  115. courseMember
  116. .filter((value) => value.role === "student")
  117. .forEach((value) => {
  118. const curr = hasWbwUsers.find((value1) => value1?.id === value.user_id);
  119. if (!curr && value.user) {
  120. nonWbwUser.push(value.user);
  121. }
  122. });
  123. }
  124. console.debug("没交作业", courseMember, sentData, nonWbwUser);
  125. return (
  126. <>
  127. <List
  128. loading={loading}
  129. header={
  130. <div style={{ display: "flex", justifyContent: "space-between" }}>
  131. <span></span>
  132. <Space>
  133. <Button
  134. type="link"
  135. shape="round"
  136. icon={<ReloadOutlined />}
  137. onClick={() => load()}
  138. />
  139. </Space>
  140. </div>
  141. }
  142. itemLayout="horizontal"
  143. split={false}
  144. dataSource={sentData}
  145. renderItem={(item, index) => (
  146. <List.Item key={index}>
  147. <SentEditInner
  148. {...item}
  149. readonly={isCourse}
  150. answer={answer}
  151. wbwProgress={isCourse ?? wbwProgress}
  152. />
  153. </List.Item>
  154. )}
  155. />
  156. <div>
  157. {isCourseAnswer ? (
  158. <Space style={{ flexWrap: "wrap" }}>
  159. {"无作业:"}
  160. {nonWbwUser.length > 0
  161. ? nonWbwUser.map((item, id) => {
  162. return <User {...item} />;
  163. })
  164. : "无"}
  165. </Space>
  166. ) : undefined}
  167. </div>
  168. </>
  169. );
  170. };
  171. export default SentWbwWidget;