import React, {Component} from 'react'; import {Row, Col, Input, Icon, Button, Spin} from 'antd'; import './index.css'; import {Mutation, Query} from "react-apollo"; import gql from "graphql-tag"; import {SHOW_SCHEMA, DELETE_SCHEMA, SHOW_TABLE} from '../../gql' import Table from "./Table"; class Schema extends Component { constructor(props) { super(props); console.log('Schema props',props); this.state = { currentTable: props.schemaID ?'':'add', // default schemaID and schemaName schemaID: props.schemaID || props.history.location.state.schemaID, schemaName: props.schemaName || props.history.location.state.schemaName, }; } switchTable = (table) => { this.setState({ currentTable: table }) }; findColumns = data => this.state.currentTable === '' ? [] : data.find(table => table.name === this.state.currentTable) ? data.find(table => table.name === this.state.currentTable).cols : []; findRemark = data => this.state.currentTable === '' ? '' : data.find(table => table.name === this.state.currentTable) ? data.find(table => table.name === this.state.currentTable).remark : ''; componentWillReceiveProps(next) { this.setState({ currentTable: '', schemaID: next.schemaID, schemaName: next.schemaName }); } render() { let userID = this.props.userID; console.log('this.state.currentTable',this.state.currentTable); return ( { ({loading, error, data}) => { if (loading) { return } if (error) { return 'error!'; } // let schemaName = data.schema_by_id.schemaName; if (data.schema_by_id === null) data = []; else data = JSON.parse(data.schema_by_id.schemaData); return (
{ this.state.currentTable === '' ?
{ this.setState({ currentTable: '' }) }}> {this.state.schemaName}
Name Remark { this.setState({ currentTable: 'add' }) }}>
{ data.map(table => ( this.switchTable(table.name)}>{table.name} {table.remark} )) }
: this.state.currentTable === 'add' ? :
obj.name === this.state.currentTable)} columns={this.findColumns(data)} remark={this.findRemark(data)} schemaID={this.state.schemaID} schemaName={this.state.schemaName} userID={userID} /> } ); } } ) } } export default Schema; // 备用代码 class DeleteSchemaButton extends Component { constructor(props) { super(props); this.state = { schemaName: props.schemaName } } render() { let userID = this.props.userID; return ( { let data = cache.readQuery({query: SHOW_SCHEMA, variables: {user_id: userID}}); data.schema_by_props.splice(data.schema_by_props.findIndex(obj => obj.schemaName === this.state.schemaName), 1); cache.writeQuery({ query: SHOW_SCHEMA, variables: {user_id: userID}, data }); }} > {(delete_schema, {loading, error}) => { if (error) return 'error'; if (loading) return ; return (