AnthologyList.tsx 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import { useState, useEffect } from "react";
  2. import { List } from "antd";
  3. import { get } from "../../request";
  4. import type { IAnthologyListResponse } from "../../api/Article";
  5. import AnthologyCard from "./AnthologyCard";
  6. import type { IAnthologyData } from "./AnthologyCard";
  7. interface IWidget {
  8. studioName?: string;
  9. searchKey?: string;
  10. }
  11. const AnthologyListWidget = ({ studioName, searchKey }: IWidget) => {
  12. const [tableData, setTableData] = useState<IAnthologyData[]>([]);
  13. const [total, setTotal] = useState<number>();
  14. const [currPage, setCurrPage] = useState<number>(1);
  15. const pageSize = 20;
  16. useEffect(() => {
  17. const offset = (currPage - 1) * pageSize;
  18. let url = `/v2/anthology?view=public&offset=${offset}&limit=${pageSize}`;
  19. if (typeof studioName !== "undefined") {
  20. url += `&studio=${studioName}`;
  21. }
  22. if (typeof searchKey === "string" && searchKey.length > 0) {
  23. url += `&search=${searchKey}`;
  24. }
  25. console.log("get-url", url);
  26. get<IAnthologyListResponse>(url).then(function (json) {
  27. if (json.ok) {
  28. const newTree: IAnthologyData[] = json.data.rows.map((item) => {
  29. return {
  30. id: item.uid,
  31. title: item.title,
  32. subTitle: item.subtitle,
  33. summary: item.summary,
  34. articles: item.article_list.map((al) => {
  35. return {
  36. key: al.article,
  37. title: al.title,
  38. level: parseInt(al.level),
  39. };
  40. }),
  41. studio: item.studio,
  42. created_at: item.created_at,
  43. updated_at: item.updated_at,
  44. };
  45. });
  46. setTableData(newTree);
  47. setTotal(json.data.count);
  48. } else {
  49. setTableData([]);
  50. setTotal(0);
  51. }
  52. });
  53. }, [currPage, searchKey, studioName]);
  54. return (
  55. <List
  56. itemLayout="vertical"
  57. size="large"
  58. dataSource={tableData}
  59. pagination={{
  60. onChange: (page) => {
  61. console.log(page);
  62. setCurrPage(page);
  63. },
  64. showQuickJumper: true,
  65. showSizeChanger: false,
  66. pageSize: pageSize,
  67. total: total,
  68. position: "both",
  69. showTotal: (total) => {
  70. return `结果: ${total}`;
  71. },
  72. }}
  73. renderItem={(item) => (
  74. <List.Item>
  75. <AnthologyCard data={item} />
  76. </List.Item>
  77. )}
  78. />
  79. );
  80. };
  81. export default AnthologyListWidget;