|
1 | | -import React from 'react'; |
| 1 | +import React, { useEffect, useState } from 'react'; |
2 | 2 | import { Modal, Upload, Button, message } from 'antd'; |
3 | 3 | import { UploadOutlined } from '@ant-design/icons'; |
4 | 4 |
|
5 | | -interface FileUploadModalProps { |
| 5 | +const uploadFileType = { |
| 6 | + ncx: { |
| 7 | + accept: '.ncx', |
| 8 | + uploadUrl: '/api/converter/ncx/upload', |
| 9 | + }, |
| 10 | + dbp: { |
| 11 | + accept: '.dbp', |
| 12 | + uploadUrl: '/api/converter/dbp/upload', |
| 13 | + }, |
| 14 | +}; |
| 15 | +interface IImportConnectionProps { |
6 | 16 | open: boolean; |
7 | 17 | onClose: () => void; |
8 | | - onUploaded?: (file: File) => void; |
9 | 18 | onConfirm?: () => void; |
10 | 19 | } |
11 | 20 |
|
12 | | -const FileUploadModal: React.FC<FileUploadModalProps> = ({ open, onClose, onUploaded, onConfirm }) => { |
13 | | - const beforeUpload = (file: File) => { |
14 | | - const fileName = file.name.toLowerCase(); |
15 | | - if (fileName.includes('navicat')) { |
16 | | - message.success('Navicat file detected'); |
17 | | - onUploaded && onUploaded(file); |
18 | | - } else if (fileName.includes('dbeaver')) { |
19 | | - message.success('DBeaver file detected'); |
20 | | - onUploaded && onUploaded(file); |
21 | | - } else if (fileName.includes('datagrip')) { |
22 | | - message.success('DataGrip file detected'); |
23 | | - onUploaded && onUploaded(file); |
24 | | - } else { |
25 | | - message.error('File type not recognized'); |
26 | | - return false; |
| 21 | +const ImportConnection: React.FC<IImportConnectionProps> = ({ open, onClose, onConfirm }) => { |
| 22 | + const [selectedFile, setSelectedFile] = useState<File | null>(null); |
| 23 | + const [uploading, setUploading] = useState(false); |
| 24 | + |
| 25 | + useEffect(() => { |
| 26 | + if (open) { |
| 27 | + setSelectedFile(null); |
27 | 28 | } |
28 | | - return false; // Prevent auto-upload for the sake of this demo |
| 29 | + }, [open]); |
| 30 | + |
| 31 | + const handleBeforeUpload = (file: File) => { |
| 32 | + setSelectedFile(file); |
| 33 | + return false; // 停止自动上传 |
29 | 34 | }; |
30 | 35 |
|
31 | | - const renderSelect = () => { |
32 | | - // 下拉框 选择Navciat、dbever、datagrip |
| 36 | + const handleConfirmUpload = async () => { |
| 37 | + if (!selectedFile) return; |
| 38 | + |
| 39 | + const formData = new FormData(); |
| 40 | + formData.append('file', selectedFile); |
| 41 | + |
| 42 | + const fileExtension = selectedFile.name.split('.').pop() || ''; |
| 43 | + console.log('fileExtension', fileExtension); |
| 44 | + |
| 45 | + const { uploadUrl } = uploadFileType[fileExtension] || {}; |
| 46 | + |
| 47 | + try { |
| 48 | + setUploading(true); |
| 49 | + |
| 50 | + const response = await fetch(uploadUrl, { |
| 51 | + method: 'POST', |
| 52 | + body: formData, |
| 53 | + }); |
| 54 | + if (response.ok) { |
| 55 | + message.success(`${selectedFile.name} 导入数据源成功`); |
| 56 | + onConfirm && onConfirm(); |
| 57 | + } else { |
| 58 | + message.error(`${selectedFile.name} 导入数据源是吧`); |
| 59 | + } |
| 60 | + } catch (error) { |
| 61 | + message.error(`Error: ${error}`); |
| 62 | + } finally { |
| 63 | + setUploading(false); |
| 64 | + } |
33 | 65 | }; |
| 66 | + |
34 | 67 | return ( |
35 | 68 | <Modal |
36 | | - title="Upload your file" |
| 69 | + title="导入文件, .ncx(navicat) 或 .dbp(dbever)" |
37 | 70 | open={open} |
38 | 71 | onCancel={onClose} |
39 | | - onOk={onConfirm} |
40 | | - footer={[ |
41 | | - <Button key="back" onClick={onClose}> |
42 | | - Cancel |
43 | | - </Button>, |
44 | | - <Button key="submit" type="primary" onClick={onConfirm}> |
45 | | - Confirm |
46 | | - </Button>, |
47 | | - ]} |
| 72 | + onOk={handleConfirmUpload} |
| 73 | + confirmLoading={uploading} |
48 | 74 | > |
49 | | - <Upload |
50 | | - action="/api/converter/ncx/upload" |
51 | | - name="file" |
52 | | - accept=".ncx" |
53 | | - beforeUpload={beforeUpload} |
54 | | - onChange={(info) => { |
55 | | - console.log(info); |
56 | | - }} |
57 | | - > |
| 75 | + <Upload name="file" accept=".ncx,.dbp" beforeUpload={handleBeforeUpload} maxCount={1}> |
58 | 76 | <Button icon={<UploadOutlined />}>Select File</Button> |
59 | 77 | </Upload> |
60 | 78 | </Modal> |
61 | 79 | ); |
62 | 80 | }; |
63 | 81 |
|
64 | | -export default FileUploadModal; |
| 82 | +export default ImportConnection; |
0 commit comments