AnthologyDetail.tsx 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import { useState, useEffect } from "react";
  2. import { Space, Typography, message } from "antd";
  3. import { get } from "../../request";
  4. import type {
  5. IAnthologyDataResponse,
  6. IAnthologyResponse,
  7. } from "../api/Article";
  8. import type { IAnthologyData } from "./AnthologyCard";
  9. import StudioName from "../auth/Studio";
  10. import TimeShow from "../general/TimeShow";
  11. import Marked from "../general/Marked";
  12. import AnthologyTocTree from "../anthology/AnthologyTocTree";
  13. const { Title, Text, Paragraph } = Typography;
  14. interface IWidgetAnthologyDetail {
  15. aid?: string;
  16. channels?: string[];
  17. visible?: boolean;
  18. onArticleSelect?: Function;
  19. onArticleClick?: Function;
  20. onLoad?: Function;
  21. onTitle?: Function;
  22. onLoading?: Function;
  23. onError?: Function;
  24. }
  25. const AnthologyDetailWidget = ({
  26. aid,
  27. channels,
  28. visible = true,
  29. onArticleSelect,
  30. onArticleClick,
  31. onLoading,
  32. onTitle,
  33. onError,
  34. }: IWidgetAnthologyDetail) => {
  35. const [tableData, setTableData] = useState<IAnthologyData>();
  36. useEffect(() => {
  37. fetchData(aid);
  38. }, [aid]);
  39. function fetchData(id?: string) {
  40. const url = `/v2/anthology/${id}`;
  41. console.log("url", url);
  42. if (typeof onLoading !== "undefined") {
  43. onLoading(true);
  44. }
  45. get<IAnthologyResponse>(url)
  46. .then((response) => {
  47. if (response.ok) {
  48. const item: IAnthologyDataResponse = response.data;
  49. let newTree: IAnthologyData = {
  50. id: item.uid,
  51. title: item.title,
  52. subTitle: item.subtitle,
  53. summary: item.summary,
  54. articles: [],
  55. studio: item.studio,
  56. created_at: item.created_at,
  57. updated_at: item.updated_at,
  58. };
  59. setTableData(newTree);
  60. if (typeof onTitle !== "undefined") {
  61. onTitle(item.title);
  62. }
  63. console.log("toc", newTree.articles);
  64. } else {
  65. if (typeof onError !== "undefined") {
  66. onError(response.data, response.message);
  67. }
  68. message.error(response.message);
  69. }
  70. })
  71. .finally(() => {
  72. if (typeof onLoading !== "undefined") {
  73. onLoading(false);
  74. }
  75. })
  76. .catch((error) => {
  77. console.error(error);
  78. if (typeof onError !== "undefined") {
  79. onError(error, "");
  80. }
  81. });
  82. }
  83. return (
  84. <div style={{ padding: 12, visibility: visible ? "visible" : "hidden" }}>
  85. <Title level={4}>{tableData?.title}</Title>
  86. <div>
  87. <Text type="secondary">{tableData?.subTitle}</Text>
  88. </div>
  89. <Paragraph>
  90. <Space>
  91. <StudioName data={tableData?.studio} />
  92. <TimeShow updatedAt={tableData?.updated_at} />
  93. </Space>
  94. </Paragraph>
  95. <Paragraph>
  96. <Marked text={tableData?.summary} />
  97. </Paragraph>
  98. <Title level={5}>目录</Title>
  99. <AnthologyTocTree
  100. anthologyId={aid}
  101. channels={channels}
  102. onClick={(anthologyId: string, id: string, target: string) => {
  103. if (typeof onArticleClick !== "undefined") {
  104. onArticleClick(anthologyId, id, target);
  105. }
  106. }}
  107. />
  108. </div>
  109. );
  110. };
  111. export default AnthologyDetailWidget;