GenerateJs.jsx 5.8 KB

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