SyncTaskAdd.jsx 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. import { Steps, Button, Space, Form, Drawer, message } from 'antd'
  2. import StepOne from './StepOne'
  3. import StepTwo from './StepTwo'
  4. import StepThree from './StepThree'
  5. import StepFour from './StepFour'
  6. import React, { useState, useEffect } from 'react'
  7. import DataTableStruct from './DataTableStruct'
  8. import styled from 'styled-components'
  9. import { MenuFoldOutlined, MenuUnfoldOutlined } from '@ant-design/icons'
  10. import { getDataSourceList, getTableSchema } from '../services'
  11. import { useForm } from 'antd/es/form/Form'
  12. const { Step } = Steps
  13. const SyncTask = styled.div`
  14. padding: 20px;
  15. .check__btn {
  16. position: absolute;
  17. right: 0;
  18. top: 20%;
  19. border-radius: 0;
  20. }
  21. `
  22. export default function SyncTaskAdd() {
  23. // 步骤三ref
  24. const stepThreeRef = React.createRef()
  25. // 步骤数
  26. const [currentStep, setCurrentStep] = useState(0)
  27. // 初始化数据源列表数据 constructor
  28. const [dataSourceList, setDataSourceList] = useState([])
  29. // 构建loading状态
  30. const [building, setBuilding] = useState(false)
  31. // 提交loading状态
  32. const [submiting, setSubmiting] = useState(false)
  33. // 完成状态
  34. const [isfinishBuild, setIsFinishBuild] = useState(false)
  35. // 提取源表单表结构
  36. const [drawDataStruct, setDrawDataStruct] = useState([])
  37. const updateDrawDataStruct = async (ds_id, table_name) => {await updateTableStruct(ds_id, table_name, setDrawDataStruct)}
  38. // 加载源表单表结构
  39. const [loadDataStruct, setLoadDataStruct] = useState([])
  40. const updateLoadDataStruct = async (id, table_name) => {await updateTableStruct(id, table_name, setLoadDataStruct)}
  41. // 更新表结构
  42. const updateTableStruct = async (id, table_name, setFunc) => {
  43. const { data } = await getTableSchema({id, table_name})
  44. if (data.code === 200) {
  45. const tableList = data.data.map(item => {
  46. const splitData = item.split(':')
  47. return {
  48. key: splitData[0],
  49. id: splitData[0],
  50. field: splitData[1],
  51. type: splitData[2]
  52. }
  53. })
  54. setFunc(tableList)
  55. } else {
  56. message.error('表结构数据加载失败')
  57. }
  58. }
  59. // 配置提取源表单
  60. const [drawDataForm] = Form.useForm()
  61. // 配置加载源表单
  62. const [loadDataForm] = Form.useForm()
  63. // 规则表单
  64. const [ruleForm] = Form.useForm()
  65. // 同步参数表单
  66. const [syncDataForm] = Form.useForm()
  67. // 当前表单
  68. const [currentForm, setCurrentForm] = useState(drawDataForm)
  69. // 上一步/下一步
  70. const changeStep = num => {
  71. if (num === -1) {
  72. setIsFinishBuild(false)
  73. }
  74. setCurrentStep(currentStep + num)
  75. }
  76. // 监听步骤设置当前表单
  77. useEffect(() => {
  78. switch (currentStep) {
  79. case 0:
  80. setCurrentForm(drawDataForm)
  81. break
  82. case 1:
  83. setCurrentForm(loadDataForm)
  84. break
  85. case 2:
  86. setCurrentForm(ruleForm)
  87. break;
  88. case 3:
  89. setCurrentForm(syncDataForm)
  90. break
  91. default:
  92. break
  93. }
  94. }, [currentStep])
  95. // 右侧表结构可视
  96. const [visible, setVisible] = useState(false)
  97. // 展示
  98. const showDrawer = () => {
  99. setVisible(true)
  100. }
  101. // 关闭
  102. const onClose = () => {
  103. setVisible(false)
  104. }
  105. // 获取列表数据
  106. const fetchDataSourceList = async () => {
  107. const { data } = await getDataSourceList()
  108. if (data.code === 200) {
  109. const list = data.data.items.map(item => {
  110. return {
  111. key: item.id,
  112. datasource_name: item.datasource_name,
  113. datasource: item.datasource,
  114. }
  115. })
  116. setDataSourceList(list)
  117. }
  118. }
  119. useEffect(() => {
  120. fetchDataSourceList()
  121. }, [])
  122. // 下一步
  123. const nextStep = () => {
  124. if (currentStep === 2) {
  125. console.log(stepThreeRef.current)
  126. }
  127. currentForm
  128. .validateFields()
  129. .then(() => {
  130. changeStep(1)
  131. })
  132. .catch(err => {
  133. message.error('请检查表单数据是否完整')
  134. })
  135. }
  136. // 完成构建
  137. const finishBuild = async () => {
  138. setBuilding(true)
  139. setTimeout(() => {
  140. message.success('构建成功')
  141. setBuilding(false)
  142. setIsFinishBuild(true)
  143. }, 2000)
  144. }
  145. // 构建表单
  146. const build = () => {
  147. finishBuild()
  148. }
  149. // 完成提交
  150. const finishSubmit = async () => {
  151. setSubmiting(true)
  152. setTimeout(() => {
  153. message.success('提交成功')
  154. setSubmiting(false)
  155. }, 2000)
  156. }
  157. // 提交表单
  158. const submit = () => {
  159. currentForm
  160. .validateFields()
  161. .then(() => {
  162. console.log('submit')
  163. finishSubmit()
  164. })
  165. .catch(err => {
  166. message.error('请检查表单数据是否完整')
  167. })
  168. }
  169. return (
  170. <SyncTask>
  171. {/* 步骤条 */}
  172. <Steps current={currentStep} labelPlacement="vertical" size="small">
  173. <Step title="步骤1: 配置提取源" />
  174. <Step title="步骤2: 配置加载源" />
  175. <Step title="步骤3: 配置转换规则" />
  176. <Step title="步骤4: 设置同步参数" />
  177. </Steps>
  178. {/* 表单项 */}
  179. {/* 配置提取源 */}
  180. {currentStep === 0 && <StepOne drawDataForm={drawDataForm} dataSourceList={dataSourceList} updateTableStruct={updateDrawDataStruct}/>}
  181. {/* 配置加载源 */}
  182. {currentStep === 1 && <StepTwo loadDataForm={loadDataForm} dataSourceList={dataSourceList} updateTableStruct={updateLoadDataStruct}/>}
  183. {/* 配置转换规则 */}
  184. {currentStep === 2 &&
  185. <StepThree
  186. onRef={stepThreeRef}
  187. drawDataForm={drawDataForm}
  188. loadDataForm={loadDataForm}
  189. ruleForm={ruleForm}
  190. />
  191. }
  192. {/* 设置同步参数 */}
  193. {currentStep === 3 && <StepFour syncDataForm={syncDataForm} />}
  194. {/* 按扭操作 */}
  195. {/* 表结构预览 */}
  196. {(currentStep === 0 || currentStep === 1 ) &&
  197. <>
  198. <Button
  199. type="primary"
  200. onClick={showDrawer}
  201. className="check__btn"
  202. size="large"
  203. icon={<MenuFoldOutlined />}>
  204. 表结构预览
  205. </Button>
  206. <Drawer
  207. title="表结构预览"
  208. placement="right"
  209. onClose={onClose}
  210. visible={visible}
  211. width={600}
  212. mask={false}
  213. destroyOnClose={true}
  214. closeIcon={<MenuUnfoldOutlined style={{ color: '#1881DA' }} />}
  215. >
  216. <DataTableStruct formData={currentForm} tableData={currentStep === 0 ? drawDataStruct : loadDataStruct}/>
  217. </Drawer>
  218. </>
  219. }
  220. {/* 按扭操作 */}
  221. <Space style={{ margin: '20px' }}>
  222. <Button
  223. onClick={() => {
  224. changeStep(-1)
  225. }}
  226. disabled={currentStep === 0 || building}
  227. >
  228. 上一步
  229. </Button>
  230. {currentStep === 3 ? (
  231. <Button
  232. type="primary"
  233. onClick={isfinishBuild ? submit : build}
  234. loading={isfinishBuild ? submiting : building}>
  235. {isfinishBuild ? '提交' : '构建'}
  236. </Button>
  237. ) : (
  238. <Button type="primary" onClick={nextStep}>
  239. 下一步
  240. </Button>
  241. )}
  242. </Space>
  243. </SyncTask>
  244. )
  245. }