ArticleListPublic.tsx 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import { Link } from "react-router";
  2. import { useRef } from "react";
  3. import { Space } from "antd";
  4. import { type ActionType, ProList } from "@ant-design/pro-components";
  5. import { get } from "../../request";
  6. import type { IArticleListResponse } from "../../api/Article";
  7. import type { IStudio } from "../auth/Studio";
  8. import type { IUser } from "../auth/User";
  9. import TimeShow from "../general/TimeShow";
  10. interface DataItem {
  11. sn: number;
  12. id: string;
  13. title: string;
  14. subtitle: string;
  15. summary?: string | null;
  16. anthologyCount?: number;
  17. anthologyTitle?: string;
  18. publicity: number;
  19. createdAt?: string;
  20. updatedAt: string;
  21. studio?: IStudio;
  22. editor?: IUser;
  23. }
  24. interface IWidget {
  25. search?: string;
  26. studioName?: string;
  27. }
  28. const ArticleListWidget = ({ studioName }: IWidget) => {
  29. const ref = useRef<ActionType | null>(null);
  30. return (
  31. <>
  32. <ProList<DataItem>
  33. rowKey="id"
  34. actionRef={ref}
  35. metas={{
  36. title: {
  37. render: (_text, row) => {
  38. return <Link to={`/article/article/${row.id}`}>{row.title}</Link>;
  39. },
  40. },
  41. description: {
  42. dataIndex: "summary",
  43. },
  44. subTitle: {
  45. render: (_text, row) => {
  46. return (
  47. <Space>
  48. {row.editor?.nickName}
  49. <TimeShow
  50. updatedAt={row.updatedAt}
  51. showLabel={false}
  52. showIcon={false}
  53. />
  54. </Space>
  55. );
  56. },
  57. },
  58. }}
  59. request={async (params = {}) => {
  60. let url = `/v2/article?view=public`;
  61. const offset =
  62. ((params.current ? params.current : 1) - 1) *
  63. (params.pageSize ? params.pageSize : 20);
  64. url += `&limit=${params.pageSize}&offset=${offset}`;
  65. url += params.keyword ? "&search=" + params.keyword : "";
  66. url += studioName ? "&studio=" + studioName : "";
  67. const res = await get<IArticleListResponse>(url);
  68. const items: DataItem[] = res.data.rows.map((item, id) => {
  69. return {
  70. sn: id + 1,
  71. id: item.uid,
  72. title: item.title,
  73. subtitle: item.subtitle,
  74. summary: item.summary,
  75. anthologyCount: item.anthology_count,
  76. anthologyTitle: item.anthology_first?.title,
  77. publicity: item.status,
  78. updatedAt: item.updated_at,
  79. studio: item.studio,
  80. editor: item.editor,
  81. };
  82. });
  83. return {
  84. total: res.data.count,
  85. succcess: true,
  86. data: items,
  87. };
  88. }}
  89. bordered
  90. pagination={{
  91. showQuickJumper: true,
  92. showSizeChanger: true,
  93. pageSize: 20,
  94. }}
  95. search={false}
  96. options={{
  97. search: true,
  98. }}
  99. />
  100. </>
  101. );
  102. };
  103. export default ArticleListWidget;