APIGroupCardRender.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. import React, {Component} from 'react';
  2. import {Input, Collapse, Button, Radio, Icon, Tooltip} from 'antd';
  3. import {idGen} from "../../../../../func";
  4. import {graphqlUrl, manageUsers} from "../../../../../config";
  5. import {FormattedMessage} from 'react-intl';
  6. import {ADD_APIGROUP, UPDATE_APIGROUP, UPDATE_PROJECT_GROUP, UPDATE_PROJECT_ONLY_STATUS} from "../../../../../gql";
  7. import {request} from 'graphql-request'
  8. import {removePrefix, shiftPrefix} from "../../../../../func";
  9. const Panel = Collapse.Panel;
  10. // 如需改变显示,请在此处 value 处更改
  11. // 如需添加中文,请在此处 value 处添加
  12. // eg: 'xxxx': <F.. id='xxx'/>
  13. const valueToKey = {
  14. 'groupName': 'groupName',
  15. 'environmentName': 'environmentName',
  16. 'defaultDomain': 'defaultDomain',
  17. 'frontType': 'frontType',
  18. 'userDomain': 'userDomain',
  19. 'userStatus': 'userStatus',
  20. 'region': 'region',
  21. };
  22. const toolTipTitle = {
  23. 'groupName': 'its groupName',
  24. 'environmentName': 'its environmentName',
  25. 'defaultDomain': 'its defaultDomain',
  26. 'frontType': 'its frontType',
  27. 'userDomain': 'its userDomain',
  28. 'userStatus': 'its userStatus',
  29. 'region': 'its region',
  30. };
  31. const youMustFill = {
  32. 'groupName': true,
  33. 'environmentName': true,
  34. 'defaultDomain': true,
  35. 'frontType': true,
  36. 'userDomain': false,
  37. 'userStatus': true,
  38. 'region': true,
  39. };
  40. class APIGroupCardRender extends Component {
  41. constructor(props) {
  42. super(props);
  43. if (props.group !== '' && props.group !== null) {
  44. let {groupName, environmentName, defaultDomain, frontType, userDomain, userStatus, region} = props.group;
  45. this.state = {
  46. showOK: false,
  47. groupName,
  48. environmentName,
  49. defaultDomain,
  50. frontType,
  51. userDomain,
  52. userStatus,
  53. groupRegion: region
  54. }
  55. } else {
  56. this.state = {
  57. showOK: false,
  58. groupName: 'graphql_endpoint',
  59. environmentName: 'test',
  60. defaultDomain: '',
  61. frontType: 'http&https',
  62. userDomain: '',
  63. userStatus: 'open',
  64. groupRegion: props.region === '' ? 'ap-beijing' : props.region,
  65. };
  66. }
  67. }
  68. componentWillReceiveProps(next) {
  69. if (next.group !== '' && next.group !== null) {
  70. let {groupName, environmentName, defaultDomain, frontType, userDomain, userStatus, region} = next.group;
  71. this.setState({
  72. groupName,
  73. environmentName,
  74. defaultDomain,
  75. frontType,
  76. userDomain,
  77. userStatus,
  78. groupRegion: next.region === '' ? region : next.region
  79. })
  80. } else {
  81. this.setState({
  82. groupName: 'graphql_endpoint',
  83. environmentName: 'test',
  84. defaultDomain: '',
  85. frontType: 'http&https',
  86. userDomain: '',
  87. userStatus: 'open',
  88. groupRegion: next.region === '' ? 'ap-beijing' : next.region
  89. });
  90. }
  91. };
  92. switchConfig = (label) => {
  93. return (e) => {
  94. this.setState({
  95. [label]: e.target.value
  96. })
  97. };
  98. };
  99. ok = (id, userID, projectID, cloudID, group) => {
  100. let {userStatus, userDomain, groupName, groupRegion, frontType, environmentName} = this.state;
  101. let region = shiftPrefix('ap-', groupRegion);
  102. let varObj = {
  103. id,
  104. cloud_id: cloudID,
  105. user_id: userID,
  106. userStatus,
  107. userDomain,
  108. groupName,
  109. frontType,
  110. region,
  111. environmentName,
  112. defaultDomain: '',
  113. status: '',
  114. serviceId: '',
  115. createdAt: new Date().getTime(),
  116. updatedAt: ''
  117. };
  118. if (group === '' || group === null) {
  119. request(graphqlUrl, ADD_APIGROUP, varObj).then(
  120. data => {
  121. if (data.create_apiGWGroup !== null) {
  122. this.setState({
  123. showOK: true
  124. });
  125. // 写回 project
  126. request(graphqlUrl, UPDATE_PROJECT_GROUP, {
  127. id: projectID,
  128. updatedAt: new Date().getTime(),
  129. apiGWGroup_id: id,
  130. projectStatus: 'grouped'
  131. }).then(data => {
  132. if (data.update_project !== null)
  133. this.props.stepByStep(2);
  134. });
  135. }
  136. setTimeout(() => {
  137. this.setState({
  138. showOK: false
  139. })
  140. }, 1500)
  141. }
  142. )
  143. } else {
  144. let {userStatus, userDomain, groupName, groupRegion, frontType, environmentName} = this.state;
  145. let region = shiftPrefix('ap-', groupRegion);
  146. let varObj = {
  147. id: group.id,
  148. userStatus,
  149. userDomain,
  150. groupName,
  151. frontType,
  152. region,
  153. environmentName,
  154. updatedAt: new Date().getTime()
  155. };
  156. request(graphqlUrl, UPDATE_APIGROUP, varObj).then(
  157. data => {
  158. if (data.update_apiGWGroup !== null) {
  159. this.setState({
  160. showOK: true
  161. });
  162. request(graphqlUrl, UPDATE_PROJECT_ONLY_STATUS, {
  163. id: projectID,
  164. updatedAt: new Date().getTime(),
  165. projectStatus: 'updated'
  166. }).then(data => {
  167. if(data.update_project !== null)
  168. this.props.stepByStep(2);
  169. })
  170. }
  171. setTimeout(() => {
  172. this.setState({
  173. showOK: false
  174. })
  175. }, 1500)
  176. }
  177. )
  178. }
  179. };
  180. render() {
  181. const customPanelStyle = {
  182. background: '#f7f7f7',
  183. borderRadius: 4,
  184. marginBottom: 24,
  185. border: 0,
  186. overflow: 'hidden',
  187. };
  188. return (
  189. <div>
  190. <div style={{marginBottom: 10}}>
  191. <span className='vice-title'>
  192. {
  193. youMustFill['groupName'] ?
  194. <span
  195. style={{color: 'red', display: 'inline', marginRight: 10}}>*</span>
  196. :
  197. ''
  198. }
  199. <FormattedMessage id={valueToKey['groupName']}/>
  200. &nbsp;
  201. <Tooltip placement="top" title={toolTipTitle['groupName']}>
  202. <Icon type="question-circle"/>
  203. </Tooltip>
  204. </span>
  205. <Input value={this.state.groupName} style={{width: 400}}
  206. onChange={this.switchConfig('groupName')}/>
  207. </div>
  208. <div style={{marginBottom: 10}}>
  209. <span className='vice-title'>
  210. {
  211. youMustFill['region'] ?
  212. <span
  213. style={{color: 'red', display: 'inline', marginRight: 10}}>*</span>
  214. :
  215. ''
  216. }
  217. <FormattedMessage id={valueToKey['region']}/>
  218. &nbsp;
  219. <Tooltip placement="top" title={toolTipTitle['region']}>
  220. <Icon type="question-circle"/>
  221. </Tooltip>
  222. </span>
  223. <Radio.Group onChange={(e) => {
  224. // 如果不想一变所有都变,就将 onchange 改成
  225. // onChange={this.switchConfig('groupRegion')}
  226. this.props.switchRegion(e);
  227. }} value={removePrefix('ap-', this.state.groupRegion)} buttonStyle="solid">
  228. <Radio.Button value="guangzhou"><FormattedMessage id="Guangzhou"/></Radio.Button>
  229. <Radio.Button value="shanghai"><FormattedMessage id="Shanghai"/></Radio.Button>
  230. <Radio.Button value="beijing"><FormattedMessage id="Beijing"/></Radio.Button>
  231. <Radio.Button value="chengdu"><FormattedMessage id="Chengdu"/></Radio.Button>
  232. </Radio.Group>
  233. </div>
  234. <div style={{marginBottom: 10}}>
  235. <span className='vice-title'>
  236. {
  237. youMustFill['environmentName'] ?
  238. <span
  239. style={{color: 'red', display: 'inline', marginRight: 10}}>*</span>
  240. :
  241. ''
  242. }
  243. <FormattedMessage id={valueToKey['environmentName']}/>
  244. &nbsp;
  245. <Tooltip placement="top" title={toolTipTitle['environmentName']}>
  246. <Icon type="question-circle"/>
  247. </Tooltip>
  248. </span>
  249. <Radio.Group onChange={this.switchConfig('environmentName')}
  250. defaultValue={this.state.environmentName}
  251. buttonStyle="solid">
  252. <Radio.Button value="test"><FormattedMessage id="test"/></Radio.Button>
  253. <Radio.Button value="prepub"><FormattedMessage id="prepub"/></Radio.Button>
  254. <Radio.Button value="release"><FormattedMessage id="release"/></Radio.Button>
  255. </Radio.Group>
  256. </div>
  257. <div style={{marginBottom: 10}}>
  258. <span className='vice-title'>
  259. {
  260. youMustFill['frontType'] ?
  261. <span style={{
  262. color: 'red',
  263. display: 'inline',
  264. marginRight: 10
  265. }}>*</span>
  266. :
  267. ''
  268. }
  269. <FormattedMessage id={valueToKey['frontType']}/>
  270. &nbsp;
  271. <Tooltip placement="top" title={toolTipTitle['frontType']}>
  272. <Icon type="question-circle"/>
  273. </Tooltip>
  274. </span>
  275. <Radio.Group onChange={this.switchConfig('frontType')}
  276. defaultValue={this.state.frontType}
  277. buttonStyle="solid">
  278. <Radio.Button value="http">http</Radio.Button>
  279. <Radio.Button value="https">https</Radio.Button>
  280. <Radio.Button value="http&https">http&https</Radio.Button>
  281. </Radio.Group>
  282. </div>
  283. <Collapse bordered={false}>
  284. <Panel header={<FormattedMessage id="Want more options?"/>}
  285. style={customPanelStyle}>
  286. {/*<div style={{marginBottom: 10}}>*/}
  287. {/*<span className='vice-title'>{valueToKey['defaultDomain']}*/}
  288. {/*&nbsp;*/}
  289. {/*<Tooltip placement="top" title={toolTipTitle['defaultDomain']}>*/}
  290. {/*<Icon type="question-circle"/>*/}
  291. {/*</Tooltip>*/}
  292. {/*</span>*/}
  293. {/*<Input value={this.state.defaultDomain} style={{width: 400}} disabled/>*/}
  294. {/*</div>*/}
  295. <div style={{marginBottom: 10}}>
  296. <span className='vice-title'>
  297. {
  298. youMustFill['userDomain'] ?
  299. <span style={{color: 'red', display: 'inline', marginRight: 10}}>*</span>
  300. :
  301. ''
  302. }
  303. <FormattedMessage id={valueToKey['userDomain']}/>
  304. &nbsp;
  305. <Tooltip placement="top" title={toolTipTitle['userDomain']}>
  306. <Icon type="question-circle"/>
  307. </Tooltip>
  308. </span>
  309. <Input value={this.state.userDomain} style={{width: 400}}
  310. onChange={this.switchConfig('userDomain')}/>
  311. </div>
  312. {/*<div style={{marginBottom: 10}}>*/}
  313. {/*<span className='vice-title'>{valueToKey['userStatus']}*/}
  314. {/*&nbsp;*/}
  315. {/*<Tooltip placement="top" title={toolTipTitle['userStatus']}>*/}
  316. {/*<Icon type="question-circle"/>*/}
  317. {/*</Tooltip>*/}
  318. {/*</span>*/}
  319. {/*<Radio.Group onChange={this.switchConfig('userStatus')} defaultValue={this.state.userStatus}*/}
  320. {/*buttonStyle="solid">*/}
  321. {/*<Radio.Button value="open">open</Radio.Button>*/}
  322. {/*<Radio.Button value="close">close</Radio.Button>*/}
  323. {/*</Radio.Group>*/}
  324. {/*</div>*/}
  325. </Panel>
  326. </Collapse>
  327. {
  328. manageUsers.includes(this.props.userID) ?
  329. <div>
  330. <div>
  331. {
  332. this.props.stepAllShow ? '' :
  333. <Button onClick={() => {
  334. this.props.stepByStep(0)
  335. }}><FormattedMessage id="previous-step"/></Button>
  336. }
  337. <Button onClick={() => {
  338. const id = idGen('group');
  339. this.ok(id, this.props.userID, this.props.projectID, this.props.cloudID, this.props.group);
  340. }} type='primary'><FormattedMessage id="save"/></Button>
  341. </div>
  342. {
  343. this.state.showOK === true ?
  344. <Icon type="check-circle" theme="twoTone" twoToneColor="#52c41a"/>
  345. :
  346. ''
  347. }
  348. </div>
  349. :
  350. this.props.trialcase ?
  351. ''
  352. :
  353. <div>
  354. <div>
  355. {
  356. this.props.stepAllShow ? '' :
  357. <Button onClick={() => {
  358. this.props.stepByStep(0)
  359. }}><FormattedMessage id="previous-step"/></Button>
  360. }
  361. <Button onClick={() => {
  362. const id = idGen('group');
  363. this.ok(id, this.props.userID, this.props.projectID, this.props.cloudID, this.props.group);
  364. }} type='primary'><FormattedMessage id="save"/></Button>
  365. </div>
  366. {
  367. this.state.showOK === true ?
  368. <Icon type="check-circle" theme="twoTone"
  369. twoToneColor="#52c41a"/>
  370. :
  371. ''
  372. }
  373. </div>
  374. }
  375. </div>
  376. )
  377. }
  378. }
  379. export default APIGroupCardRender;