GroupFile.tsx 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. import { useIntl } from "react-intl";
  2. import { useRef, useState } from "react";
  3. import { ActionType, ProList } from "@ant-design/pro-components";
  4. import { Space, Tag, Button, Layout, Popconfirm } from "antd";
  5. import { delete_, get } from "../../request";
  6. import { IShareListResponse } from "../api/Share";
  7. import { 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>();
  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. // TODO
  104. console.log(params, sorter, filter);
  105. let url = `/v2/share?view=group&id=${groupId}`;
  106. const offset =
  107. ((params.current ? params.current : 1) - 1) *
  108. (params.pageSize ? params.pageSize : 20);
  109. url += `&limit=${params.pageSize}&offset=${offset}`;
  110. if (typeof params.keyword !== "undefined") {
  111. url += "&search=" + (params.keyword ? params.keyword : "");
  112. }
  113. const res = await get<IShareListResponse>(url);
  114. if (res.ok) {
  115. console.log(res.data.rows);
  116. setResCount(res.data.count);
  117. switch (res.data.role) {
  118. case "owner":
  119. setCanDelete(true);
  120. break;
  121. case "manager":
  122. setCanDelete(true);
  123. break;
  124. }
  125. const items: DataItem[] = res.data.rows.map((item, id) => {
  126. let member: DataItem = {
  127. id: item.id,
  128. name: item.res_name,
  129. tag: [],
  130. image: "",
  131. description: item.owner?.nickName,
  132. };
  133. switch (item.power) {
  134. case 0:
  135. member.tag.push({ title: "拥有者", color: "success" });
  136. break;
  137. case 1:
  138. member.tag.push({ title: "管理员", color: "default" });
  139. break;
  140. }
  141. return member;
  142. });
  143. console.log(items);
  144. return {
  145. total: res.data.count,
  146. succcess: true,
  147. data: items,
  148. };
  149. } else {
  150. console.error(res.message);
  151. return {
  152. total: 0,
  153. succcess: false,
  154. data: [],
  155. };
  156. }
  157. }}
  158. pagination={{
  159. showQuickJumper: true,
  160. showSizeChanger: true,
  161. }}
  162. />
  163. </Content>
  164. );
  165. };
  166. export default GroupFileWidget;