GenerateJs.jsx 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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=${'onlineSchema'}`)
  57. // axios.get(`${genJsUrl}?schema=${'commentsSchema'}`)
  58. // axios.get(`${genJsUrl}?schema=${'order_schemaID'}`)
  59. axios.get(`${genJsUrl}?schema=${schemaID}`)
  60. .then((res) => {
  61. if (this._isMounted) {
  62. if (res.data !== '') {
  63. // console.log('js res', res.data);
  64. let graphqlJs = beautify(res.data, {indent_size: 2, space_in_empty_paren: true});
  65. // console.log('beautify graphqlJs',graphqlJs);
  66. _this.setState({
  67. graphqlJs,
  68. show: true
  69. });
  70. } else {
  71. _this.setState({
  72. show: true
  73. })
  74. }
  75. }
  76. })
  77. .catch((err) => {
  78. console.log(err);
  79. });
  80. }
  81. componentWillUnmount() {
  82. this._isMounted = false;
  83. }
  84. saveFile = () => {
  85. let {schemaName, graphqlJs} = this.state;
  86. let blob = new Blob([graphqlJs], {type: "text/plain;charset=utf-8"});
  87. saveAs(blob, `${schemaName}_graphql.js`);
  88. };
  89. render() {
  90. let {graphqlJs} = this.state;
  91. let queryList = [];
  92. let mutationList = [];
  93. let graphqlJsList = beautify(graphqlJs, {indent_size: 2, space_in_empty_paren: true});
  94. // console.log('beautify graphqlJs',graphqlJsList);
  95. graphqlJsList.split("}\n}\n").forEach((item) => {
  96. if (item.indexOf('query') > -1) queryList.push(item + "}\n}\n");
  97. else mutationList.push(item + "}\n}\n");
  98. });
  99. return (
  100. <div>
  101. <Tabs
  102. defaultActiveKey="query"
  103. tabPosition="left"
  104. >
  105. <TabPane tab="Query" key="query">
  106. <div className='download-button'>
  107. <Button type="primary" style={{float: 'right'}} onClick={() => this.saveFile()}>
  108. <FormattedMessage id="Download"/>
  109. </Button>
  110. </div>
  111. <br/>
  112. {this.state.show ?
  113. <GraphqlJs graphqlList={queryList}/>
  114. :
  115. <Loading/>
  116. }
  117. </TabPane>
  118. <TabPane tab="Mutation" key="mutation">
  119. <div className='download-button'>
  120. <Button type="primary" style={{float: 'right'}} onClick={() => this.saveFile()}>
  121. <FormattedMessage id="Download"/>
  122. </Button>
  123. </div>
  124. <br/>
  125. {this.state.show ?
  126. <GraphqlJs graphqlList={mutationList}/>
  127. :
  128. <Loading/>
  129. }
  130. </TabPane>
  131. </Tabs>
  132. <div>
  133. <BackTop/>
  134. </div>
  135. </div>
  136. )
  137. }
  138. }
  139. export default GenerateJs;
  140. const GraphqlJs = (props) => {
  141. return (
  142. <div>
  143. <pre>
  144. <code>
  145. {props.graphqlList}
  146. </code>
  147. </pre>
  148. </div>
  149. );
  150. };
  151. const Loading = () => {
  152. return (
  153. <div style={{width: '100%', height: window.innerHeight - 164}}>
  154. <Spin
  155. tip={'loading...'}
  156. style={{
  157. position: 'relative',
  158. top: '50%',
  159. left: '50%',
  160. transform: 'translate(-50%,-50%)'
  161. }}/>
  162. </div>
  163. );
  164. };