AnthologiesAtArticle.tsx 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import { Space, Typography, message } from "antd";
  2. import { useEffect, useState } from "react";
  3. import { get } from "../../request";
  4. import type { IArticleMapListResponse } from "../../api/Article";
  5. const { Link, Paragraph } = Typography;
  6. interface IList {
  7. key?: string;
  8. label?: string;
  9. }
  10. interface IWidget {
  11. articleId?: string;
  12. anthologyId?: string | null;
  13. onClick?: Function;
  14. }
  15. const AnthologiesAtArticleWidget = ({
  16. articleId,
  17. anthologyId,
  18. onClick,
  19. }: IWidget) => {
  20. const [list, setList] = useState<IList[]>();
  21. useEffect(() => {
  22. //查询这个article 有多少文集
  23. const url = `/v2/article-map?view=article&id=${articleId}`;
  24. console.log("url", url);
  25. get<IArticleMapListResponse>(url).then((json) => {
  26. if (json.ok) {
  27. const anthologies: IList[] = json.data.rows.map((item) => {
  28. return {
  29. key: item.collection?.id,
  30. label: item.collection?.title,
  31. };
  32. });
  33. console.log("anthologies", anthologies);
  34. setList(anthologies.filter((value) => value.key !== anthologyId));
  35. } else {
  36. message.error("获取文集列表失败");
  37. }
  38. });
  39. }, [articleId]);
  40. let title = "";
  41. if (anthologyId) {
  42. title = "其他文集";
  43. } else {
  44. title = "文集列表";
  45. }
  46. return (
  47. <Paragraph style={{ display: list && list.length > 0 ? "block" : "none" }}>
  48. <Space>
  49. {title}
  50. {list?.map((item, index) => {
  51. return (
  52. <Link
  53. key={index}
  54. onClick={(e) => {
  55. if (typeof onClick !== "undefined") {
  56. onClick(item.key, e);
  57. }
  58. }}
  59. >
  60. {item.label}
  61. </Link>
  62. );
  63. })}
  64. </Space>
  65. </Paragraph>
  66. );
  67. };
  68. export default AnthologiesAtArticleWidget;