APIPathCard.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. import React, {Component} from 'react';
  2. import {Input, Radio, Collapse, Button, Icon} from 'antd';
  3. import {request} from 'graphql-request'
  4. import {ADD_APIGWPATH, UPDATE_APIGWPATH} from "../../../../gql";
  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. 'apiGWName': 'Name',
  14. 'apiGWDesc': 'Description',
  15. 'requestMethod': 'Method'
  16. };
  17. class APIPathCard extends Component {
  18. constructor(props) {
  19. super(props);
  20. let {apiGWName, apiGWDesc, requestMethod} = props.path;
  21. props.path !== '' ?
  22. this.state = {
  23. configs: ['apiGWName', 'apiGWDesc'],
  24. showOK: false,
  25. apiGWName,
  26. apiGWDesc,
  27. requestMethod: requestMethod
  28. }
  29. :
  30. this.state = {
  31. configs: ['apiGWName', 'apiGWDesc'],
  32. showOK: false,
  33. apiGWName: props.defalutName,
  34. apiGWDesc: '',
  35. requestMethod: 'GET'
  36. };
  37. }
  38. componentWillReceiveProps(next) {
  39. if (next.path !== '') {
  40. let {apiGWName, apiGWDesc, requestMethod} = next.path;
  41. this.setState({
  42. apiGWName,
  43. apiGWDesc,
  44. requestMethod
  45. })
  46. } else {
  47. this.setState({
  48. apiGWName: next.defalutName,
  49. apiGWDesc: '',
  50. requestMethod: 'GET'
  51. });
  52. }
  53. };
  54. switchConfig = (label) => {
  55. return (e) => {
  56. this.setState({
  57. [label]: e.target.value
  58. })
  59. };
  60. };
  61. ok = () => {
  62. let {apiGWName, apiGWDesc, requestMethod} = this.state;
  63. let varObj = {
  64. id: idGen('path'),
  65. user_id: this.props.userID,
  66. apiGWGroup_id: this.props.groupID,
  67. deploy_id: this.props.deployID,
  68. apiGWName,
  69. apiGWDesc,
  70. requestMethod,
  71. serviceType: 'SCF',
  72. apiGWPath: '/graphql',
  73. timeout: 300,
  74. apiId: '',
  75. createdAt: new Date().getTime(),
  76. updatedAt: ''
  77. };
  78. if (this.props.path === '') {
  79. if(this.props.deployID !== '' && this.props.groupID!=='') {
  80. request(graphqlUrl, ADD_APIGWPATH, varObj).then(
  81. data => {
  82. if (data.create_apiGWPath !== null) {
  83. this.setState({
  84. showOK: true
  85. })
  86. }
  87. setTimeout(()=>{
  88. this.setState({
  89. showOK: false
  90. })
  91. }, 1500)
  92. }
  93. )
  94. }
  95. } else {
  96. let {apiGWName, apiGWDesc, requestMethod} = this.state;
  97. let varObj = {
  98. id: this.props.path.id,
  99. apiGWName,
  100. apiGWDesc,
  101. requestMethod,
  102. updatedAt: new Date().getTime()
  103. };
  104. request(graphqlUrl, UPDATE_APIGWPATH, varObj).then(
  105. data => {
  106. if (data.update_apiGWPath !== null) {
  107. this.setState({
  108. showOK: true
  109. })
  110. }
  111. setTimeout(()=>{
  112. this.setState({
  113. showOK: false
  114. })
  115. }, 1500)
  116. }
  117. )
  118. }
  119. };
  120. render() {
  121. const customPanelStyle = {
  122. background: '#f7f7f7',
  123. borderRadius: 4,
  124. marginBottom: 24,
  125. border: 0,
  126. overflow: 'hidden',
  127. };
  128. return (
  129. <div>
  130. <Collapse bordered={false}>
  131. <Panel header="Want more options?" style={customPanelStyle}>
  132. {
  133. this.state.configs.map(config => (
  134. <div key={config} style={{marginBottom: 10}}>
  135. <span className='vice-title'>{valueToKey[config]}: </span>
  136. <Input value={this.state[config]} style={{width: 200}}
  137. onChange={this.switchConfig(config)}/>
  138. </div>
  139. ))
  140. }
  141. <div style={{marginBottom: 10}}>
  142. <span className='vice-title'>{valueToKey['requestMethod']}: </span>
  143. <Radio.Group onChange={this.switchConfig('requestMethod')}
  144. defaultValue={this.state.requestMethod}
  145. buttonStyle="solid">
  146. <Radio.Button value="GET">get</Radio.Button>
  147. <Radio.Button value="POST">post</Radio.Button>
  148. </Radio.Group>
  149. </div>
  150. </Panel>
  151. </Collapse>
  152. {
  153. manageUsers.includes(this.props.userID) ?
  154. <div>
  155. <Button onClick={this.ok} type='primary'>save</Button>
  156. {
  157. this.state.showOK === true?
  158. <Icon type="check-circle" theme="twoTone" twoToneColor="#52c41a"/>
  159. :
  160. ''
  161. }
  162. </div>
  163. :
  164. this.props.trialcase?
  165. ''
  166. :
  167. <div>
  168. <Button onClick={this.ok} type='primary'>save</Button>
  169. {
  170. this.state.showOK === true?
  171. <Icon type="check-circle" theme="twoTone" twoToneColor="#52c41a"/>
  172. :
  173. ''
  174. }
  175. </div>
  176. }
  177. </div>
  178. )
  179. }
  180. }
  181. export default APIPathCard;