import-flow-modal.tsx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import { apiInterceptors, importFlow } from '@/client/api';
  2. import CanvasWrapper from '@/pages/construct/flow/canvas/index';
  3. import { UploadOutlined } from '@ant-design/icons';
  4. import { Button, Form, GetProp, Modal, Radio, Upload, UploadFile, UploadProps, message } from 'antd';
  5. import { useEffect, useState } from 'react';
  6. import { useTranslation } from 'react-i18next';
  7. import { Edge, Node } from 'reactflow';
  8. type Props = {
  9. isImportModalOpen: boolean;
  10. setNodes: React.Dispatch<React.SetStateAction<Node<any, string | undefined>[]>>;
  11. setEdges: React.Dispatch<React.SetStateAction<Edge<any>[]>>;
  12. setIsImportFlowModalOpen: (value: boolean) => void;
  13. };
  14. type FileType = Parameters<GetProp<UploadProps, 'beforeUpload'>>[0];
  15. export const ImportFlowModal: React.FC<Props> = ({ isImportModalOpen, setIsImportFlowModalOpen }) => {
  16. const { t } = useTranslation();
  17. const [form] = Form.useForm();
  18. const [messageApi, contextHolder] = message.useMessage();
  19. const [fileList, setFileList] = useState<UploadFile[]>([]);
  20. useEffect(() => {
  21. if (isImportModalOpen) {
  22. form.resetFields();
  23. setFileList([]);
  24. }
  25. }, [isImportModalOpen, form]);
  26. const onFlowImport = async (values: any) => {
  27. values.file = values.file?.[0];
  28. const formData: any = new FormData();
  29. fileList.forEach(file => {
  30. formData.append('file', file as FileType);
  31. formData.append('save_flow', values.save_flow);
  32. });
  33. const [, , res] = await apiInterceptors(importFlow(formData));
  34. if (res?.success) {
  35. messageApi.success(t('Import_Flow_Success'));
  36. localStorage.setItem('importFlowData', JSON.stringify(res?.data));
  37. CanvasWrapper();
  38. } else if (res?.err_msg) {
  39. messageApi.error(res?.err_msg);
  40. }
  41. setIsImportFlowModalOpen(false);
  42. };
  43. const props: UploadProps = {
  44. onRemove: (file: any) => {
  45. const index = fileList.indexOf(file);
  46. const newFileList = fileList.slice();
  47. newFileList.splice(index, 1);
  48. setFileList(newFileList);
  49. },
  50. beforeUpload: (file: any) => {
  51. setFileList([...fileList, file]);
  52. return false;
  53. },
  54. fileList,
  55. };
  56. return (
  57. <>
  58. <Modal
  59. title={t('Import_Flow')}
  60. open={isImportModalOpen}
  61. onCancel={() => setIsImportFlowModalOpen(false)}
  62. footer={[
  63. <Button key='cancel' onClick={() => setIsImportFlowModalOpen(false)}>
  64. {t('cancel')}
  65. </Button>,
  66. <Button key='submit' type='primary' onClick={() => form.submit()}>
  67. {t('verify')}
  68. </Button>,
  69. ]}
  70. >
  71. <Form
  72. form={form}
  73. className='mt-6'
  74. labelCol={{ span: 6 }}
  75. wrapperCol={{ span: 16 }}
  76. onFinish={onFlowImport}
  77. initialValues={{
  78. save_flow: false,
  79. }}
  80. >
  81. <Form.Item
  82. name='file'
  83. label={t('Select_File')}
  84. valuePropName='fileList'
  85. getValueFromEvent={e => (Array.isArray(e) ? e : e && e.fileList)}
  86. rules={[{ required: true, message: 'Please upload a file' }]}
  87. >
  88. <Upload {...props} accept='.json,.zip' maxCount={1}>
  89. <Button icon={<UploadOutlined />}> {t('Upload')}</Button>
  90. </Upload>
  91. </Form.Item>
  92. <Form.Item name='save_flow' label={t('Save_After_Import')} hidden>
  93. <Radio.Group>
  94. <Radio value={true}>{t('Yes')}</Radio>
  95. <Radio value={false}>{t('No')}</Radio>
  96. </Radio.Group>
  97. </Form.Item>
  98. </Form>
  99. </Modal>
  100. {contextHolder}
  101. </>
  102. );
  103. };