DeployCard.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. import React, {Component} from 'react';
  2. import {Input, Radio, Collapse, Button, Icon} 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. class DeployCard extends Component {
  24. constructor(props) {
  25. super(props);
  26. let {functionName, cosBucketName, cosObjectName, cosBucketRegion, serviceName, vpcId, subnetId, region, description} = props.deploy;
  27. props.deploy !== '' ?
  28. this.state = {
  29. configs: ['description', 'cosBucketName', 'cosObjectName', 'vpcId', 'subnetId'],
  30. description,
  31. showOK: false,
  32. functionName,
  33. region,
  34. cosBucketName,
  35. cosObjectName,
  36. cosBucketRegion,
  37. serviceName,
  38. vpcId,
  39. subnetId
  40. }
  41. :
  42. this.state = {
  43. configs: ['description', 'cosBucketName', 'cosObjectName', 'vpcId', 'subnetId'],
  44. description: '',
  45. showOK: false,
  46. functionName: props.defalutName,
  47. region: props.region,
  48. cosBucketName: 'graphqlfc',
  49. cosObjectName: props.defalutName,
  50. cosBucketRegion: props.region,
  51. serviceName: '',
  52. vpcId: '',
  53. subnetId: '',
  54. }
  55. }
  56. componentWillReceiveProps(next) {
  57. if (next.deploy !== '') {
  58. let {functionName, cosBucketName, cosObjectName, cosBucketRegion, serviceName, vpcId, subnetId, region, description} = next.deploy;
  59. this.setState({
  60. description,
  61. functionName,
  62. region,
  63. cosBucketName,
  64. cosObjectName,
  65. cosBucketRegion,
  66. serviceName,
  67. vpcId,
  68. subnetId
  69. })
  70. } else {
  71. this.setState({
  72. functionName: next.defalutName,
  73. region: next.region,
  74. cosBucketName: 'graphqlfc',
  75. cosObjectName: next.defalutName,
  76. cosBucketRegion: next.region,
  77. serviceName: '',
  78. vpcId: '',
  79. subnetId: ''
  80. });
  81. }
  82. };
  83. switchConfig = (label) => {
  84. return (e) => {
  85. this.setState({
  86. [label]: e.target.value
  87. })
  88. };
  89. };
  90. ok = (id) => {
  91. let {description, cosBucketName, subnetId, cosObjectName, region, vpcId, cosBucketRegion, functionName} = this.state;
  92. let varObj = {
  93. id,
  94. cloud_id: this.props.cloudID,
  95. user_id: this.props.userID,
  96. description,
  97. cosBucketName,
  98. subnetId,
  99. cosObjectName,
  100. region,
  101. vpcId,
  102. cosBucketRegion,
  103. functionName,
  104. memorySize: 512,
  105. timeout: 300,
  106. handler: 'tencent_graphql.Bridge::handler',
  107. serviceName: "",
  108. fc_id: '',
  109. createdAt: new Date().getTime(),
  110. updatedAt: ''
  111. };
  112. if (this.props.deploy === '') {
  113. request(graphqlUrl, ADD_DEPLOY, varObj).then(
  114. data => {
  115. if (data.create_deploy !== null) {
  116. this.setState({
  117. showOK: true
  118. })
  119. }
  120. setTimeout(() => {
  121. this.setState({
  122. showOK: false
  123. })
  124. }, 1500)
  125. }
  126. )
  127. } else {
  128. let {description, cosBucketName, subnetId, cosObjectName, region, vpcId, cosBucketRegion, functionName} = this.state;
  129. let varObj = {
  130. id: this.props.deploy.id,
  131. description,
  132. subnetId,
  133. cosBucketName,
  134. cosObjectName,
  135. region,
  136. vpcId,
  137. cosBucketRegion,
  138. functionName,
  139. updatedAt: new Date().getTime()
  140. };
  141. request(graphqlUrl, UPDATE_DEPLOY, varObj).then(
  142. data => {
  143. if (data.update_deploy !== null) {
  144. this.setState({
  145. showOK: true
  146. })
  147. }
  148. setTimeout(() => {
  149. this.setState({
  150. showOK: false
  151. })
  152. }, 1500)
  153. }
  154. )
  155. }
  156. };
  157. render() {
  158. const customPanelStyle = {
  159. background: '#f7f7f7',
  160. borderRadius: 4,
  161. marginBottom: 24,
  162. border: 0,
  163. overflow: 'hidden',
  164. };
  165. return (
  166. <div>
  167. <div style={{marginBottom: 10}}>
  168. <span className='vice-title'>{valueToKey['functionName']}: </span>
  169. <Input value={this.state.functionName} style={{width: 400}}
  170. onChange={this.switchConfig('functionName')}/>
  171. </div>
  172. <div style={{marginBottom: 10}}>
  173. <span className='vice-title'>{valueToKey['region']}: </span>
  174. <Radio.Group onChange={(e) => {
  175. this.props.switchRegion(e);
  176. this.setState({
  177. region: e.target.value
  178. })
  179. }} defaultValue={this.state.region} buttonStyle="solid">
  180. <Radio.Button value="guangzhou">Guangzhou</Radio.Button>
  181. <Radio.Button value="shanghai">Shanghai</Radio.Button>
  182. <Radio.Button value="beijing">Beijing</Radio.Button>
  183. <Radio.Button value="chengdu">Chengdu</Radio.Button>
  184. </Radio.Group>
  185. </div>
  186. <Collapse bordered={false}>
  187. <Panel header="Want more options?" style={customPanelStyle}>
  188. <div style={{marginBottom: 10}}>
  189. <span className='vice-title'>{valueToKey['cosBucketRegion']}: </span>
  190. <Radio.Group onChange={this.switchConfig('cosBucketRegion')}
  191. value={this.state.cosBucketRegion} buttonStyle="solid">
  192. <Radio.Button value="guangzhou">Guangzhou</Radio.Button>
  193. <Radio.Button value="shanghai">Shanghai</Radio.Button>
  194. <Radio.Button value="beijing">Beijing</Radio.Button>
  195. <Radio.Button value="chengdu">Chengdu</Radio.Button>
  196. </Radio.Group>
  197. </div>
  198. <div style={{marginBottom: 10}}>
  199. <span className='vice-title'>{valueToKey['serviceName']}: </span>
  200. <Input value={this.state.serviceName} style={{width: 200}} disabled
  201. onChange={this.switchConfig('serviceName')}/>
  202. </div>
  203. {
  204. this.state.configs.map(config => (
  205. <div key={config} style={{marginBottom: 10}}>
  206. <span className='vice-title'>{valueToKey[config]}: </span>
  207. <Input value={this.state[config]} style={{width: 200}}
  208. onChange={this.switchConfig(config)}/>
  209. </div>
  210. ))
  211. }
  212. </Panel>
  213. </Collapse>
  214. {
  215. manageUsers.includes(this.props.userID) ?
  216. <div>
  217. <Button onClick={() => {
  218. const id = idGen('deploy');
  219. this.ok(id);
  220. this.props.pass(id, 'deploy');
  221. }} type='primary'>save</Button>
  222. {
  223. this.state.showOK === true ?
  224. <Icon type="check-circle" theme="twoTone" twoToneColor="#52c41a"/>
  225. :
  226. ''
  227. }
  228. </div>
  229. :
  230. this.props.trialcase ?
  231. ''
  232. :
  233. <div>
  234. <Button onClick={() => {
  235. const id = idGen('deploy');
  236. this.ok(id);
  237. this.props.pass(id, 'deploy');
  238. }} type='primary'>save</Button>
  239. {
  240. this.state.showOK === true ?
  241. <Icon type="check-circle" theme="twoTone" twoToneColor="#52c41a"/>
  242. :
  243. ''
  244. }
  245. </div>
  246. }
  247. </div>
  248. )
  249. }
  250. }
  251. export default DeployCard;