GroupFile.tsx 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. metas={{
  37. title: {
  38. dataIndex: "name",
  39. },
  40. avatar: {
  41. dataIndex: "image",
  42. editable: false,
  43. },
  44. description: {
  45. dataIndex: "description",
  46. },
  47. content: {
  48. dataIndex: "content",
  49. editable: false,
  50. },
  51. subTitle: {
  52. render: (_text, row, index) => {
  53. const showtag = row.tag.map((item, id) => {
  54. return (
  55. <Tag color={item.color} key={id}>
  56. {item.title}
  57. </Tag>
  58. );
  59. });
  60. return (
  61. <Space size={0} key={index}>
  62. {showtag}
  63. </Space>
  64. );
  65. },
  66. },
  67. actions: {
  68. render: (_text, row, index) => [
  69. canDelete ? (
  70. <Popconfirm
  71. key={index}
  72. title={intl.formatMessage({
  73. id: "forms.message.res.remove",
  74. })}
  75. onConfirm={() => {
  76. console.log("delete", row.id);
  77. delete_<IDeleteResponse>("/api/v2/share/" + row.id).then(
  78. (json) => {
  79. if (json.ok) {
  80. console.log("delete ok");
  81. ref.current?.reload();
  82. }
  83. }
  84. );
  85. }}
  86. okText={intl.formatMessage({ id: "buttons.ok" })}
  87. cancelText={intl.formatMessage({ id: "buttons.cancel" })}
  88. >
  89. <Button size="small" type="link" danger key="link">
  90. {intl.formatMessage({ id: "buttons.remove" })}
  91. </Button>
  92. </Popconfirm>
  93. ) : (
  94. <></>
  95. ),
  96. ],
  97. },
  98. }}
  99. request={async (params = {}, sorter, filter) => {
  100. console.log(params, sorter, filter);
  101. let url = `/api/v2/share?view=group&id=${groupId}`;
  102. const offset =
  103. ((params.current ? params.current : 1) - 1) *
  104. (params.pageSize ? params.pageSize : 20);
  105. url += `&limit=${params.pageSize}&offset=${offset}`;
  106. if (typeof params.keyword !== "undefined") {
  107. url += "&search=" + (params.keyword ? params.keyword : "");
  108. }
  109. const res = await get<IShareListResponse>(url);
  110. if (res.ok) {
  111. console.log(res.data.rows);
  112. setResCount(res.data.count);
  113. switch (res.data.role) {
  114. case "owner":
  115. setCanDelete(true);
  116. break;
  117. case "manager":
  118. setCanDelete(true);
  119. break;
  120. }
  121. const items: DataItem[] = res.data.rows.map((item) => {
  122. const member: DataItem = {
  123. id: item.id,
  124. name: item.res_name,
  125. tag: [],
  126. image: "",
  127. description: item.owner?.nickName,
  128. };
  129. switch (item.power) {
  130. case 0:
  131. member.tag.push({ title: "拥有者", color: "success" });
  132. break;
  133. case 1:
  134. member.tag.push({ title: "管理员", color: "default" });
  135. break;
  136. }
  137. return member;
  138. });
  139. console.log(items);
  140. return {
  141. total: res.data.count,
  142. succcess: true,
  143. data: items,
  144. };
  145. } else {
  146. console.error(res.message);
  147. return {
  148. total: 0,
  149. succcess: false,
  150. data: [],
  151. };
  152. }
  153. }}
  154. pagination={{
  155. showQuickJumper: true,
  156. showSizeChanger: true,
  157. }}
  158. />
  159. </Content>
  160. );
  161. };
  162. export default GroupFileWidget;