From 88b235c10612c19bbf253f457ffda2faf2ab54a2 Mon Sep 17 00:00:00 2001 From: vapao Date: Wed, 17 Mar 2021 20:56:57 +0800 Subject: [PATCH] improve code --- spug_web/src/pages/deploy/request/Ext1Form.js | 41 +++++------ spug_web/src/pages/deploy/request/Ext2Form.js | 29 +++----- .../src/pages/deploy/request/HostSelector.js | 68 +++++++++++++++++++ 3 files changed, 97 insertions(+), 41 deletions(-) create mode 100644 spug_web/src/pages/deploy/request/HostSelector.js diff --git a/spug_web/src/pages/deploy/request/Ext1Form.js b/spug_web/src/pages/deploy/request/Ext1Form.js index 9e9d638..44d8678 100644 --- a/spug_web/src/pages/deploy/request/Ext1Form.js +++ b/spug_web/src/pages/deploy/request/Ext1Form.js @@ -5,14 +5,17 @@ */ import React, { useState, useEffect } from 'react'; import { observer } from 'mobx-react'; -import { Modal, Form, Input, Select, Tag, message } from 'antd'; +import { Modal, Form, Input, Select, message, Button } from 'antd'; +import HostSelector from './HostSelector'; import hostStore from 'pages/host/store'; import http from 'libs/http'; import store from './store'; import lds from 'lodash'; +import moment from 'moment'; export default observer(function () { const [form] = Form.useForm(); + const [visible, setVisible] = useState(false); const [loading, setLoading] = useState(false); const [versions, setVersions] = useState([]); const [host_ids, setHostIds] = useState([]); @@ -22,9 +25,7 @@ export default observer(function () { setHostIds(lds.clone(host_ids || app_host_ids)); http.get('/api/repository/', {params: {deploy_id}}) .then(res => setVersions(res)) - if (hostStore.records.length === 0) { - hostStore.fetchRecords() - } + if (hostStore.records.length === 0) hostStore.fetchRecords() }, []) function handleSubmit() { @@ -44,17 +45,6 @@ export default observer(function () { }, () => setLoading(false)) } - function handleChange(id) { - const index = host_ids.indexOf(id); - if (index === -1) { - setHostIds([id, ...host_ids]) - } else { - const tmp = lds.clone(host_ids); - tmp.splice(index, 1); - setHostIds(tmp) - } - } - return ( - {item.remarks ? `${item.version} (${item.remarks})` : item.version} +
+ {item.remarks ? `${item.version} (${item.remarks})` : item.version} + 构建于 {moment(item.created_at).fromNow()} +
))} + + {host_ids.length > 0 && `已选择 ${host_ids.length} 台`} + + - - {store.record.app_host_ids.map(id => ( - handleChange(id)}> - {lds.get(hostStore.idMap, `${id}.name`)}({lds.get(hostStore.idMap, `${id}.hostname`)}:{lds.get(hostStore.idMap, `${id}.port`)}) - - ))} - + {visible && setVisible(false)} + onOk={ids => setHostIds(ids)}/>}
) }) \ No newline at end of file diff --git a/spug_web/src/pages/deploy/request/Ext2Form.js b/spug_web/src/pages/deploy/request/Ext2Form.js index 4d54304..ef1c8e7 100644 --- a/spug_web/src/pages/deploy/request/Ext2Form.js +++ b/spug_web/src/pages/deploy/request/Ext2Form.js @@ -6,14 +6,16 @@ import React, { useState, useEffect } from 'react'; import { observer } from 'mobx-react'; import { UploadOutlined } from '@ant-design/icons'; -import { Modal, Form, Input, Tag, Upload, message, Button } from 'antd'; +import { Modal, Form, Input, Upload, message, Button } from 'antd'; import hostStore from 'pages/host/store'; import { http, X_TOKEN } from 'libs'; import store from './store'; import lds from 'lodash'; +import HostSelector from "./HostSelector"; export default observer(function () { const [form] = Form.useForm(); + const [visible, setVisible] = useState(false); const [loading, setLoading] = useState(false); const [uploading, setUploading] = useState(false); const [fileList, setFileList] = useState([]); @@ -51,17 +53,6 @@ export default observer(function () { }, () => setLoading(false)) } - function handleChange(id, v) { - const index = host_ids.indexOf(id); - if (index === -1) { - setHostIds([id, ...host_ids]) - } else { - const tmp = lds.clone(host_ids); - tmp.splice(index, 1); - setHostIds(tmp) - } - } - function handleUploadChange(v) { if (v.fileList.length === 0) { setFileList([]) @@ -108,14 +99,16 @@ export default observer(function () { {fileList.length === 0 ? : null} - - {store.record['app_host_ids'].map(id => ( - handleChange(id, v)}> - {lds.get(hostStore.idMap, `${id}.name`)}({lds.get(hostStore.idMap, `${id}.hostname`)}:{lds.get(hostStore.idMap, `${id}.port`)}) - - ))} + + {host_ids.length > 0 && `已选择 ${host_ids.length} 台`} + + {visible && setVisible(false)} + onOk={ids => setHostIds(ids)}/>} ) }) \ No newline at end of file diff --git a/spug_web/src/pages/deploy/request/HostSelector.js b/spug_web/src/pages/deploy/request/HostSelector.js new file mode 100644 index 0000000..2379b04 --- /dev/null +++ b/spug_web/src/pages/deploy/request/HostSelector.js @@ -0,0 +1,68 @@ +import React, { useState } from 'react'; +import { observer } from 'mobx-react'; +import { Modal, Table, Button, Alert } from 'antd'; +import hostStore from 'pages/host/store'; +import lds from 'lodash'; + +export default observer(function (props) { + const [selectedRowKeys, setSelectedRowKeys] = useState(props.host_ids || []); + + function handleClickRow(record) { + const index = selectedRowKeys.indexOf(record.id); + if (index !== -1) { + selectedRowKeys.splice(index, 1) + } else { + selectedRowKeys.push(record.id) + } + setSelectedRowKeys([...selectedRowKeys]) + } + + function handleSubmit() { + if (props.onOk) { + const res = props.onOk(selectedRowKeys); + if (res && res.then) { + res.then(props.onCancel) + } else { + props.onCancel(); + } + } + } + + return ( + + {selectedRowKeys.length > 0 && ( + setSelectedRowKeys([])}>取消选择}/> + )} + ({id}))} + onRow={record => { + return { + onClick: () => handleClickRow(record) + } + }} + rowSelection={{ + selectedRowKeys, + onSelect: handleClickRow, + onSelectAll: (_, __, changeRows) => changeRows.map(x => handleClickRow(x)) + }}> + lds.get(hostStore.idMap, `${id}.name`)}/> + lds.get(hostStore.idMap, `${id}.hostname`)}/> +
+
+ ) +}) \ No newline at end of file