Config.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. import React, {Component} from 'react';
  2. import {Button, Input, Spin, Icon} from 'antd';
  3. import {SHOW_CLOUD, ADD_CLOUD, UPDATE_CLOUD} from "../gql";
  4. import {request} from 'graphql-request'
  5. const idGen = (kind) => {
  6. return kind + '_' + Date.now() + '_' + Math.random().toString().slice(-8);
  7. };
  8. class Config extends Component {
  9. constructor(props) {
  10. super(props);
  11. this.state = {
  12. show: false,
  13. showOK: '',
  14. showNotOK: '',
  15. tencentCLoudID: '',
  16. tenID: '',
  17. tenKey: '',
  18. aliyunCLoudID: '',
  19. aliID: '',
  20. aliKey: '',
  21. awsCLoudID: '',
  22. awsID: '',
  23. awsKey: '',
  24. };
  25. request('http://123.206.193.98:3000/graphql', SHOW_CLOUD, {user_id: props.userID}).then(
  26. data => {
  27. let tencent = data.cloud_by_props.find(cloud => cloud.cloudName === 'tencent');
  28. let aliyun = data.cloud_by_props.find(cloud => cloud.cloudName === 'aliyun');
  29. let amazon = data.cloud_by_props.find(cloud => cloud.cloudName === 'amazon');
  30. this.setState({
  31. show: true,
  32. tencentCLoudID: tencent ? tencent.id : '',
  33. tenID: tencent ? tencent.secretId : '',
  34. tenKey: tencent ? tencent.secretKey : '',
  35. aliyunCLoudID: aliyun ? aliyun.id : '',
  36. aliID: aliyun ? aliyun.secretId : '',
  37. aliKey: aliyun ? aliyun.secretKey : '',
  38. awsCLoudID: amazon ? amazon.id : '',
  39. awsID: amazon ? amazon.secretId : '',
  40. awsKey: amazon ? amazon.secretKey : ''
  41. });
  42. }
  43. );
  44. }
  45. inputChange = (kind) => {
  46. return (e) => {
  47. this.setState({
  48. [kind]: e.target.value
  49. })
  50. }
  51. };
  52. submit = (id, cloudName, secretId, secretKey) => {
  53. return () => {
  54. if (id === '') {
  55. let varObj = {
  56. id: idGen('cloud'),
  57. user_id: this.props.userID,
  58. appId: '',
  59. cloudName,
  60. secretId,
  61. secretKey,
  62. createdAt: new Date().getTime(),
  63. updatedAt: ''
  64. };
  65. request('http://123.206.193.98:3000/graphql', ADD_CLOUD, varObj).then(
  66. data => {
  67. if(data.create_cloud.length !== null) {
  68. this.setState({
  69. showOK: cloudName
  70. });
  71. setTimeout(()=>{
  72. this.setState({
  73. showOK: ''
  74. });
  75. }, 1500)
  76. } else {
  77. this.setState({
  78. showNotOK: cloudName
  79. });
  80. setTimeout(()=>{
  81. this.setState({
  82. showNotOK: ''
  83. });
  84. }, 1500)
  85. }
  86. }
  87. );
  88. } else {
  89. let varObj = {
  90. id,
  91. secretId,
  92. secretKey,
  93. updatedAt: new Date().getTime()
  94. };
  95. request('http://123.206.193.98:3000/graphql', UPDATE_CLOUD, varObj).then(
  96. data => {
  97. if(data.update_cloud !== null) {
  98. this.setState({
  99. showOK: cloudName
  100. });
  101. setTimeout(()=>{
  102. this.setState({
  103. showOK: ''
  104. });
  105. }, 1500)
  106. } else {
  107. this.setState({
  108. showNotOK: cloudName
  109. });
  110. setTimeout(()=>{
  111. this.setState({
  112. showNotOK: ''
  113. });
  114. }, 1500)
  115. }
  116. }
  117. );
  118. }
  119. }
  120. };
  121. render() {
  122. return (
  123. this.state.show ?
  124. <div>
  125. <div>
  126. <span className='cloud-name'>Tencent: </span>
  127. <div style={{marginBottom: 15}}>
  128. <div>
  129. <div>
  130. ID: <Input style={{width: 250}} value={this.state.tenID}
  131. onChange={this.inputChange('tenID')}/>
  132. </div>
  133. <div>
  134. Key: <Input style={{width: 250}} value={this.state.tenKey}
  135. onChange={this.inputChange('tenKey')}/>
  136. </div>
  137. {
  138. this.state.showOK === 'tencent'?
  139. <Icon type="check-circle" theme="twoTone" twoToneColor="#52c41a" />
  140. :
  141. this.state.showNotOK === 'tencent'?
  142. <span>not ok</span>
  143. :
  144. ""
  145. }
  146. </div>
  147. <Button type='primary' onClick={this.submit(this.state.tencentCLoudID ,'tencent', this.state.tenID, this.state.tenKey)}>ok</Button>
  148. </div>
  149. </div>
  150. <div>
  151. <span className='cloud-name'>Aliyun: </span>
  152. <div style={{marginBottom: 15}}>
  153. <div>
  154. <div>
  155. ID: <Input style={{width: 250}} value={this.state.aliID}
  156. onChange={this.inputChange('aliID')}/>
  157. </div>
  158. <div>
  159. Key: <Input style={{width: 250}} value={this.state.aliKey}
  160. onChange={this.inputChange('aliKey')}/>
  161. </div>
  162. {
  163. this.state.showOK === 'aliyun'?
  164. <Icon type="check-circle" theme="twoTone" twoToneColor="#52c41a" />
  165. :
  166. this.state.showNotOK === 'aliyun'?
  167. <span>not ok</span>
  168. :
  169. ""
  170. }
  171. </div>
  172. <Button type='primary' onClick={this.submit(this.state.aliyunCLoudID, 'aliyun', this.state.aliID, this.state.aliKey)}>ok</Button>
  173. </div>
  174. </div>
  175. <div>
  176. <span className='cloud-name'>AWS: </span>
  177. <div style={{marginBottom: 15}}>
  178. <div>
  179. <div>
  180. ID: <Input style={{width: 250}} value={this.state.awsID}
  181. onChange={this.inputChange('awsID')}/>
  182. </div>
  183. <div>
  184. Key: <Input style={{width: 250}} value={this.state.awsKey}
  185. onChange={this.inputChange('awsKey')}/>
  186. </div>
  187. {
  188. this.state.showOK === 'amazon'?
  189. <Icon type="check-circle" theme="twoTone" twoToneColor="#52c41a" />
  190. :
  191. this.state.showNotOK === 'amazon'?
  192. <span>not ok</span>
  193. :
  194. ""
  195. }
  196. </div>
  197. <Button type='primary' onClick={this.submit(this.state.awsCLoudID, 'amazon', this.state.awsID, this.state.awsKey)}>ok</Button>
  198. </div>
  199. </div>
  200. </div>
  201. :
  202. <Spin style={{marginLeft: 3}}/>
  203. )
  204. }
  205. }
  206. export default Config;