import React, {Component} from 'react';
import {FormattedMessage} from 'react-intl';
import {Input, Radio, Collapse, Button, Icon, Tooltip} from 'antd';
import {idGen} from "../../../../../func";
import {graphqlUrl, manageUsers} from "../../../../../config";
import {
ADD_DEPLOY,
UPDATE_DEPLOY,
UPDATE_PROJECT_DEPLOY_AND_CLOUD,
UPDATE_PROJECT_ONLY_STATUS
} from "../../../../../gql";
import {request} from 'graphql-request'
import {removePrefix, shiftPrefix} from "../../../../../func";
const Panel = Collapse.Panel;
// 如需改变显示,请在此处 value 处更改
// 如需添加中文,请在此处 value 处添加
// eg: 'xxxx':
const valueToKey = {
'functionName': 'functionName',
'cosBucketName': 'cosBucketName',
'cosObjectName': 'cosObjectName',
'cosBucketRegion': 'cosBucketRegion',
'serviceName': 'serviceName',
'subnetId': 'subnetId',
'vpcId': 'vpcId',
'region': 'region',
'description': 'description',
};
const toolTipTitle = {
'functionName': 'its funcname',
'cosBucketName': 'its cosBucketName',
'cosObjectName': 'its cosObjectName',
'cosBucketRegion': 'its cosBucketRegion',
'serviceName': 'its serviceName',
'subnetId': 'its subnetId',
'vpcId': 'its vpcId',
'region': 'its region',
'description': 'its description',
};
const youMustFill = {
'functionName': true,
'cosBucketName': true,
'cosObjectName': true,
'cosBucketRegion': true,
'serviceName': true,
'subnetId': false,
'vpcId': false,
'region': true,
'description': false,
};
class DeployCardRender extends Component {
constructor(props) {
super(props);
if(props.deploy !== '' && props.deploy !== null) {
let {functionName, cosBucketName, cosObjectName, cosBucketRegion, serviceName, vpcId, subnetId, region, description} = props.deploy;
this.state = {
configs: ['description', 'vpcId', 'subnetId'],
description,
showOK: false,
functionName,
region,
cosBucketName,
cosObjectName,
cosBucketRegion,
serviceName,
vpcId,
subnetId
}
} else {
this.state = {
configs: ['description', 'vpcId', 'subnetId'],
description: '',
showOK: false,
functionName: props.defalutName,
region: props.region === '' ? 'ap-beijing' : props.region,
cosBucketName: 'graphqlfc',
cosObjectName: props.defalutName,
cosBucketRegion: props.region === '' ? 'ap-beijing' : props.region,
serviceName: '',
vpcId: '',
subnetId: '',
};
}
}
componentWillReceiveProps(next) {
if (next.deploy !== '' && next.deploy !== null) {
let {functionName, cosBucketName, cosObjectName, cosBucketRegion, serviceName, vpcId, subnetId, region, description} = next.deploy;
this.setState({
description,
functionName,
region: next.region === '' ? region : next.region,
cosBucketName,
cosObjectName,
cosBucketRegion: next.region === '' ? cosBucketRegion : next.region,
serviceName,
vpcId,
subnetId
})
} else {
this.setState({
functionName: next.defalutName,
region: next.region === '' ? 'ap-beijing' : next.region,
cosBucketName: 'graphqlfc',
cosObjectName: next.defalutName,
cosBucketRegion: next.region === '' ? 'ap-beijing' : next.region,
serviceName: '',
vpcId: '',
subnetId: ''
});
}
};
switchConfig = (label) => {
return (e) => {
this.setState({
[label]: e.target.value
})
};
};
ok = (id, userID, projectID, cloudID, deploy) => {
let {description, cosBucketName, subnetId, cosObjectName, region, vpcId, cosBucketRegion, functionName} = this.state;
region = shiftPrefix('ap-', region);
cosBucketRegion = shiftPrefix('ap-', cosBucketRegion);
let varObj = {
id,
cloud_id: cloudID,
user_id: userID,
description,
cosBucketName,
subnetId,
cosObjectName,
region,
vpcId,
cosBucketRegion,
functionName,
memorySize: 512,
timeout: 300,
handler: 'tencent_graphql.Bridge::handler',
serviceName: "",
fc_id: '',
createdAt: new Date().getTime(),
updatedAt: ''
};
if (deploy === '') {
request(graphqlUrl, ADD_DEPLOY, varObj).then(
data => {
if (data.create_deploy !== null) {
this.setState({
showOK: true
});
// 写回 project
request(graphqlUrl, UPDATE_PROJECT_DEPLOY_AND_CLOUD, {
id: projectID,
updatedAt: new Date().getTime(),
deploy_id: id,
cloud_id: cloudID,
projectStatus: 'functioned'
}).then(data => {
if(data.update_project !== null)
this.props.stepByStep(1);
})
}
setTimeout(() => {
this.setState({
showOK: false
})
}, 1500)
}
)
} else {
let {description, cosBucketName, subnetId, cosObjectName, region, vpcId, cosBucketRegion, functionName} = this.state;
region = shiftPrefix('ap-', region);
cosBucketRegion = shiftPrefix('ap-', cosBucketRegion);
let varObj = {
id: deploy.id,
description,
subnetId,
cosBucketName,
cosObjectName,
region,
vpcId,
cosBucketRegion,
functionName,
updatedAt: new Date().getTime()
};
request(graphqlUrl, UPDATE_DEPLOY, varObj).then(
data => {
if (data.update_deploy !== null) {
this.setState({
showOK: true
});
request(graphqlUrl, UPDATE_PROJECT_ONLY_STATUS, {
id: projectID,
updatedAt: new Date().getTime(),
projectStatus: 'updated'
}).then(data => {
if(data.update_project !== null)
this.props.stepByStep(1);
})
}
setTimeout(() => {
this.setState({
showOK: false
})
}, 1500)
}
)
}
};
render() {
const customPanelStyle = {
background: '#f7f7f7',
borderRadius: 4,
marginBottom: 24,
border: 0,
overflow: 'hidden',
};
const ioobotCloudID = ['tencent_CloudID', 'aliyun_CloudID'];
const disable = {
'subnetId': ioobotCloudID.includes(this.props.cloudID) ? true : '',
'vpcId': ioobotCloudID.includes(this.props.cloudID) ? true : '',
};
return (
{
youMustFill['functionName']?
*
:
''
}
{
youMustFill['region']?
*
:
''
}
{
this.props.switchRegion(e);
}} defaultValue={this.state.region} value={removePrefix('ap-', this.state.region)}
buttonStyle="solid">
{
youMustFill['cosBucketRegion']?
*
:
''
}
{
// 如果不想一变所有都变,就将 onchange 改成
// onChange={this.switchConfig('cosBucketRegion')}
this.props.switchRegion(e);
}} defaultValue={this.state.region}
value={removePrefix('ap-', this.state.cosBucketRegion)} buttonStyle="solid">
{
youMustFill['cosBucketName']?
*
:
''
}
{
youMustFill['cosObjectName']?
*
:
''
}
} style={customPanelStyle}>
{/*腾讯云为空字符串,不显示,这里未作区分*/}
{/**/}
{/*{valueToKey['serviceName']}*/}
{/* */}
{/**/}
{/**/}
{/**/}
{/**/}
{/**/}
{/*
*/}
{
this.state.configs.map(config => (
{
youMustFill[config]?
*
:
''
}
))
}
{
manageUsers.includes(this.props.userID) ?
{
this.state.showOK === true ?
:
''
}
:
this.props.trialcase ?
''
:
{
this.state.showOK === true ?
:
''
}
}
)
}
}
export default DeployCardRender;