TransferCreate.tsx 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import { ModalForm, ProForm } from "@ant-design/pro-components";
  2. import { Alert, Form, message, notification } from "antd";
  3. import { post } from "../../request";
  4. import type {
  5. ITransferCreateResponse,
  6. ITransferRequest,
  7. } from "../../api/Transfer";
  8. import { useIntl } from "react-intl";
  9. import UserSelect from "../template/UserSelect";
  10. import { useEffect, useState } from "react";
  11. import type { TResType } from "../discussion/DiscussionListCard";
  12. interface IWidget {
  13. studioName?: string;
  14. resType: TResType;
  15. resId?: string[];
  16. resName?: string;
  17. open?: boolean;
  18. onOpenChange?: Function;
  19. onCreate?: Function;
  20. }
  21. const TransferCreateWidget = ({
  22. __studioName,
  23. resType,
  24. resId,
  25. resName,
  26. open = false,
  27. onOpenChange,
  28. onCreate,
  29. }: IWidget) => {
  30. const intl = useIntl();
  31. const [form] = Form.useForm<{ studio: string }>();
  32. const [modalVisit, setModalVisit] = useState(open);
  33. useEffect(() => setModalVisit(open), [open]);
  34. const strTransfer = intl.formatMessage({
  35. id: `columns.studio.transfer.title`,
  36. });
  37. return (
  38. <ModalForm<{
  39. studio: string;
  40. }>
  41. open={modalVisit}
  42. onOpenChange={(visible) => {
  43. if (typeof onOpenChange !== "undefined") {
  44. onOpenChange(visible);
  45. }
  46. }}
  47. title={intl.formatMessage({
  48. id: `columns.studio.transfer.title`,
  49. })}
  50. form={form}
  51. autoFocusFirstInput
  52. modalProps={{
  53. destroyOnClose: true,
  54. onCancel: () => console.log("run"),
  55. }}
  56. submitTimeout={2000}
  57. onFinish={async (values) => {
  58. console.log(values);
  59. if (typeof resId === "undefined") {
  60. console.error("res id is undefined");
  61. return;
  62. }
  63. const data = {
  64. res_type: resType,
  65. res_id: resId,
  66. new_owner: values.studio,
  67. };
  68. const res = await post<ITransferRequest, ITransferCreateResponse>(
  69. `/v2/transfer`,
  70. data
  71. );
  72. if (res.ok) {
  73. if (typeof onCreate === "undefined") {
  74. notification.open({
  75. message: strTransfer,
  76. description: `${resType} ${resName} 等 ${res.data} 个资源已经转出。请等待对方确认。可以在转移管理中查看状态或取消。`,
  77. duration: 0,
  78. });
  79. } else {
  80. onCreate();
  81. }
  82. } else {
  83. message.error(res.message, 10);
  84. }
  85. return true;
  86. }}
  87. >
  88. <Alert
  89. message={`将${resName} ${strTransfer}下面的用户。操作后需要等待对方确认后,资源才会被转移。可以在${strTransfer}查看和取消`}
  90. />
  91. <ProForm.Group>
  92. <UserSelect name="studio" multiple={false} />
  93. </ProForm.Group>
  94. </ModalForm>
  95. );
  96. };
  97. export default TransferCreateWidget;