GroupFile.tsx 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. import { useIntl } from "react-intl";
  2. import { useRef, useState } from "react";
  3. import { type ActionType, ProList } from "@ant-design/pro-components";
  4. import { Space, Tag, Button, Layout, Popconfirm } from "antd";
  5. import { delete_, get } from "../../request";
  6. import type { IShareListResponse } from "../../api/Share";
  7. import type { IDeleteResponse } from "../../api/Group";
  8. const { Content } = Layout;
  9. interface IRoleTag {
  10. title: string;
  11. color: string;
  12. }
  13. interface DataItem {
  14. id?: string;
  15. name?: string;
  16. tag: IRoleTag[];
  17. image: string;
  18. description?: string;
  19. }
  20. interface IWidget {
  21. groupId?: string;
  22. }
  23. const GroupFileWidget = ({ groupId }: IWidget) => {
  24. const intl = useIntl(); //i18n
  25. const [canDelete, setCanDelete] = useState(false);
  26. const [resCount, setResCount] = useState(0);
  27. const ref = useRef<ActionType | null>(null);
  28. return (
  29. <Content>
  30. <ProList<DataItem>
  31. rowKey="id"
  32. actionRef={ref}
  33. headerTitle={
  34. intl.formatMessage({ id: "group.files" }) + "-" + resCount.toString()
  35. }
  36. showActions="hover"
  37. metas={{
  38. title: {
  39. dataIndex: "name",
  40. },
  41. avatar: {
  42. dataIndex: "image",
  43. editable: false,
  44. },
  45. description: {
  46. dataIndex: "description",
  47. },
  48. content: {
  49. dataIndex: "content",
  50. editable: false,
  51. },
  52. subTitle: {
  53. render: (_text, row, index, _action) => {
  54. const showtag = row.tag.map((item, id) => {
  55. return (
  56. <Tag color={item.color} key={id}>
  57. {item.title}
  58. </Tag>
  59. );
  60. });
  61. return (
  62. <Space size={0} key={index}>
  63. {showtag}
  64. </Space>
  65. );
  66. },
  67. },
  68. actions: {
  69. render: (_text, row, index, _action) => [
  70. canDelete ? (
  71. <Popconfirm
  72. key={index}
  73. title={intl.formatMessage({
  74. id: "forms.message.res.remove",
  75. })}
  76. onConfirm={(
  77. _e?: React.MouseEvent<HTMLElement, MouseEvent>
  78. ) => {
  79. console.log("delete", row.id);
  80. delete_<IDeleteResponse>("/v2/share/" + row.id).then(
  81. (json) => {
  82. if (json.ok) {
  83. console.log("delete ok");
  84. ref.current?.reload();
  85. }
  86. }
  87. );
  88. }}
  89. okText={intl.formatMessage({ id: "buttons.ok" })}
  90. cancelText={intl.formatMessage({ id: "buttons.cancel" })}
  91. >
  92. <Button size="small" type="link" danger key="link">
  93. {intl.formatMessage({ id: "buttons.remove" })}
  94. </Button>
  95. </Popconfirm>
  96. ) : (
  97. <></>
  98. ),
  99. ],
  100. },
  101. }}
  102. request={async (params = {}, sorter, filter) => {
  103. console.log(params, sorter, filter);
  104. let url = `/v2/share?view=group&id=${groupId}`;
  105. const offset =
  106. ((params.current ? params.current : 1) - 1) *
  107. (params.pageSize ? params.pageSize : 20);
  108. url += `&limit=${params.pageSize}&offset=${offset}`;
  109. if (typeof params.keyword !== "undefined") {
  110. url += "&search=" + (params.keyword ? params.keyword : "");
  111. }
  112. const res = await get<IShareListResponse>(url);
  113. if (res.ok) {
  114. console.log(res.data.rows);
  115. setResCount(res.data.count);
  116. switch (res.data.role) {
  117. case "owner":
  118. setCanDelete(true);
  119. break;
  120. case "manager":
  121. setCanDelete(true);
  122. break;
  123. }
  124. const items: DataItem[] = res.data.rows.map((item, _id) => {
  125. const member: DataItem = {
  126. id: item.id,
  127. name: item.res_name,
  128. tag: [],
  129. image: "",
  130. description: item.owner?.nickName,
  131. };
  132. switch (item.power) {
  133. case 0:
  134. member.tag.push({ title: "拥有者", color: "success" });
  135. break;
  136. case 1:
  137. member.tag.push({ title: "管理员", color: "default" });
  138. break;
  139. }
  140. return member;
  141. });
  142. console.log(items);
  143. return {
  144. total: res.data.count,
  145. succcess: true,
  146. data: items,
  147. };
  148. } else {
  149. console.error(res.message);
  150. return {
  151. total: 0,
  152. succcess: false,
  153. data: [],
  154. };
  155. }
  156. }}
  157. pagination={{
  158. showQuickJumper: true,
  159. showSizeChanger: true,
  160. }}
  161. />
  162. </Content>
  163. );
  164. };
  165. export default GroupFileWidget;