TextBookToc.tsx 1.1 KB

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