Article.tsx 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. import { useEffect, useState } from "react";
  2. import { message } from "antd";
  3. import { modeChange } from "../../reducers/article-mode";
  4. import { get } from "../../request";
  5. import store from "../../store";
  6. import { IArticleDataResponse, IArticleResponse } from "../api/Article";
  7. import ArticleView from "./ArticleView";
  8. import { ICourseCurrUserResponse } from "../api/Course";
  9. import { ICourseUser, signIn } from "../../reducers/course-user";
  10. import { ITextbook, refresh } from "../../reducers/current-course";
  11. import ExerciseList from "./ExerciseList";
  12. import ExerciseAnswer from "../course/ExerciseAnswer";
  13. export type ArticleMode = "read" | "edit" | "wbw";
  14. export type ArticleType =
  15. | "article"
  16. | "chapter"
  17. | "paragraph"
  18. | "cs-para"
  19. | "sent"
  20. | "sim"
  21. | "page"
  22. | "textbook"
  23. | "exercise"
  24. | "exercise-list"
  25. | "corpus_sent/original"
  26. | "corpus_sent/commentary"
  27. | "corpus_sent/nissaya"
  28. | "corpus_sent/translation";
  29. interface IWidgetArticle {
  30. type?: ArticleType;
  31. articleId?: string;
  32. mode?: ArticleMode;
  33. active?: boolean;
  34. }
  35. const Widget = ({
  36. type,
  37. articleId,
  38. mode = "read",
  39. active = false,
  40. }: IWidgetArticle) => {
  41. const [articleData, setArticleData] = useState<IArticleDataResponse>();
  42. const [articleMode, setArticleMode] = useState<ArticleMode>(mode);
  43. const [extra, setExtra] = useState(<></>);
  44. let channels: string[] = [];
  45. if (typeof articleId !== "undefined") {
  46. const aId = articleId.split("_");
  47. if (aId.length > 1) {
  48. channels = aId.slice(1);
  49. }
  50. }
  51. useEffect(() => {
  52. /**
  53. * 由课本进入插叙当前用户的权限和channel
  54. */
  55. if (
  56. type === "textbook" ||
  57. type === "exercise" ||
  58. type === "exercise-list"
  59. ) {
  60. if (typeof articleId !== "undefined") {
  61. const id = articleId.split("_");
  62. get<ICourseCurrUserResponse>(`/v2/course-curr?course_id=${id[0]}`).then(
  63. (response) => {
  64. console.log("course user", response);
  65. if (response.ok) {
  66. const it: ICourseUser = {
  67. channelId: response.data.channel_id,
  68. role: response.data.role,
  69. };
  70. store.dispatch(signIn(it));
  71. /**
  72. * redux发布课程信息
  73. */
  74. const ic: ITextbook = {
  75. courseId: id[0],
  76. articleId: id[1],
  77. };
  78. store.dispatch(refresh(ic));
  79. }
  80. }
  81. );
  82. }
  83. }
  84. }, [articleId, type]);
  85. useEffect(() => {
  86. console.log("mode", mode, articleMode);
  87. if (!active) {
  88. return;
  89. }
  90. setArticleMode(mode);
  91. //发布mode变更
  92. store.dispatch(modeChange(mode));
  93. if (mode !== articleMode && mode !== "read" && articleMode !== "read") {
  94. console.log("set mode", mode, articleMode);
  95. return;
  96. }
  97. if (typeof type !== "undefined" && typeof articleId !== "undefined") {
  98. let url = "";
  99. switch (type) {
  100. case "article":
  101. const aIds = articleId.split("_");
  102. url = `/v2/article/${aIds[0]}?mode=${mode}`;
  103. if (aIds.length > 1) {
  104. const channels = aIds.slice(1);
  105. url += "&channel=" + channels.join();
  106. }
  107. break;
  108. case "textbook":
  109. /**
  110. * 从课本进入
  111. * id两部分组成
  112. * 课程id_文章id
  113. */
  114. const id = articleId.split("_");
  115. if (id.length < 2) {
  116. message.error("文章id期待2个,实际只给了一个");
  117. return;
  118. }
  119. url = `/v2/article/${id[1]}?mode=${mode}&view=textbook&course=${id[0]}`;
  120. break;
  121. case "exercise":
  122. /**
  123. * 从练习进入
  124. * id 由4部分组成
  125. * 课程id_文章id_练习id_username
  126. */
  127. const exerciseId = articleId.split("_");
  128. if (exerciseId.length < 3) {
  129. message.error("练习id期待3个");
  130. return;
  131. }
  132. console.log("exe", exerciseId);
  133. url = `/v2/article/${exerciseId[1]}?mode=${mode}&course=${exerciseId[0]}&exercise=${exerciseId[2]}&user=${exerciseId[3]}`;
  134. setExtra(
  135. <ExerciseAnswer
  136. courseId={exerciseId[0]}
  137. articleId={exerciseId[1]}
  138. exerciseId={exerciseId[2]}
  139. />
  140. );
  141. break;
  142. case "exercise-list":
  143. /**
  144. * 从练习进入
  145. * id 由3部分组成
  146. * 课程id_文章id_练习id
  147. */
  148. const exerciseListId = articleId.split("_");
  149. if (exerciseListId.length < 3) {
  150. message.error("练习id期待3个");
  151. return;
  152. }
  153. url = `/v2/article/${exerciseListId[1]}?mode=${mode}&course=${exerciseListId[0]}&exercise=${exerciseListId[2]}`;
  154. //url = `/v2/article/${exerciseListId[1]}?mode=${mode}&course=${exerciseListId[0]}&exercise=${exerciseListId[2]}&list=true`;
  155. setExtra(
  156. <ExerciseList
  157. courseId={exerciseListId[0]}
  158. articleId={exerciseListId[1]}
  159. exerciseId={exerciseListId[2]}
  160. />
  161. );
  162. break;
  163. default:
  164. url = `/v2/corpus/${type}/${articleId}/${mode}`;
  165. break;
  166. }
  167. console.log("url", url);
  168. get<IArticleResponse>(url).then((json) => {
  169. console.log("article", json);
  170. if (json.ok) {
  171. setArticleData(json.data);
  172. } else {
  173. message.error(json.message);
  174. }
  175. });
  176. }
  177. }, [active, type, articleId, mode, articleMode]);
  178. return (
  179. <>
  180. <ArticleView
  181. id={articleData?.uid}
  182. title={articleData?.title}
  183. subTitle={articleData?.subtitle}
  184. summary={articleData?.summary}
  185. content={articleData ? articleData.content : ""}
  186. html={articleData?.html}
  187. path={articleData?.path}
  188. created_at={articleData?.created_at}
  189. updated_at={articleData?.updated_at}
  190. channels={channels}
  191. type={type}
  192. articleId={articleId}
  193. />
  194. {extra}
  195. </>
  196. );
  197. };
  198. export default Widget;