Exercise.tsx 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import { Button, Card, Space, Switch } from "antd";
  2. import { Link, useParams } from "react-router";
  3. import { useAppSelector } from "../../hooks";
  4. import { courseUser } from "../../reducers/course-user";
  5. import SelectChannel from "../course/SelectChannel";
  6. import { currentUser as _currentUser } from "../../reducers/current-user";
  7. import { courseInfo } from "../../reducers/current-course";
  8. interface IWidgetExerciseCtl {
  9. id?: string;
  10. title?: string;
  11. channel?: string;
  12. children?: React.ReactNode;
  13. }
  14. const ExerciseCtl = ({ id, title, channel, children }: IWidgetExerciseCtl) => {
  15. const { type } = useParams(); //url 参数
  16. const cardTitle = title ? title : "练习";
  17. const user = useAppSelector(courseUser);
  18. const currUser = useAppSelector(_currentUser);
  19. const currCourse = useAppSelector(courseInfo);
  20. let exeButton = <></>;
  21. switch (user?.role) {
  22. case "student":
  23. switch (type) {
  24. case "textbook":
  25. if (typeof user.channelId === "string") {
  26. exeButton = (
  27. <Link
  28. to={`/article/exercise/${currCourse?.courseId}_${currCourse?.articleId}_${id}_${currUser?.realName}/wbw`}
  29. target="_blank"
  30. >
  31. <Button type="primary">做练习</Button>
  32. </Link>
  33. );
  34. } else {
  35. exeButton = <SelectChannel exerciseId={id} channel={channel} />;
  36. }
  37. break;
  38. default:
  39. exeButton = (
  40. <Space>
  41. 问题筛选
  42. <Switch
  43. onChange={(checked: boolean) => {
  44. console.log(`switch to ${checked}`);
  45. }}
  46. />
  47. 对答案
  48. <Switch
  49. onChange={(checked: boolean) => {
  50. console.log(`switch to ${checked}`);
  51. }}
  52. />
  53. </Space>
  54. );
  55. break;
  56. }
  57. break;
  58. case "assistant":
  59. if (type === "textbook") {
  60. exeButton = (
  61. <Link
  62. to={`/article/exercise-list/${currCourse?.courseId}_${currCourse?.articleId}_${id}/wbw`}
  63. target="_blank"
  64. >
  65. <Button type="primary">查看作业列表</Button>
  66. </Link>
  67. );
  68. }
  69. break;
  70. default:
  71. break;
  72. }
  73. return (
  74. <Card
  75. title={cardTitle}
  76. extra={exeButton}
  77. style={{ backgroundColor: "beige" }}
  78. >
  79. {children}
  80. </Card>
  81. );
  82. };
  83. interface IWidget {
  84. props: string;
  85. children?: React.ReactNode;
  86. }
  87. const Widget = ({ props, children }: IWidget) => {
  88. const prop: IWidgetExerciseCtl = JSON.parse(atob(props));
  89. return <ExerciseCtl {...prop}>{children}</ExerciseCtl>;
  90. };
  91. export default Widget;