APIPathCard.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. import React, {Component} from 'react';
  2. import {FormattedMessage} from 'react-intl';
  3. import {Input, Radio, Collapse, Button, Icon, Tooltip} from 'antd';
  4. import {request} from 'graphql-request'
  5. import {ADD_APIGWPATH, UPDATE_APIGWPATH} from "../../../../gql";
  6. import {idGen} from "../../../../func";
  7. import {graphqlUrl} from "../../../../config";
  8. import {manageUsers} from "../../../../config";
  9. const Panel = Collapse.Panel;
  10. // 如需改变显示,请在此处 value 处更改
  11. // 如需添加中文,请在此处 value 处添加
  12. // eg: 'xxxx': <F.. id='xxx'/>
  13. const valueToKey = {
  14. 'apiGWName': 'apiGWName',
  15. 'apiGWDesc': 'apiGWDesc',
  16. 'requestMethod': 'requestMethod'
  17. };
  18. const toolTipTitle = {
  19. 'apiGWName': 'its apiGWName',
  20. 'apiGWDesc': 'its apiGWDesc',
  21. 'requestMethod': 'its requestMethod'
  22. };
  23. const youMustFill = {
  24. 'apiGWName': true,
  25. 'apiGWDesc': false,
  26. 'requestMethod': true
  27. };
  28. class APIPathCard extends Component {
  29. constructor(props) {
  30. super(props);
  31. if(props.path !== '' && props.path !== null) {
  32. let {apiGWName, apiGWDesc, requestMethod} = props.path;
  33. this.state = {
  34. configs: ['apiGWName', 'apiGWDesc'],
  35. showOK: false,
  36. apiGWName,
  37. apiGWDesc,
  38. requestMethod: requestMethod
  39. }
  40. } else {
  41. this.state = {
  42. configs: ['apiGWName', 'apiGWDesc'],
  43. showOK: false,
  44. apiGWName: props.defalutName,
  45. apiGWDesc: '',
  46. requestMethod: 'GET'
  47. };
  48. }
  49. }
  50. componentWillReceiveProps(next) {
  51. if (next.path !== '' && next.path !== null) {
  52. let {apiGWName, apiGWDesc, requestMethod} = next.path;
  53. this.setState({
  54. apiGWName,
  55. apiGWDesc,
  56. requestMethod
  57. })
  58. } else {
  59. this.setState({
  60. apiGWName: next.defalutName,
  61. apiGWDesc: '',
  62. requestMethod: 'GET'
  63. });
  64. }
  65. };
  66. switchConfig = (label) => {
  67. return (e) => {
  68. this.setState({
  69. [label]: e.target.value
  70. })
  71. };
  72. };
  73. ok = (id) => {
  74. let {apiGWName, apiGWDesc, requestMethod} = this.state;
  75. let varObj = {
  76. id,
  77. user_id: this.props.userID,
  78. apiGWGroup_id: this.props.groupID,
  79. deploy_id: this.props.deployID,
  80. apiGWName,
  81. apiGWDesc,
  82. requestMethod,
  83. serviceType: 'SCF',
  84. apiGWPath: '/graphql',
  85. timeout: 300,
  86. apiId: '',
  87. createdAt: new Date().getTime(),
  88. updatedAt: ''
  89. };
  90. if (this.props.path === '') {
  91. if(this.props.deployID !== '' && this.props.groupID!=='' && this.props.deployID !== null && this.props.groupID!==null) {
  92. request(graphqlUrl, ADD_APIGWPATH, varObj).then(
  93. data => {
  94. if (data.create_apiGWPath !== null) {
  95. this.setState({
  96. showOK: true
  97. });
  98. this.props.stepByStep(3);
  99. }
  100. setTimeout(()=>{
  101. this.setState({
  102. showOK: false
  103. })
  104. }, 1500)
  105. }
  106. )
  107. }
  108. } else {
  109. let {apiGWName, apiGWDesc, requestMethod} = this.state;
  110. let varObj = {
  111. id: this.props.path.id,
  112. apiGWName,
  113. apiGWDesc,
  114. requestMethod,
  115. updatedAt: new Date().getTime()
  116. };
  117. request(graphqlUrl, UPDATE_APIGWPATH, varObj).then(
  118. data => {
  119. if (data.update_apiGWPath !== null) {
  120. this.setState({
  121. showOK: true
  122. });
  123. this.props.stepByStep(3);
  124. }
  125. setTimeout(()=>{
  126. this.setState({
  127. showOK: false
  128. })
  129. }, 1500)
  130. }
  131. )
  132. }
  133. };
  134. render() {
  135. const customPanelStyle = {
  136. background: '#f7f7f7',
  137. borderRadius: 4,
  138. marginBottom: 24,
  139. border: 0,
  140. overflow: 'hidden',
  141. };
  142. return (
  143. <div>
  144. <Collapse bordered={false}>
  145. <Panel header=<FormattedMessage id="Want more options?"/> style={customPanelStyle}>
  146. {
  147. this.state.configs.map(config => (
  148. <div key={config} style={{marginBottom: 10}}>
  149. <span className='vice-title'>
  150. {
  151. youMustFill[config]?
  152. <span style={{color: 'red', display: 'inline', marginRight: 10}}>*</span>
  153. :
  154. ''
  155. }
  156. <FormattedMessage id={valueToKey[config]}/>
  157. &nbsp;
  158. <Tooltip placement="top" title={toolTipTitle[config]}>
  159. <Icon type="question-circle"/>
  160. </Tooltip>
  161. </span>
  162. <Input value={this.state[config]} style={{width: 200}}
  163. onChange={this.switchConfig(config)}/>
  164. </div>
  165. ))
  166. }
  167. <div style={{marginBottom: 10}}>
  168. <span className='vice-title'>
  169. {
  170. youMustFill['requestMethod']?
  171. <span style={{color: 'red', display: 'inline', marginRight: 10}}>*</span>
  172. :
  173. ''
  174. }
  175. <FormattedMessage id={valueToKey['requestMethod']}/>
  176. &nbsp;
  177. <Tooltip placement="top" title={toolTipTitle['requestMethod']}>
  178. <Icon type="question-circle"/>
  179. </Tooltip>
  180. </span>
  181. <Radio.Group onChange={this.switchConfig('requestMethod')}
  182. defaultValue={this.state.requestMethod}
  183. buttonStyle="solid">
  184. <Radio.Button value="GET">get</Radio.Button>
  185. <Radio.Button value="POST">post</Radio.Button>
  186. </Radio.Group>
  187. </div>
  188. </Panel>
  189. </Collapse>
  190. {
  191. manageUsers.includes(this.props.userID) ?
  192. <div>
  193. <Button onClick={()=>{
  194. const id = idGen('path');
  195. this.ok(id);
  196. this.props.pass(id, 'path');
  197. }} type='primary'><FormattedMessage id="save"/></Button>
  198. {
  199. this.state.showOK === true?
  200. <Icon type="check-circle" theme="twoTone" twoToneColor="#52c41a"/>
  201. :
  202. ''
  203. }
  204. </div>
  205. :
  206. this.props.trialcase?
  207. ''
  208. :
  209. <div>
  210. <Button onClick={()=>{
  211. const id = idGen('path');
  212. this.ok(id);
  213. this.props.pass(id, 'path');
  214. }} type='primary'><FormattedMessage id="save"/></Button>
  215. {
  216. this.state.showOK === true?
  217. <Icon type="check-circle" theme="twoTone" twoToneColor="#52c41a"/>
  218. :
  219. ''
  220. }
  221. </div>
  222. }
  223. </div>
  224. )
  225. }
  226. }
  227. export default APIPathCard;