import React from 'react'; import { observer } from 'mobx-react'; import { Modal, Form, Input, Select, Col, Button, Tag, Icon, message } from 'antd'; import hostStore from 'pages/host/store'; import http from 'libs/http'; import store from './store'; import lds from 'lodash'; @observer class Ext1Form extends React.Component { constructor(props) { super(props); this.isReady = false; this.state = { loading: false, fetching: true, git_type: lds.get(store.record, 'extra.0', 'branch'), extra1: lds.get(store.record, 'extra.1'), extra2: lds.get(store.record, 'extra.2'), versions: {}, host_ids: store.record['app_host_ids'].concat() } } componentDidMount() { this.fetchVersions(); if (hostStore.records.length === 0) { hostStore.fetchRecords() } } fetchVersions = () => { this.setState({fetching: true}); http.get(`/api/app/deploy/${store.record.deploy_id}/versions/`) .then(res => { this.setState({versions: res}, this._initExtra1); }) .finally(() => this.setState({fetching: false})) }; _initExtra1 = () => { if (this.isReady === true || this.state.extra1 === undefined) { const {git_type, versions: {branches, tags}} = this.state; let [extra1, extra2] = [undefined, undefined]; if (git_type === 'branch') { if (branches) { extra1 = lds.get(Object.keys(branches), 0); extra2 = lds.get(branches[extra1], '0.id') } } else { if (tags) { extra1 = lds.get(Object.keys(tags), 0) } } this.setState({extra1, extra2}) } else { this.isReady = true } }; switchType = (v) => { this.setState({git_type: v, extra1: null}, this._initExtra1) }; switchExtra1 = (v) => { let {git_type, extra2, versions: {branches}} = this.state; if (git_type === 'branch') { extra2 = lds.get(branches[v], '0.id') } this.setState({extra1: v, extra2}) }; handleSubmit = () => { if (this.state.host_ids.length === 0) { return message.error('请至少选择一个要发布的目标主机') } this.setState({loading: true}); const {git_type, extra1, extra2} = this.state; const formData = this.props.form.getFieldsValue(); formData['id'] = store.record.id; formData['deploy_id'] = store.record.deploy_id; formData['host_ids'] = this.state.host_ids; formData['extra'] = [git_type, extra1, extra2]; http.post('/api/deploy/request/', formData) .then(res => { message.success('操作成功'); store.ext1Visible = false; store.fetchRecords() }, () => this.setState({loading: false})) }; handleChange = (id) => { 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}) } }; render() { const info = store.record; const {host_ids, git_type, extra1, extra2, fetching, versions: {branches, tags}} = this.state; const {getFieldDecorator} = this.props.form; return ( store.ext1Visible = false} confirmLoading={this.state.loading} onOk={this.handleSubmit}>
{getFieldDecorator('name', {initialValue: info['name']})( )} {fetching ? : } {git_type === 'branch' && ( )} {getFieldDecorator('desc', {initialValue: info['desc']})( )} {info['app_host_ids'].map(id => ( this.handleChange(id)}> {lds.get(hostStore.idMap, `${id}.name`)}({lds.get(hostStore.idMap, `${id}.hostname`)}:{lds.get(hostStore.idMap, `${id}.port`)}) ))}
) } } export default Form.create()(Ext1Form)