DeployCard.js 9.6 KB

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