Article.tsx 6.0 KB

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