UploadCsvModal.tsx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * <<
  3. * Davinci
  4. * ==
  5. * Copyright (C) 2016 - 2017 EDP
  6. * ==
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. * >>
  19. */
  20. import React, { useCallback, useState } from 'react'
  21. import {
  22. Modal,
  23. Form,
  24. Input,
  25. Radio,
  26. Popover,
  27. Upload,
  28. Icon,
  29. Button,
  30. message
  31. } from 'antd'
  32. const FormItem = Form.Item
  33. const RadioGroup = Radio.Group
  34. import { FormItemProps, FormComponentProps } from 'antd/lib/form'
  35. import { UploadProps } from 'antd/lib/upload'
  36. import { ICSVMetaInfo } from '../types'
  37. const formItemLayout: Partial<FormItemProps> = {
  38. labelCol: { span: 6 },
  39. wrapperCol: { span: 16 }
  40. }
  41. interface IUploadCsvModalProps extends FormComponentProps<ICSVMetaInfo> {
  42. visible: boolean
  43. sourceId: number
  44. uploading: boolean
  45. onValidate: (
  46. values: Pick<ICSVMetaInfo, 'sourceId' | 'tableName' | 'mode'>,
  47. callback: (errMsg?: string) => void
  48. ) => void
  49. onOk: (value: ICSVMetaInfo) => void
  50. onCancel: () => void
  51. }
  52. const UploadCsvModal: React.FC<IUploadCsvModalProps> = (props) => {
  53. const { visible, sourceId, uploading, form, onValidate, onOk, onCancel } = props
  54. const { getFieldDecorator } = form
  55. const [files, setFiles] = useState([])
  56. const preventUpload: UploadProps['beforeUpload'] = useCallback((file) => {
  57. setFiles([file])
  58. return false
  59. }, [])
  60. const save = useCallback(() => {
  61. if (!files.length) {
  62. message.error('请上传 csv 文件!')
  63. return
  64. }
  65. form.validateFields((err, values) => {
  66. if (err) {
  67. return
  68. }
  69. values.file = files[0]
  70. values.sourceId = sourceId
  71. onOk(values)
  72. })
  73. }, [sourceId, files, onOk])
  74. const validateTableName = useCallback(
  75. (_, tableName: string, callback) => {
  76. onValidate(
  77. {
  78. sourceId,
  79. tableName,
  80. mode: form.getFieldValue('mode')
  81. },
  82. callback
  83. )
  84. },
  85. [sourceId, onValidate]
  86. )
  87. const resetFields = useCallback(() => {
  88. form.resetFields()
  89. setFiles([])
  90. }, [])
  91. return (
  92. <Modal
  93. title="上传 CSV 文件"
  94. visible={visible}
  95. maskClosable={false}
  96. afterClose={resetFields}
  97. confirmLoading={uploading}
  98. onOk={save}
  99. onCancel={onCancel}
  100. >
  101. <Form>
  102. <FormItem label="表名" {...formItemLayout}>
  103. {getFieldDecorator<ICSVMetaInfo>('tableName', {
  104. rules: [
  105. {
  106. required: true,
  107. message: '表名不能为空'
  108. },
  109. {
  110. validator: validateTableName
  111. }
  112. ],
  113. validateFirst: true,
  114. validateTrigger: ''
  115. })(<Input />)}
  116. </FormItem>
  117. <FormItem label="主键" {...formItemLayout}>
  118. {getFieldDecorator<ICSVMetaInfo>('primaryKeys', {})(<Input />)}
  119. </FormItem>
  120. <FormItem label="索引键" {...formItemLayout}>
  121. {getFieldDecorator<ICSVMetaInfo>('indexKeys', {})(<Input />)}
  122. </FormItem>
  123. <FormItem label="导入方式" {...formItemLayout}>
  124. {getFieldDecorator<ICSVMetaInfo>('mode', {
  125. initialValue: 0
  126. })(
  127. <RadioGroup>
  128. <Radio value={0}>新增</Radio>
  129. <Radio value={1}>替换</Radio>
  130. <Radio value={2}>追加</Radio>
  131. <Radio value={3}>覆盖</Radio>
  132. </RadioGroup>
  133. )}
  134. <Popover
  135. placement="right"
  136. content={
  137. <>
  138. <p>新增:首次上传文件到新表</p>
  139. <p>替换:保持原有表结构不变,清空原有表数据后上传</p>
  140. <p>追加:保持原有表结构不变,保持原有表数据并追加</p>
  141. <p>覆盖:重建表结构并替换数据</p>
  142. </>
  143. }
  144. >
  145. <Icon type="question-circle-o" />
  146. </Popover>
  147. </FormItem>
  148. <FormItem label="上传" {...formItemLayout}>
  149. <Upload
  150. accept=".csv"
  151. multiple={false}
  152. fileList={files}
  153. beforeUpload={preventUpload}
  154. >
  155. <Button>
  156. <Icon type="upload" />
  157. Click to Upload CSV
  158. </Button>
  159. </Upload>
  160. </FormItem>
  161. </Form>
  162. </Modal>
  163. )
  164. }
  165. export default Form.create<IUploadCsvModalProps>()(UploadCsvModal)