/** * Copyright (c) OpenSpug Organization. https://github.com/openspug/spug * Copyright (c) * Released under the AGPL-3.0 License. */ import React from 'react'; import { observer } from 'mobx-react'; import { Modal, Form, Input, Tag, Upload, message, Button } from 'antd'; import hostStore from 'pages/host/store'; import http from 'libs/http'; import store from './store'; import lds from 'lodash'; @observer class Ext2Form extends React.Component { constructor(props) { super(props); this.token = localStorage.getItem('token'); this.state = { loading: false, uploading: false, fileList: [], host_ids: store.record['app_host_ids'].concat() } } componentDidMount() { if (hostStore.records.length === 0) { hostStore.fetchRecords() } const file = lds.get(store, 'record.extra.1'); if (file) { file.uid = '0'; this.setState({fileList: [file]}) } } handleSubmit = () => { if (this.state.host_ids.length === 0) { return message.error('请至少选择一个要发布的目标主机') } this.setState({loading: true}); const formData = this.props.form.getFieldsValue(); formData['id'] = store.record.id; formData['deploy_id'] = store.record.deploy_id; formData['extra'] = [formData['extra']]; if (this.state.fileList.length > 0) { formData['extra'].push(lds.pick(this.state.fileList[0], ['path', 'name'])) } formData['host_ids'] = this.state.host_ids; http.post('/api/deploy/request/', formData) .then(res => { message.success('操作成功'); store.ext2Visible = false; store.fetchRecords() }, () => this.setState({loading: false})) }; handleChange = (id, v) => { const host_ids = this.state.host_ids; const index = host_ids.indexOf(id); if (index === -1) { this.setState({host_ids: [id, ...host_ids]}) } else { host_ids.splice(index, 1); this.setState({host_ids}) } }; handleUploadChange = (v) => { if (v.fileList.length === 0) { this.setState({fileList: []}) } }; handleUpload = (file, fileList) => { this.setState({uploading: true}); const formData = new FormData(); formData.append('file', file); formData.append('deploy_id', store.record.deploy_id); http.post('/api/deploy/request/upload/', formData, {timeout: 120000}) .then(res => { file.path = res; this.setState({fileList: [file]}) }) .finally(() => this.setState({uploading: false})) return false }; render() { const info = store.record; const {host_ids, fileList, uploading} = this.state; const {getFieldDecorator} = this.props.form; return ( store.ext2Visible = false} confirmLoading={this.state.loading} onOk={this.handleSubmit}>
{getFieldDecorator('name', {initialValue: info['name']})( )} {getFieldDecorator('extra', {initialValue: lds.get(info, 'extra.0')})( )} {fileList.length === 0 ? : null} {info['app_host_ids'].map(id => ( this.handleChange(id, v)}> {lds.get(hostStore.idMap, `${id}.name`)}({lds.get(hostStore.idMap, `${id}.hostname`)}:{lds.get(hostStore.idMap, `${id}.port`)}) ))}
) } } export default Form.create()(Ext2Form)