import React, {Component} from 'react';
import {Row, Col, Input, Icon, Button, Spin} from 'antd';
import './index.css';
import SchemaChange from './change/SchemaChange';
import {Mutation, Query} from "react-apollo";
import {SHOW_SCHEMA, ADD_SCHEMA, DELETE_SCHEMA, SHOW_TABLE} from './gql'
const Search = Input.Search;
const idGen = (kind) => {
return kind + '_' + Date.now() + '_' + Math.random().toString().slice(-8);
};
class Schema extends Component {
constructor(props) {
super(props);
this.state = {
currentSchema: '',
currentTable: '',
schemaID: '',
userID: 'xy_1'
};
}
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 : '';
switchSchema = (name, id) => {
this.setState({
currentSchema: name,
schemaID: id
});
};
switchTable = (table) => {
this.setState({
currentTable: table
})
};
render() {
return (
{this.state.currentSchema}
{
this.state.currentTable === 'add' ?
:
{
({loading, error, data}) => {
if (loading)
return ;
if (error)
return 'error';
if (data.schema_by_id === null) data = [];
else data = JSON.parse(data.schema_by_id.schemaData);
return (
obj.name === this.state.currentTable)}
columns={this.findColumns(data)}
remark={this.findRemark(data)}
schemaID={this.state.schemaID}
userID={this.state.userID}
/>
)
}
}
}
)
}
}
export default Schema;
class AddSchemaInput extends Component {
render() {
let userID = this.props.userID;
let varobj = {
id: idGen('schema'),
user_id: userID,
createdAt: new Date().getTime(),
updatedAt: '',
schemaState: 'create',
schemaData: JSON.stringify([])
};
return (
{
let data = cache.readQuery({query: SHOW_SCHEMA, variables: {user_id: userID}});
data.schema_by_props.push(create_schema);
cache.writeQuery({
query: SHOW_SCHEMA,
variables: {user_id: userID},
data
});
}}
>
{(create_schema, {loading, error}) => {
if (loading)
return ;
if (error)
return 'error';
return (
{
this.setState({
switch: true,
currentSchema: value,
currentTable: ''
});
create_schema({
variables: {
...varobj,
schemaName: value
}
});
}}
/>
)
}}
)
}
}
class ShowSchemaList extends Component {
render() {
let userID = this.props.userID;
return (
{
({loading, error, data}) => {
if (loading) {
return
}
if (error) {
return 'error!';
}
if (data.schema_by_props.length === 0)
return (
no schemas, create one
);
else {
return (
{
data.schema_by_props.map((schema) => {
return
{
this.props.switchSchema(schema.schemaName, schema.id);
}}>
{schema.schemaName}
{
JSON.parse(schema.schemaData).map(table => (
{
this.props.switchTable(table.name)
}}
key={table.name}
className='show'>
{table.name}
{table.remark}
))
}
})
}
);
}
}
}
)
}
}
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 (
)
}
}