DeployCard.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. import React, {Component} from 'react';
  2. import {Input, Radio, Collapse, Button, Icon, Tooltip} from 'antd';
  3. import {ADD_DEPLOY, UPDATE_DEPLOY} from "../../../../gql";
  4. import {request} from 'graphql-request'
  5. import {idGen} from "../../../../func";
  6. import {graphqlUrl} from "../../../../config";
  7. import {manageUsers} from "../../../../config";
  8. const Panel = Collapse.Panel;
  9. // 如需改变显示,请在此处 value 处更改
  10. // 如需添加中文,请在此处 value 处添加
  11. // eg: 'xxxx': <F.. id='xxx'/>
  12. const valueToKey = {
  13. 'functionName': 'Name',
  14. 'cosBucketName': 'cosBucketName',
  15. 'cosObjectName': 'cosObjectName',
  16. 'cosBucketRegion': 'cosBucketRegion',
  17. 'serviceName': 'serviceName',
  18. 'subnetId': 'subnetId',
  19. 'vpcId': 'vpcId',
  20. 'region': 'region',
  21. 'description': 'description',
  22. };
  23. const toolTipTitle = {
  24. 'functionName': 'its funcname',
  25. 'cosBucketName': 'its cosBucketName',
  26. 'cosObjectName': 'its cosObjectName',
  27. 'cosBucketRegion': 'its cosBucketRegion',
  28. 'serviceName': 'its serviceName',
  29. 'subnetId': 'its subnetId',
  30. 'vpcId': 'its vpcId',
  31. 'region': 'its region',
  32. 'description': 'its description',
  33. };
  34. const removePrefix = (prefix, value) => {
  35. let r = new RegExp(prefix);
  36. return value.replace(r, '');
  37. };
  38. const shiftPrefix = (prefix, value) => {
  39. value = removePrefix(prefix, value);
  40. return prefix + value;
  41. };
  42. class DeployCard extends Component {
  43. constructor(props) {
  44. super(props);
  45. let {functionName, cosBucketName, cosObjectName, cosBucketRegion, serviceName, vpcId, subnetId, region, description} = props.deploy;
  46. props.deploy !== '' ?
  47. this.state = {
  48. configs: ['cosBucketName', 'cosObjectName', 'description', 'vpcId', 'subnetId'],
  49. description,
  50. showOK: false,
  51. functionName,
  52. region,
  53. cosBucketName,
  54. cosObjectName,
  55. cosBucketRegion,
  56. serviceName,
  57. vpcId,
  58. subnetId
  59. }
  60. :
  61. this.state = {
  62. configs: ['cosBucketName', 'cosObjectName', 'description', 'vpcId', 'subnetId'],
  63. description: '',
  64. showOK: false,
  65. functionName: props.defalutName,
  66. region: props.region,
  67. cosBucketName: 'graphqlfc',
  68. cosObjectName: props.defalutName,
  69. cosBucketRegion: props.region,
  70. serviceName: '',
  71. vpcId: '',
  72. subnetId: '',
  73. };
  74. }
  75. componentWillReceiveProps(next) {
  76. if (next.deploy !== '') {
  77. let {functionName, cosBucketName, cosObjectName, cosBucketRegion, serviceName, vpcId, subnetId, region, description} = next.deploy;
  78. this.setState({
  79. description,
  80. functionName,
  81. region: next.region === '' ? region : next.region,
  82. cosBucketName,
  83. cosObjectName,
  84. cosBucketRegion: next.region === '' ? cosBucketRegion : next.region,
  85. serviceName,
  86. vpcId,
  87. subnetId
  88. })
  89. } else {
  90. this.setState({
  91. functionName: next.defalutName,
  92. region: next.region === '' ? 'ap-beijing' : next.region,
  93. cosBucketName: 'graphqlfc',
  94. cosObjectName: next.defalutName,
  95. cosBucketRegion: next.region === '' ? 'ap-beijing' : next.region,
  96. serviceName: '',
  97. vpcId: '',
  98. subnetId: ''
  99. });
  100. }
  101. };
  102. switchConfig = (label) => {
  103. return (e) => {
  104. this.setState({
  105. [label]: e.target.value
  106. })
  107. };
  108. };
  109. ok = (id) => {
  110. let {description, cosBucketName, subnetId, cosObjectName, region, vpcId, cosBucketRegion, functionName} = this.state;
  111. region = shiftPrefix('ap-', region);
  112. cosBucketRegion = shiftPrefix('ap-', cosBucketRegion);
  113. let varObj = {
  114. id,
  115. cloud_id: this.props.cloudID,
  116. user_id: this.props.userID,
  117. description,
  118. cosBucketName,
  119. subnetId,
  120. cosObjectName,
  121. region,
  122. vpcId,
  123. cosBucketRegion,
  124. functionName,
  125. memorySize: 512,
  126. timeout: 300,
  127. handler: 'tencent_graphql.Bridge::handler',
  128. serviceName: "",
  129. fc_id: '',
  130. createdAt: new Date().getTime(),
  131. updatedAt: ''
  132. };
  133. if (this.props.deploy === '') {
  134. request(graphqlUrl, ADD_DEPLOY, varObj).then(
  135. data => {
  136. if (data.create_deploy !== null) {
  137. this.setState({
  138. showOK: true
  139. })
  140. }
  141. setTimeout(() => {
  142. this.setState({
  143. showOK: false
  144. })
  145. }, 1500)
  146. }
  147. )
  148. } else {
  149. let {description, cosBucketName, subnetId, cosObjectName, region, vpcId, cosBucketRegion, functionName} = this.state;
  150. region = shiftPrefix('ap-', region);
  151. cosBucketRegion = shiftPrefix('ap-', cosBucketRegion);
  152. let varObj = {
  153. id: this.props.deploy.id,
  154. description,
  155. subnetId,
  156. cosBucketName,
  157. cosObjectName,
  158. region,
  159. vpcId,
  160. cosBucketRegion,
  161. functionName,
  162. updatedAt: new Date().getTime()
  163. };
  164. request(graphqlUrl, UPDATE_DEPLOY, varObj).then(
  165. data => {
  166. if (data.update_deploy !== null) {
  167. this.setState({
  168. showOK: true
  169. })
  170. }
  171. setTimeout(() => {
  172. this.setState({
  173. showOK: false
  174. })
  175. }, 1500)
  176. }
  177. )
  178. }
  179. };
  180. render() {
  181. const customPanelStyle = {
  182. background: '#f7f7f7',
  183. borderRadius: 4,
  184. marginBottom: 24,
  185. border: 0,
  186. overflow: 'hidden',
  187. };
  188. const ioobotCloudID = ['tencent_CloudID', 'aliyun_CloudID'];
  189. const disable = {
  190. 'subnetId': ioobotCloudID.includes(this.props.cloudID) ? true : '',
  191. 'vpcId': ioobotCloudID.includes(this.props.cloudID) ? true : '',
  192. };
  193. return (
  194. <div>
  195. <div style={{marginBottom: 10}}>
  196. <span className='vice-title'>{valueToKey['functionName']}
  197. <span> </span>
  198. <Tooltip placement="top" title={toolTipTitle['functionName']}>
  199. <Icon type="question-circle"/>
  200. </Tooltip>
  201. </span>
  202. <Input value={this.state.functionName} style={{width: 400}}
  203. onChange={this.switchConfig('functionName')}/>
  204. </div>
  205. <div style={{marginBottom: 10}}>
  206. <span className='vice-title'>{valueToKey['region']}
  207. <span> </span>
  208. <Tooltip placement="top" title={toolTipTitle['region']}>
  209. <Icon type="question-circle"/>
  210. </Tooltip>
  211. </span>
  212. <Radio.Group onChange={(e) => {
  213. this.props.switchRegion(e);
  214. }} defaultValue={this.state.region} value={removePrefix('ap-', this.state.region)}
  215. buttonStyle="solid">
  216. <Radio.Button value="guangzhou">Guangzhou</Radio.Button>
  217. <Radio.Button value="shanghai">Shanghai</Radio.Button>
  218. <Radio.Button value="beijing">Beijing</Radio.Button>
  219. <Radio.Button value="chengdu">Chengdu</Radio.Button>
  220. </Radio.Group>
  221. </div>
  222. <Collapse bordered={false}>
  223. <Panel header="Want more options?" style={customPanelStyle}>
  224. <div style={{marginBottom: 10}}>
  225. <span className='vice-title'>{valueToKey['cosBucketRegion']}
  226. <span> </span>
  227. <Tooltip placement="top" title={toolTipTitle['cosBucketRegion']}>
  228. <Icon type="question-circle"/>
  229. </Tooltip>
  230. </span>
  231. <Radio.Group onChange={(e) => {
  232. // 如果不想一变所有都变,就将 onchange 改成
  233. // onChange={this.switchConfig('cosBucketRegion')}
  234. this.props.switchRegion(e);
  235. }} defaultValue={this.state.region}
  236. value={removePrefix('ap-', this.state.cosBucketRegion)} buttonStyle="solid">
  237. <Radio.Button value="guangzhou">Guangzhou</Radio.Button>
  238. <Radio.Button value="shanghai">Shanghai</Radio.Button>
  239. <Radio.Button value="beijing">Beijing</Radio.Button>
  240. <Radio.Button value="chengdu">Chengdu</Radio.Button>
  241. </Radio.Group>
  242. </div>
  243. {/*腾讯云为空字符串,不显示,这里未作区分*/}
  244. {/*<div style={{marginBottom: 10}}>*/}
  245. {/*<span className='vice-title'>{valueToKey['serviceName']}*/}
  246. {/*<span> </span>*/}
  247. {/*<Tooltip placement="top" title={toolTipTitle['serviceName']}>*/}
  248. {/*<Icon type="question-circle"/>*/}
  249. {/*</Tooltip>*/}
  250. {/*</span>*/}
  251. {/*<Input value={this.state.serviceName} style={{width: 200}} disabled*/}
  252. {/*onChange={this.switchConfig('serviceName')}/>*/}
  253. {/*</div>*/}
  254. {
  255. this.state.configs.map(config => (
  256. <div key={config} style={{marginBottom: 10}}>
  257. <span className='vice-title'>{valueToKey[config]}
  258. <span> </span>
  259. <Tooltip placement="top" title={toolTipTitle[config]}>
  260. <Icon type="question-circle"/>
  261. </Tooltip>
  262. </span>
  263. <Input value={this.state[config]} style={{width: 200}}
  264. disabled={disable[config] === true}
  265. onChange={this.switchConfig(config)}/>
  266. </div>
  267. ))
  268. }
  269. </Panel>
  270. </Collapse>
  271. {
  272. manageUsers.includes(this.props.userID) ?
  273. <div>
  274. <Button onClick={() => {
  275. const id = idGen('deploy');
  276. this.ok(id);
  277. this.props.pass(id, 'deploy');
  278. }} type='primary'>save</Button>
  279. {
  280. this.state.showOK === true ?
  281. <Icon type="check-circle" theme="twoTone" twoToneColor="#52c41a"/>
  282. :
  283. ''
  284. }
  285. </div>
  286. :
  287. this.props.trialcase ?
  288. ''
  289. :
  290. <div>
  291. <Button onClick={() => {
  292. const id = idGen('deploy');
  293. this.ok(id);
  294. this.props.pass(id, 'deploy');
  295. }} type='primary'>save</Button>
  296. {
  297. this.state.showOK === true ?
  298. <Icon type="check-circle" theme="twoTone" twoToneColor="#52c41a"/>
  299. :
  300. ''
  301. }
  302. </div>
  303. }
  304. </div>
  305. )
  306. }
  307. }
  308. export default DeployCard;