|
| 1 | +import React, { memo, useState, useEffect } from 'react'; |
| 2 | +import styles from './index.less'; |
| 3 | +import classnames from 'classnames'; |
| 4 | +import { i18n, isEn } from '@/i18n'; |
| 5 | +import { Form, Modal, Input, Select, Spin } from 'antd'; |
| 6 | +import connectionService, { IDriverResponse } from '@/service/connection'; |
| 7 | +import { DatabaseTypeCode, ConnectionEnvType, databaseMap } from '@/constants'; |
| 8 | +import UploadDriver from '@/components/UploadDriver'; |
| 9 | +import LoadingGracile from '@/components/Loading/LoadingGracile'; |
| 10 | +const { Option } = Select; |
| 11 | + |
| 12 | +interface IProps { |
| 13 | + className?: string; |
| 14 | + onChange: (data: any) => void; |
| 15 | + backfillData: any; |
| 16 | +} |
| 17 | + |
| 18 | +enum DownloadStatus { |
| 19 | + Default, |
| 20 | + Loading, |
| 21 | + Error, |
| 22 | + Success |
| 23 | +} |
| 24 | + |
| 25 | +export default memo<IProps>(function Driver(props) { |
| 26 | + const { className, backfillData, onChange } = props; |
| 27 | + const [downloadStatus, setDownloadStatus] = useState<DownloadStatus>(); |
| 28 | + const [driveForm] = Form.useForm(); |
| 29 | + const [driverObj, setDriverObj] = useState<IDriverResponse>(); |
| 30 | + const [uploadDriverModal, setUploadDriverModal] = useState(false); |
| 31 | + const [driverSaved, setDriverSaved] = useState<any>({}); |
| 32 | + |
| 33 | + useEffect(() => { |
| 34 | + if (backfillData) { |
| 35 | + getDriverList(); |
| 36 | + } |
| 37 | + }, [backfillData?.type]) |
| 38 | + |
| 39 | + useEffect(() => { |
| 40 | + if (backfillData) { |
| 41 | + driveForm.setFieldsValue({ |
| 42 | + jdbcDriverClass: backfillData?.driverConfig?.jdbcDriverClass, |
| 43 | + jdbcDriver: backfillData?.driverConfig?.jdbcDriver |
| 44 | + }) |
| 45 | + } |
| 46 | + }, [backfillData?.driverConfig, backfillData?.id]) |
| 47 | + |
| 48 | + function getDriverList() { |
| 49 | + connectionService.getDriverList({ dbType: backfillData.type }).then(res => { |
| 50 | + setDriverObj(res) |
| 51 | + }) |
| 52 | + } |
| 53 | + |
| 54 | + function formChange(data: any) { |
| 55 | + setDriverSaved(data); |
| 56 | + } |
| 57 | + |
| 58 | + function saveDriver() { |
| 59 | + connectionService.saveDriver(driverSaved).then(res => { |
| 60 | + setUploadDriverModal(false); |
| 61 | + getDriverList(); |
| 62 | + }) |
| 63 | + } |
| 64 | + |
| 65 | + function downloadDrive() { |
| 66 | + setDownloadStatus(DownloadStatus.Loading) |
| 67 | + connectionService.downloadDriver({ dbType: backfillData.type }).then(res => { |
| 68 | + // setDownloadStatus(DownloadStatus.Success) |
| 69 | + getDriverList(); |
| 70 | + }).catch(() => { |
| 71 | + setDownloadStatus(DownloadStatus.Error) |
| 72 | + }) |
| 73 | + } |
| 74 | + |
| 75 | + function onValuesChange(data: any) { |
| 76 | + const selected = driverObj?.driverConfigList.find(t => t.jdbcDriver === data.jdbcDriver); |
| 77 | + driveForm.setFieldsValue({ |
| 78 | + jdbcDriverClass: selected?.jdbcDriverClass |
| 79 | + }); |
| 80 | + onChange({ |
| 81 | + jdbcDriverClass: selected?.jdbcDriverClass, |
| 82 | + jdbcDriver: data.jdbcDriver |
| 83 | + }) |
| 84 | + } |
| 85 | + |
| 86 | + return <div className={classnames(styles.box, className)}> |
| 87 | + <Form |
| 88 | + form={driveForm} |
| 89 | + onValuesChange={onValuesChange} |
| 90 | + colon={false} |
| 91 | + > |
| 92 | + <Form.Item labelAlign="left" name="jdbcDriver" label={i18n('connection.title.driver')}> |
| 93 | + <Select> |
| 94 | + {driverObj?.driverConfigList?.map((t) => ( |
| 95 | + <Option key={t.jdbcDriver} value={t.jdbcDriver}> |
| 96 | + {t.jdbcDriver} |
| 97 | + </Option> |
| 98 | + ))} |
| 99 | + </Select> |
| 100 | + </Form.Item> |
| 101 | + <Form.Item labelAlign="left" name="jdbcDriverClass" label="Class"> |
| 102 | + <Input disabled /> |
| 103 | + </Form.Item> |
| 104 | + </Form> |
| 105 | + <div className={styles.downloadDriveFooter}> |
| 106 | + { |
| 107 | + (!driverObj?.driverConfigList?.length || downloadStatus === DownloadStatus.Success) ? <div onClick={downloadDrive} className={styles.downloadDrive}> |
| 108 | + { |
| 109 | + (downloadStatus === DownloadStatus.Default) && <div className={classnames(styles.downloadText, styles.downloadTextDownload)}>{i18n('connection.text.downloadDriver')}</div> |
| 110 | + } |
| 111 | + { |
| 112 | + (downloadStatus === DownloadStatus.Loading) && |
| 113 | + <div className={classnames(styles.downloadText, styles.downloadTextLoading)}> |
| 114 | + <LoadingGracile></LoadingGracile> |
| 115 | + <div className={styles.text}> |
| 116 | + {i18n('connection.text.downloading')} |
| 117 | + </div> |
| 118 | + </div> |
| 119 | + } |
| 120 | + { |
| 121 | + (downloadStatus === DownloadStatus.Error) && <div className={classnames(styles.downloadText, styles.downloadTextError)}>{i18n('connection.text.tryAgainDownload')}</div> |
| 122 | + } |
| 123 | + { |
| 124 | + (downloadStatus === DownloadStatus.Success) && <div className={classnames(styles.downloadText, styles.downloadTextSuccess)}>{i18n('connection.text.downloadSuccess')}</div> |
| 125 | + } |
| 126 | + |
| 127 | + </div> : <div></div> |
| 128 | + } |
| 129 | + |
| 130 | + <div |
| 131 | + className={styles.uploadCustomDrive} |
| 132 | + onClick={() => { setUploadDriverModal(true) }} |
| 133 | + > |
| 134 | + {i18n('connection.tips.customUpload')} |
| 135 | + </div> |
| 136 | + </div> |
| 137 | + <Modal |
| 138 | + destroyOnClose={true} |
| 139 | + title={i18n('connection.title.uploadDriver')} |
| 140 | + open={uploadDriverModal} |
| 141 | + onOk={() => { saveDriver() }} |
| 142 | + onCancel={() => { setUploadDriverModal(false) }} |
| 143 | + > |
| 144 | + <UploadDriver |
| 145 | + jdbcDriverClass={driverObj?.defaultDriverConfig?.jdbcDriverClass} |
| 146 | + formChange={formChange} |
| 147 | + databaseType={backfillData.type} |
| 148 | + ></UploadDriver> |
| 149 | + </Modal> |
| 150 | + </div> |
| 151 | +}) |
0 commit comments