AnthologyList.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. import { type ActionType, ProTable } from "@ant-design/pro-components";
  2. import { useIntl } from "react-intl";
  3. import { Link } from "react-router";
  4. import { message, Modal, Typography } from "antd";
  5. import { PlusOutlined } from "@ant-design/icons";
  6. import { Button, Dropdown, Popover } from "antd";
  7. import {
  8. ExclamationCircleOutlined,
  9. TeamOutlined,
  10. DeleteOutlined,
  11. EyeOutlined,
  12. } from "@ant-design/icons";
  13. import AnthologyCreate from "./AnthologyCreate";
  14. import type {
  15. IAnthologyListResponse,
  16. IDeleteResponse,
  17. } from "../../api/Article";
  18. import { delete_, get } from "../../request";
  19. import { PublicityValueEnum } from "../studio/table";
  20. import { useEffect, useRef, useState } from "react";
  21. import Share, { EResType } from "../share/Share";
  22. import StudioName, { type IStudio } from "../auth/Studio";
  23. import { type IResNumberResponse, renderBadge } from "../channel/ChannelTable";
  24. import { fullUrl, getSorterUrl } from "../../utils";
  25. const { Text } = Typography;
  26. interface IItem {
  27. sn: number;
  28. id: string;
  29. title: string;
  30. subtitle: string;
  31. publicity: number;
  32. articles: number;
  33. studio?: IStudio;
  34. updated_at: string;
  35. }
  36. interface IWidget {
  37. title?: string;
  38. studioName?: string;
  39. showCol?: string[];
  40. showCreate?: boolean;
  41. showOption?: boolean;
  42. onTitleClick?: (id: string) => void;
  43. }
  44. const AnthologyListWidget = ({
  45. title,
  46. studioName,
  47. showCreate = true,
  48. showOption = true,
  49. onTitleClick,
  50. }: IWidget) => {
  51. const intl = useIntl();
  52. const [openCreate, setOpenCreate] = useState(false);
  53. const [activeKey, setActiveKey] = useState<React.Key | undefined>("my");
  54. const [myNumber, setMyNumber] = useState<number>(0);
  55. const [collaborationNumber, setCollaborationNumber] = useState<number>(0);
  56. useEffect(() => {
  57. /**
  58. * 获取各种课程的数量
  59. */
  60. const url = `/v2/anthology-my-number?studio=${studioName}`;
  61. console.log("url", url);
  62. get<IResNumberResponse>(url).then((json) => {
  63. if (json.ok) {
  64. setMyNumber(json.data.my);
  65. setCollaborationNumber(json.data.collaboration);
  66. }
  67. });
  68. }, [studioName]);
  69. const showDeleteConfirm = (id: string, title: string) => {
  70. Modal.confirm({
  71. icon: <ExclamationCircleOutlined />,
  72. title:
  73. intl.formatMessage({
  74. id: "message.delete.confirm",
  75. }) +
  76. intl.formatMessage({
  77. id: "message.irrevocable",
  78. }),
  79. content: title,
  80. okText: intl.formatMessage({
  81. id: "buttons.delete",
  82. }),
  83. okType: "danger",
  84. cancelText: intl.formatMessage({
  85. id: "buttons.no",
  86. }),
  87. onOk() {
  88. console.log("delete", id);
  89. return delete_<IDeleteResponse>(`/v2/anthology/${id}`)
  90. .then((json) => {
  91. if (json.ok) {
  92. message.success("删除成功");
  93. ref.current?.reload();
  94. } else {
  95. message.error(json.message);
  96. }
  97. })
  98. .catch((e) => console.log("Oops errors!", e));
  99. },
  100. });
  101. };
  102. const [isModalOpen, setIsModalOpen] = useState(false);
  103. const [shareResId, setShareResId] = useState<string>("");
  104. const [shareResType, setShareResType] = useState<EResType>(
  105. EResType.collection
  106. );
  107. const showShareModal = (resId: string, resType: EResType) => {
  108. setShareResId(resId);
  109. setShareResType(resType);
  110. setIsModalOpen(true);
  111. };
  112. const handleOk = () => {
  113. setIsModalOpen(false);
  114. };
  115. const handleCancel = () => {
  116. setIsModalOpen(false);
  117. };
  118. const ref = useRef<ActionType | null>(null);
  119. return (
  120. <>
  121. <ProTable<IItem>
  122. headerTitle={title}
  123. actionRef={ref}
  124. columns={[
  125. {
  126. title: intl.formatMessage({
  127. id: "dict.fields.sn.label",
  128. }),
  129. dataIndex: "sn",
  130. key: "sn",
  131. width: 50,
  132. search: false,
  133. },
  134. {
  135. title: intl.formatMessage({
  136. id: "forms.fields.title.label",
  137. }),
  138. dataIndex: "title",
  139. key: "title",
  140. tooltip: "过长会自动收缩",
  141. ellipsis: true,
  142. render: (_text, row, index) => {
  143. return (
  144. <div key={index}>
  145. <div>
  146. <Typography.Link
  147. onClick={() => {
  148. if (typeof onTitleClick !== "undefined") {
  149. onTitleClick(row.id);
  150. }
  151. }}
  152. >
  153. {row.title}
  154. </Typography.Link>
  155. </div>
  156. <Text type="secondary">{row.subtitle}</Text>
  157. </div>
  158. );
  159. },
  160. },
  161. {
  162. title: intl.formatMessage({
  163. id: "forms.fields.owner.label",
  164. }),
  165. dataIndex: "studio",
  166. key: "studio",
  167. render: (_text, row) => {
  168. return <StudioName data={row.studio} />;
  169. },
  170. },
  171. {
  172. title: intl.formatMessage({
  173. id: "forms.fields.publicity.label",
  174. }),
  175. dataIndex: "publicity",
  176. key: "publicity",
  177. width: 100,
  178. search: false,
  179. filters: true,
  180. onFilter: true,
  181. valueEnum: PublicityValueEnum(),
  182. },
  183. {
  184. title: intl.formatMessage({
  185. id: "article.fields.article.count.label",
  186. }),
  187. dataIndex: "articles",
  188. key: "articles",
  189. width: 100,
  190. search: false,
  191. },
  192. {
  193. title: intl.formatMessage({
  194. id: "forms.fields.updated-at.label",
  195. }),
  196. key: "updated_at",
  197. width: 100,
  198. search: false,
  199. dataIndex: "updated_at",
  200. valueType: "date",
  201. sorter: true,
  202. },
  203. {
  204. title: intl.formatMessage({ id: "buttons.option" }),
  205. key: "option",
  206. width: 120,
  207. hideInTable: !showOption,
  208. valueType: "option",
  209. render: (_text, row, index) => [
  210. <Dropdown.Button
  211. key={index}
  212. type="link"
  213. trigger={["click", "contextMenu"]}
  214. menu={{
  215. items: [
  216. {
  217. key: "open",
  218. label: (
  219. <Link to={`/anthology/${row.id}`}>
  220. {intl.formatMessage({
  221. id: "buttons.open.in.library",
  222. })}
  223. </Link>
  224. ),
  225. icon: <EyeOutlined />,
  226. },
  227. {
  228. key: "share",
  229. label: intl.formatMessage({
  230. id: "buttons.share",
  231. }),
  232. icon: <TeamOutlined />,
  233. },
  234. {
  235. key: "remove",
  236. label: intl.formatMessage({
  237. id: "buttons.delete",
  238. }),
  239. icon: <DeleteOutlined />,
  240. danger: true,
  241. },
  242. ],
  243. onClick: (e) => {
  244. switch (e.key) {
  245. case "open":
  246. window.open(fullUrl(`/anthology/${row.id}`), "_blank");
  247. break;
  248. case "share":
  249. console.log("share");
  250. showShareModal(row.id, EResType.collection);
  251. break;
  252. case "remove":
  253. showDeleteConfirm(row.id, row.title);
  254. break;
  255. }
  256. },
  257. }}
  258. >
  259. <Link to={`/anthology/${row.id}`} target="_blank">
  260. {intl.formatMessage({
  261. id: "buttons.view",
  262. })}
  263. </Link>
  264. </Dropdown.Button>,
  265. ],
  266. },
  267. ]}
  268. request={async (params = {}, sorter, filter) => {
  269. console.log(params, sorter, filter);
  270. let url = `/v2/anthology?view=studio&view2=${activeKey}&name=${studioName}`;
  271. const offset =
  272. ((params.current ? params.current : 1) - 1) *
  273. (params.pageSize ? params.pageSize : 20);
  274. url += `&limit=${params.pageSize}&offset=${offset}`;
  275. url += params.keyword ? "&search=" + params.keyword : "";
  276. url += getSorterUrl(sorter);
  277. const res = await get<IAnthologyListResponse>(url);
  278. const items: IItem[] = res.data.rows.map((item, id) => {
  279. return {
  280. sn: id + offset + 1,
  281. id: item.uid,
  282. title: item.title,
  283. subtitle: item.subtitle,
  284. publicity: item.status,
  285. articles: item.childrenNumber,
  286. studio: item.studio,
  287. updated_at: item.updated_at,
  288. };
  289. });
  290. console.log(items);
  291. return {
  292. total: res.data.count,
  293. succcess: true,
  294. data: items,
  295. };
  296. }}
  297. rowKey="id"
  298. bordered
  299. pagination={{
  300. showQuickJumper: true,
  301. showSizeChanger: true,
  302. pageSize: 10,
  303. }}
  304. search={false}
  305. options={{
  306. search: true,
  307. }}
  308. toolBarRender={() => [
  309. showCreate ? (
  310. <Popover
  311. content={
  312. <AnthologyCreate
  313. studio={studioName}
  314. onSuccess={() => {
  315. setOpenCreate(false);
  316. ref.current?.reload();
  317. }}
  318. />
  319. }
  320. placement="bottomRight"
  321. trigger="click"
  322. open={openCreate}
  323. onOpenChange={(open: boolean) => {
  324. setOpenCreate(open);
  325. }}
  326. >
  327. <Button key="button" icon={<PlusOutlined />} type="primary">
  328. {intl.formatMessage({ id: "buttons.create" })}
  329. </Button>
  330. </Popover>
  331. ) : undefined,
  332. ]}
  333. toolbar={{
  334. menu: {
  335. activeKey,
  336. items: [
  337. {
  338. key: "my",
  339. label: (
  340. <span>
  341. {intl.formatMessage({ id: "labels.this-studio" })}
  342. {renderBadge(myNumber, activeKey === "my")}
  343. </span>
  344. ),
  345. },
  346. {
  347. key: "collaboration",
  348. label: (
  349. <span>
  350. {intl.formatMessage({ id: "labels.collaboration" })}
  351. {renderBadge(
  352. collaborationNumber,
  353. activeKey === "collaboration"
  354. )}
  355. </span>
  356. ),
  357. },
  358. ],
  359. onChange(key) {
  360. console.log("show course", key);
  361. setActiveKey(key);
  362. ref.current?.reload();
  363. },
  364. },
  365. }}
  366. />
  367. <Modal
  368. destroyOnHidden={true}
  369. width={700}
  370. title={intl.formatMessage({ id: "labels.collaboration" })}
  371. open={isModalOpen}
  372. onOk={handleOk}
  373. onCancel={handleCancel}
  374. >
  375. <Share resId={shareResId} resType={shareResType} />
  376. </Modal>
  377. </>
  378. );
  379. };
  380. export default AnthologyListWidget;