AttachmentImport.tsx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import { Modal, Upload, type UploadProps, message } from "antd";
  2. import { useEffect, useState } from "react";
  3. import { InboxOutlined, ExclamationCircleOutlined } from "@ant-design/icons";
  4. import { API_HOST, delete_ } from "../../request";
  5. import { get as getToken } from "../../reducers/current-user";
  6. import type { UploadFile } from "antd/es/upload/interface";
  7. import type { IDeleteResponse } from "../../api/Group";
  8. import modal from "antd/lib/modal";
  9. const { Dragger } = Upload;
  10. export const deleteRes = (id: string) => {
  11. const url = `/v2/attachment/${id}`;
  12. console.info("attachment delete url", url);
  13. delete_<IDeleteResponse>(url)
  14. .then((json) => {
  15. if (json.ok) {
  16. message.success("删除成功");
  17. } else {
  18. message.error(json.message);
  19. }
  20. })
  21. .catch((e) => console.log("Oops errors!", e));
  22. };
  23. interface IWidget {
  24. replaceId?: string;
  25. open?: boolean;
  26. onOpenChange?: Function;
  27. }
  28. const AttachmentImportWidget = ({
  29. replaceId,
  30. open = false,
  31. onOpenChange,
  32. }: IWidget) => {
  33. const [isOpen, setIsOpen] = useState(open);
  34. useEffect(() => setIsOpen(open), [open]);
  35. const props: UploadProps = {
  36. name: "file",
  37. listType: "picture",
  38. multiple: replaceId ? false : true,
  39. action: `${API_HOST}/api/v2/attachment?id=${replaceId}`,
  40. headers: {
  41. Authorization: `Bearer ${getToken()}`,
  42. },
  43. onChange(info) {
  44. const { status } = info.file;
  45. if (status !== "uploading") {
  46. console.log(info.file, info.fileList);
  47. }
  48. if (status === "done") {
  49. message.success(`${info.file.name} file uploaded successfully.`);
  50. } else if (status === "error") {
  51. message.error(`${info.file.name} file upload failed.`);
  52. }
  53. },
  54. onDrop(e) {
  55. console.log("Dropped files", e.dataTransfer.files);
  56. },
  57. onRemove: (file: UploadFile<any>): boolean => {
  58. console.log("remove", file);
  59. deleteRes(file.response.data.id);
  60. return true;
  61. },
  62. };
  63. const handleOk = () => {
  64. setIsOpen(false);
  65. if (typeof onOpenChange !== "undefined") {
  66. onOpenChange(false);
  67. }
  68. };
  69. const handleCancel = () => {
  70. modal.confirm({
  71. title: "关闭上传窗口",
  72. icon: <ExclamationCircleOutlined />,
  73. content: "所有正在上传文件将取消上传。",
  74. okText: "确认",
  75. cancelText: "取消",
  76. onOk: () => {
  77. setIsOpen(false);
  78. if (typeof onOpenChange !== "undefined") {
  79. onOpenChange(false);
  80. }
  81. },
  82. });
  83. };
  84. return (
  85. <Modal
  86. destroyOnHidden={true}
  87. width={700}
  88. title="Upload"
  89. footer={false}
  90. maskClosable={false}
  91. open={isOpen}
  92. onOk={handleOk}
  93. onCancel={handleCancel}
  94. >
  95. <Dragger {...props}>
  96. <p className="ant-upload-drag-icon">
  97. <InboxOutlined />
  98. </p>
  99. <p className="ant-upload-text">
  100. Click or drag file to this area to upload
  101. </p>
  102. <p className="ant-upload-hint">
  103. Support for a single {replaceId ? "" : "or bulk"} upload. Strictly
  104. prohibit from uploading company data or other band files
  105. </p>
  106. </Dragger>
  107. </Modal>
  108. );
  109. };
  110. export default AttachmentImportWidget;