APIGroupCard.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. import React, {Component} from 'react';
  2. import {Input, Collapse, Button, Radio, Icon} from 'antd';
  3. import {ADD_APIGROUP, UPDATE_APIGROUP} from "../../../../gql";
  4. import {request} from 'graphql-request'
  5. import {idGen} from "../../../../func";
  6. import {graphqlUrl} from "../../../../config";
  7. import {manageUsers} from "../../../../config";
  8. import {FormattedMessage} from 'react-intl';
  9. const Panel = Collapse.Panel;
  10. class APIGroupCard extends Component {
  11. constructor(props) {
  12. super(props);
  13. let {groupName, environmentName, defaultDomain, frontType, userDomain, userStatus, region} = props.group;
  14. props.group !== '' ?
  15. this.state = {
  16. showOK: false,
  17. groupName,
  18. environmentName,
  19. defaultDomain,
  20. frontType,
  21. userDomain,
  22. userStatus,
  23. groupRegion: region
  24. }
  25. :
  26. this.state = {
  27. showOK: false,
  28. groupName: 'graphql-endpoint',
  29. environmentName: 'test',
  30. defaultDomain: '',
  31. frontType: 'http&https',
  32. userDomain: '',
  33. userStatus: 'open',
  34. groupRegion: props.region
  35. };
  36. }
  37. componentWillReceiveProps(next) {
  38. if(next.group !== '') {
  39. let {groupName, environmentName, defaultDomain, frontType, userDomain, userStatus, region} = next.group;
  40. this.setState( {
  41. groupName,
  42. environmentName,
  43. defaultDomain,
  44. frontType,
  45. userDomain,
  46. userStatus,
  47. groupRegion: region
  48. })
  49. } else {
  50. this.setState ( {
  51. groupName: 'graphql-endpoint',
  52. environmentName: 'test',
  53. defaultDomain: '',
  54. frontType: 'http&https',
  55. userDomain: '',
  56. userStatus: 'open',
  57. groupRegion: next.region
  58. });
  59. }
  60. };
  61. switchConfig = (label) => {
  62. return (e) => {
  63. this.setState({
  64. [label]: e.target.value
  65. })
  66. };
  67. };
  68. ok = (id) => {
  69. let {userStatus, userDomain, groupName, frontType, environmentName} = this.state;
  70. let varObj = {
  71. id,
  72. cloud_id: this.props.cloudID,
  73. user_id: this.props.userID,
  74. userStatus,
  75. userDomain,
  76. groupName,
  77. frontType,
  78. region: this.state.groupRegion,
  79. environmentName,
  80. defaultDomain: '',
  81. status: '',
  82. serviceId: '',
  83. createdAt: new Date().getTime(),
  84. updatedAt: ''
  85. };
  86. if (this.props.group === '') {
  87. request(graphqlUrl, ADD_APIGROUP, varObj).then(
  88. data => {
  89. if (data.create_apiGWGroup !== null) {
  90. this.setState({
  91. showOK: true
  92. })
  93. }
  94. setTimeout(()=>{
  95. this.setState({
  96. showOK: false
  97. })
  98. }, 1500)
  99. }
  100. )
  101. } else {
  102. let {userStatus, userDomain, groupName, frontType, environmentName} = this.state;
  103. let varObj = {
  104. id: this.props.group.id,
  105. userStatus,
  106. userDomain,
  107. groupName,
  108. frontType,
  109. region: this.state.groupRegion,
  110. environmentName,
  111. updatedAt: new Date().getTime()
  112. };
  113. request(graphqlUrl, UPDATE_APIGROUP, varObj).then(
  114. data => {
  115. if (data.update_apiGWGroup !== null) {
  116. this.setState({
  117. showOK: true
  118. })
  119. }
  120. setTimeout(()=>{
  121. this.setState({
  122. showOK: false
  123. })
  124. }, 1500)
  125. }
  126. )
  127. }
  128. };
  129. render() {
  130. const customPanelStyle = {
  131. background: '#f7f7f7',
  132. borderRadius: 4,
  133. marginBottom: 24,
  134. border: 0,
  135. overflow: 'hidden',
  136. };
  137. return (
  138. <div>
  139. <Collapse bordered={false}>
  140. <Panel header="Want more options?" style={customPanelStyle}>
  141. <div style={{marginBottom: 10}}>
  142. <span className='vice-title'>groupName: </span>
  143. <Input value={this.state.groupName} style={{width: 400}}
  144. onChange={this.switchConfig('groupName')}/>
  145. </div>
  146. <div style={{marginBottom: 10}}>
  147. <span className='vice-title'>region: </span>
  148. <Radio.Group onChange={this.switchConfig('groupRegion')} value={this.state.groupRegion} buttonStyle="solid">
  149. <Radio.Button value="guangzhou">Guangzhou</Radio.Button>
  150. <Radio.Button value="shanghai">Shanghai</Radio.Button>
  151. <Radio.Button value="beijing">Beijing</Radio.Button>
  152. <Radio.Button value="chengdu">Chengdu</Radio.Button>
  153. </Radio.Group>
  154. </div>
  155. <div style={{marginBottom: 10}}>
  156. <span className='vice-title'>frontType: </span>
  157. <Radio.Group onChange={this.switchConfig('frontType')} defaultValue={this.state.frontType}
  158. buttonStyle="solid">
  159. <Radio.Button value="http">http</Radio.Button>
  160. <Radio.Button value="https">https</Radio.Button>
  161. <Radio.Button value="http&https">http&https</Radio.Button>
  162. </Radio.Group>
  163. </div>
  164. <div style={{marginBottom: 10}}>
  165. <span className='vice-title'>defaultDomain: </span>
  166. <Input value={this.state.defaultDomain} style={{width: 400}} disabled/>
  167. </div>
  168. <div style={{marginBottom: 10}}>
  169. <span className='vice-title'>environmentName: </span>
  170. <Radio.Group onChange={this.switchConfig('environmentName')} defaultValue={this.state.environmentName}
  171. buttonStyle="solid">
  172. <Radio.Button value="test">test</Radio.Button>
  173. <Radio.Button value="prepub">prepub</Radio.Button>
  174. <Radio.Button value="release">release</Radio.Button>
  175. </Radio.Group>
  176. </div>
  177. <div style={{marginBottom: 10}}>
  178. <span className='vice-title'>userDomain: </span>
  179. <Input value={this.state.userDomain} style={{width: 400}}
  180. onChange={this.switchConfig('userDomain')}/>
  181. </div>
  182. <div style={{marginBottom: 10}}>
  183. <span className='vice-title'>userStatus: </span>
  184. <Radio.Group onChange={this.switchConfig('userStatus')} defaultValue={this.state.userStatus}
  185. buttonStyle="solid">
  186. <Radio.Button value="open">open</Radio.Button>
  187. <Radio.Button value="close">close</Radio.Button>
  188. </Radio.Group>
  189. </div>
  190. </Panel>
  191. </Collapse>
  192. {
  193. manageUsers.includes(this.props.userID)?
  194. <div>
  195. <Button onClick={()=>{
  196. const id = idGen('group');
  197. this.ok(id);
  198. this.props.pass(id, 'group');
  199. }} type='primary'>save</Button>
  200. {
  201. this.state.showOK === true?
  202. <Icon type="check-circle" theme="twoTone" twoToneColor="#52c41a"/>
  203. :
  204. ''
  205. }
  206. </div>
  207. :
  208. this.props.trialcase?
  209. ''
  210. :
  211. <div>
  212. <Button onClick={()=>{
  213. const id = idGen('group');
  214. this.ok(id);
  215. this.props.pass(id, 'group');
  216. }} type='primary'>save</Button>
  217. {
  218. this.state.showOK === true?
  219. <Icon type="check-circle" theme="twoTone" twoToneColor="#52c41a"/>
  220. :
  221. ''
  222. }
  223. </div>
  224. }
  225. </div>
  226. )
  227. }
  228. }
  229. export default APIGroupCard;