TextBookToc.tsx 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import { useEffect, useState } from "react";
  2. import AnthologyTocTree from "./AnthologyTocTree";
  3. import { get } from "../../request";
  4. import type { ICourseResponse } from "../../api/Course";
  5. import type { TTarget } from "../../types";
  6. interface IWidget {
  7. courseId?: string | null;
  8. channels?: string[];
  9. onClick?: (article: string, target?: TTarget) => void;
  10. }
  11. const TextBookTocWidget = ({ courseId, channels, onClick }: IWidget) => {
  12. const [anthologyId, setAnthologyId] = useState<string>();
  13. useEffect(() => {
  14. if (!courseId) {
  15. return;
  16. }
  17. const url = `/api/v2/course/${courseId}`;
  18. console.debug("course url", url);
  19. get<ICourseResponse>(url).then((json) => {
  20. console.debug("course data", json.data);
  21. if (json.ok) {
  22. setAnthologyId(json.data.anthology_id);
  23. }
  24. });
  25. }, [courseId]);
  26. return (
  27. <AnthologyTocTree
  28. anthologyId={anthologyId}
  29. channels={channels}
  30. onClick={(_anthology, article, target) => {
  31. console.debug("AnthologyTocTree onClick", article);
  32. if (typeof onClick !== "undefined") {
  33. onClick(article, target);
  34. }
  35. }}
  36. />
  37. );
  38. };
  39. export default TextBookTocWidget;