import React, {Component} from 'react'; import {FormattedMessage} from 'react-intl'; import axios from 'axios'; import {BackTop, Tabs, Button, Spin, Alert} from 'antd'; import saveAs from 'file-saver'; import beautify from 'js-beautify'; import {genJsUrl} from '../../../../../config'; import './index.css'; const TabPane = Tabs.TabPane; axios.defaults.withCredentials = true; class GenerateJs extends Component { constructor(props) { super(props); this.state = { schemaID: props.schemaID, schemaName: props.schemaName, graphqlJs: '', show: false }; } componentDidMount() { this._isMounted = true; let _this = this; let {schemaID} = this.state; axios.get(`${genJsUrl}?schema=${schemaID}`) .then((res) => { if (this._isMounted) { if (res.data !== '') { // console.log('js res', res.data); let graphqlJs = beautify(res.data, {indent_size: 2, space_in_empty_paren: true}); // console.log('beautify graphqlJs',graphqlJs); _this.setState({ graphqlJs, show: true }); } else { _this.setState({ show: true }) } } }) .catch((err) => { console.log(err); }); } componentWillReceiveProps(next) { this.setState({ schemaID: next.schemaID, }); } componentDidUpdate() { this._isMounted = true; let _this = this; let {schemaID} = this.state; // axios.get(`${genJsUrl}?schema=${'onlineSchema'}`) // axios.get(`${genJsUrl}?schema=${'commentsSchema'}`) // axios.get(`${genJsUrl}?schema=${'order_schemaID'}`) axios.get(`${genJsUrl}?schema=${schemaID}`) .then((res) => { if (this._isMounted) { if (res.data !== '') { // console.log('js res', res.data); let graphqlJs = beautify(res.data, {indent_size: 2, space_in_empty_paren: true}); // console.log('beautify graphqlJs',graphqlJs); _this.setState({ graphqlJs, show: true }); } else { _this.setState({ show: true }) } } }) .catch((err) => { console.log(err); }); } componentWillUnmount() { this._isMounted = false; } saveFile = () => { let {schemaName, graphqlJs} = this.state; let blob = new Blob([graphqlJs], {type: "text/plain;charset=utf-8"}); saveAs(blob, `${schemaName}_graphql.js`); }; render() { let {graphqlJs} = this.state; let queryList = []; let mutationList = []; let graphqlJsList = beautify(graphqlJs, {indent_size: 2, space_in_empty_paren: true}); // console.log('beautify graphqlJs',graphqlJsList); graphqlJsList.split("}\n}\n").forEach((item) => { if (item.indexOf('query') > -1) queryList.push(item + "}\n}\n"); else mutationList.push(item + "}\n}\n"); }); return (
{props.graphqlList}