GenerateJs.jsx 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. import React, {Component} from 'react';
  2. import axios from 'axios';
  3. import {BackTop, Tabs, Button, Spin} from 'antd';
  4. import saveAs from 'file-saver';
  5. import beautify from 'js-beautify';
  6. import './index.css';
  7. const TabPane = Tabs.TabPane;
  8. axios.defaults.withCredentials = true;
  9. class GenerateJs extends Component {
  10. constructor(props) {
  11. super(props);
  12. this.state = {
  13. schemaID:props.schemaID,
  14. schemaName:props.schemaName,
  15. graphqlJs:'',
  16. show: false
  17. };
  18. }
  19. componentDidMount() {
  20. this._isMounted = true;
  21. let requestUrl = 'http://123.206.193.98:8999/graphql/genjs';
  22. let _this = this;
  23. let {schemaID} = this.state;
  24. axios.get(`${requestUrl}?schema=${schemaID}`)
  25. .then((res) => {
  26. if(this._isMounted){
  27. if (res.data !== '') {
  28. // console.log('js res', res.data);
  29. let graphqlJs = beautify(res.data, { indent_size: 2, space_in_empty_paren: true });
  30. // console.log('beautify graphqlJs',graphqlJs);
  31. _this.setState({
  32. graphqlJs,
  33. show: true
  34. });
  35. } else {
  36. _this.setState({
  37. show: true
  38. })
  39. }
  40. }
  41. })
  42. .catch((err) => {
  43. console.log(err);
  44. });
  45. }
  46. componentWillReceiveProps(next) {
  47. this.setState({
  48. schemaID: next.schemaID,
  49. });
  50. }
  51. componentDidUpdate() {
  52. this._isMounted = true;
  53. let requestUrl = 'http://123.206.193.98:8999/graphql/genjs';
  54. let _this = this;
  55. let {schemaID} = this.state;
  56. axios.get(`${requestUrl}?schema=${schemaID}`)
  57. .then((res) => {
  58. if(this._isMounted){
  59. if (res.data !== '') {
  60. // console.log('js res', res.data);
  61. let graphqlJs = beautify(res.data, { indent_size: 2, space_in_empty_paren: true });
  62. // console.log('beautify graphqlJs',graphqlJs);
  63. _this.setState({
  64. graphqlJs,
  65. show: true
  66. });
  67. } else {
  68. _this.setState({
  69. show: true
  70. })
  71. }
  72. }
  73. })
  74. .catch((err) => {
  75. console.log(err);
  76. });
  77. }
  78. componentWillUnmount() {
  79. this._isMounted = false;
  80. }
  81. saveFile =()=>{
  82. let {schemaName,graphqlJs} = this.state;
  83. let blob = new Blob([graphqlJs], {type: "text/plain;charset=utf-8"});
  84. saveAs(blob, `${schemaName}_graphql.js`);
  85. };
  86. render() {
  87. let {graphqlJs} = this.state;
  88. let queryList = [];
  89. let mutationList = [];
  90. let graphqlJsList = beautify(graphqlJs, { indent_size: 2, space_in_empty_paren: true });
  91. // console.log('beautify graphqlJs',graphqlJsList);
  92. graphqlJsList.split("}\n}\n").forEach((item) => {
  93. if (item.indexOf('query')>-1) queryList.push(item+"}\n}\n");
  94. else mutationList.push(item+"}\n}\n");
  95. });
  96. // console.log('queryList',queryList);
  97. // console.log('mutationList',mutationList);
  98. return (
  99. <div>
  100. <Tabs
  101. defaultActiveKey="query"
  102. tabPosition="left"
  103. >
  104. <TabPane tab="Query" key="query">
  105. <div className='download-button'>
  106. <Button type="primary" style={{float:'right'}} onClick={()=>this.saveFile()}>Download</Button>
  107. </div>
  108. <br/>
  109. {this.state.show ?
  110. <GraphqlJs graphqlList={queryList}/>
  111. :
  112. <Loading/>
  113. }
  114. </TabPane>
  115. <TabPane tab="Mutation" key="mutation">
  116. <div className='download-button'>
  117. <Button type="primary" style={{float:'right'}} onClick={()=>this.saveFile()}>Download</Button>
  118. </div>
  119. <br/>
  120. {this.state.show ?
  121. <GraphqlJs graphqlList={mutationList}/>
  122. :
  123. <Loading/>
  124. }
  125. </TabPane>
  126. </Tabs>
  127. <div>
  128. <BackTop />
  129. </div>
  130. </div>
  131. )
  132. }
  133. }
  134. export default GenerateJs;
  135. class GraphqlJs extends Component {
  136. render() {
  137. return (
  138. <div>
  139. <pre>
  140. <code>
  141. {this.props.graphqlList}
  142. </code>
  143. </pre>
  144. </div>
  145. );
  146. }
  147. }
  148. class Loading extends Component {
  149. render() {
  150. return (
  151. <div style={{width: '100%', height: window.innerHeight - 164}}>
  152. <Spin
  153. tip={'loading...'}
  154. style={{
  155. position: 'relative',
  156. top: '50%',
  157. left: '50%',
  158. transform: 'translate(-50%,-50%)'
  159. }}/>
  160. </div>
  161. );
  162. }
  163. }