|
|
@@ -1,7 +1,9 @@
|
|
|
import React, {Component} from 'react';
|
|
|
|
|
|
-import {Select, Input, Icon, Button, notification} from 'antd';
|
|
|
-import gql from "graphql-tag";
|
|
|
+import {Select, Input, Icon, Button, notification, Spin} from 'antd';
|
|
|
+import {UPDATE_SCHEMA, SHOW_SCHEMA, SHOW_TABLE} from "../gql";
|
|
|
+
|
|
|
+import {Mutation, Query} from "react-apollo";
|
|
|
|
|
|
const Option = Select.Option;
|
|
|
|
|
|
@@ -21,6 +23,9 @@ class SchemaChange extends Component {
|
|
|
}
|
|
|
|
|
|
|
|
|
+ // 下面的 handlexxxx 全是 state 内部方法,用于操作视图
|
|
|
+ // cache 仅在提交删除整体使用
|
|
|
+
|
|
|
handleNameChange = (index) => {
|
|
|
return (e) => {
|
|
|
let columns = this.state.columns;
|
|
|
@@ -51,7 +56,7 @@ class SchemaChange extends Component {
|
|
|
handleTypeNew = (value) => {
|
|
|
if (this.state.newColName !== '') {
|
|
|
let columns = this.state.columns;
|
|
|
- columns.push({name: this.state.newColName, type: value, desc: 'description'});
|
|
|
+ columns.push({name: this.state.newColName, type: value, description: 'description'});
|
|
|
this.setState({
|
|
|
columns,
|
|
|
newColName: '',
|
|
|
@@ -68,7 +73,7 @@ class SchemaChange extends Component {
|
|
|
handleDescChange = (index) => {
|
|
|
return (value) => {
|
|
|
let columns = this.state.columns;
|
|
|
- columns[index].desc = value;
|
|
|
+ columns[index].description = value;
|
|
|
this.setState({
|
|
|
columns
|
|
|
});
|
|
|
@@ -97,74 +102,88 @@ class SchemaChange extends Component {
|
|
|
render() {
|
|
|
return (
|
|
|
<div style={{margin: 20}}>
|
|
|
- <div style={{marginBottom: 20}}>
|
|
|
- <span className='table-title'> Table name</span>
|
|
|
- <Input value={this.state.currentTable} placeholder="table_name" style={{width: 120}} onChange={(e) => {
|
|
|
- this.setState({currentTable: e.target.value})
|
|
|
- }}/>
|
|
|
-
|
|
|
- </div>
|
|
|
-
|
|
|
- <div style={{marginBottom: 20}}>
|
|
|
- <span className='table-title'> Table remark</span>
|
|
|
- <Input value={this.state.remark} placeholder="table_remark" style={{width: 250}} onChange={(e) => {
|
|
|
- this.setState({remark: e.target.value})
|
|
|
- }}/>
|
|
|
- </div>
|
|
|
-
|
|
|
- <div>
|
|
|
- <span className='table-title'> Table columns</span>
|
|
|
- <span className='column-title'>name</span>
|
|
|
- <span className='column-title'>type</span>
|
|
|
- <span className='column-title'>description</span>
|
|
|
- {
|
|
|
- this.state.columns.map((col, index) =>
|
|
|
- <div key={index} style={{marginBottom: 3}}>
|
|
|
- <Input style={{width: 120, marginRight: 10}} value={col.name} onChange={this.handleNameChange(index)}/>
|
|
|
- <Select defaultValue={col.type} style={{width: 120, marginRight: 10}}
|
|
|
- onChange={this.handleTypeChange(index)}>
|
|
|
- {
|
|
|
- this.state.types.map((value) =>
|
|
|
- <Option key={Math.random()} value={value.toLowerCase()}>{value}</Option>
|
|
|
- )
|
|
|
- }
|
|
|
- </Select>
|
|
|
- <Select defaultValue={col.desc} style={{width: 120, marginRight: 10}}
|
|
|
- onChange={this.handleDescChange(index)}>
|
|
|
- {
|
|
|
- this.state.descriptions.map((value) =>
|
|
|
- <Option key={Math.random()} value={value.toLowerCase()}>{value}</Option>
|
|
|
- )
|
|
|
- }
|
|
|
- </Select>
|
|
|
- <Icon type="delete" theme="outlined" style={{cursor: 'pointer'}}
|
|
|
- onClick={this.handleColDelete(index)}/>
|
|
|
- </div>
|
|
|
- )
|
|
|
- }
|
|
|
+ <div style={{marginBottom: 20}}>
|
|
|
+ <span className='table-title'> Table name</span>
|
|
|
+ <Input value={this.state.currentTable} placeholder="table_name" style={{width: 120}}
|
|
|
+ onChange={(e) => {
|
|
|
+ this.setState({currentTable: e.target.value})
|
|
|
+ }}/>
|
|
|
+
|
|
|
+ </div>
|
|
|
+
|
|
|
+ <div style={{marginBottom: 20}}>
|
|
|
+ <span className='table-title'> Table remark</span>
|
|
|
+ <Input value={this.state.remark} placeholder="table_remark" style={{width: 250}} onChange={(e) => {
|
|
|
+ this.setState({remark: e.target.value})
|
|
|
+ }}/>
|
|
|
+ </div>
|
|
|
+
|
|
|
<div>
|
|
|
- <Input placeholder="column_name" style={{width: 120, marginRight: 10}} onChange={this.handleNameNew}
|
|
|
- value={this.state.newColName}/>
|
|
|
- <Select defaultValue="type" style={{width: 120, marginRight: 10}} onChange={this.handleTypeNew}
|
|
|
- value={this.state.newColType}>
|
|
|
- {
|
|
|
- this.state.types.map((value) =>
|
|
|
- <Option key={Math.random()} value={value.toLowerCase()}>{value}</Option>
|
|
|
- )
|
|
|
- }
|
|
|
- </Select>
|
|
|
+ <span className='table-title'> Table columns</span>
|
|
|
+ <span className='column-title'>name</span>
|
|
|
+ <span className='column-title'>type</span>
|
|
|
+ <span className='column-title'>description</span>
|
|
|
+ {
|
|
|
+ this.state.columns.map((col, index) =>
|
|
|
+ <div key={index} style={{marginBottom: 3}}>
|
|
|
+ <Input style={{width: 120, marginRight: 10}} value={col.name}
|
|
|
+ onChange={this.handleNameChange(index)}/>
|
|
|
+ <Select defaultValue={col.type} style={{width: 120, marginRight: 10}}
|
|
|
+ onChange={this.handleTypeChange(index)}>
|
|
|
+ {
|
|
|
+ this.state.types.map((value) =>
|
|
|
+ <Option key={Math.random()} value={value.toLowerCase()}>{value}</Option>
|
|
|
+ )
|
|
|
+ }
|
|
|
+ </Select>
|
|
|
+ <Select defaultValue={col.description} style={{width: 120, marginRight: 10}}
|
|
|
+ onChange={this.handleDescChange(index)}>
|
|
|
+ {
|
|
|
+ this.state.descriptions.map((value) =>
|
|
|
+ <Option key={Math.random()} value={value.toLowerCase()}>{value}</Option>
|
|
|
+ )
|
|
|
+ }
|
|
|
+ </Select>
|
|
|
+ <Icon type="delete" theme="outlined" style={{cursor: 'pointer'}}
|
|
|
+ onClick={this.handleColDelete(index)}/>
|
|
|
+ </div>
|
|
|
+ )
|
|
|
+ }
|
|
|
+ <div>
|
|
|
+ <Input placeholder="column_name" style={{width: 120, marginRight: 10}}
|
|
|
+ onChange={this.handleNameNew}
|
|
|
+ value={this.state.newColName}/>
|
|
|
+ <Select defaultValue="type" style={{width: 120, marginRight: 10}} onChange={this.handleTypeNew}
|
|
|
+ value={this.state.newColType}>
|
|
|
+ {
|
|
|
+ this.state.types.map((value) =>
|
|
|
+ <Option key={Math.random()} value={value.toLowerCase()}>{value}</Option>
|
|
|
+ )
|
|
|
+ }
|
|
|
+ </Select>
|
|
|
+ </div>
|
|
|
+ </div>
|
|
|
+ <div style={{marginTop: 20}}>
|
|
|
+ <UpdateTableButton
|
|
|
+ currentTable={this.state.currentTable}
|
|
|
+ columns={this.state.columns}
|
|
|
+ remark={this.state.remark}
|
|
|
+ currentTableIndex={this.props.currentTableIndex}
|
|
|
+ schemaID={this.props.schemaID}
|
|
|
+ userID={this.props.userID}
|
|
|
+ />
|
|
|
+ {
|
|
|
+ // undefined的时候 上层未传进来 currentTable === 'add',为的是输入框内不默认显示 add
|
|
|
+ this.state.currentTable === ''? '' : this.state.currentTable === undefined ? '' :
|
|
|
+ <DeleteTableButton
|
|
|
+ currentTable={this.state.currentTable}
|
|
|
+ currentTableIndex={this.props.currentTableIndex}
|
|
|
+ schemaID={this.props.schemaID}
|
|
|
+ userID={this.props.userID}
|
|
|
+ />
|
|
|
+ }
|
|
|
+
|
|
|
</div>
|
|
|
- </div>
|
|
|
- <div style={{marginTop: 10}}>
|
|
|
- <Button type="primary" onClick={() => {
|
|
|
- this.props.addTable(this.state.columns, this.state.remark, this.state.currentTable, this.state.currentSchema);
|
|
|
- }}>
|
|
|
- SUBMIT
|
|
|
- </Button>
|
|
|
- <Button style={{marginLeft: 10}} type="danger" onClick={() => {
|
|
|
- this.props.deleteTable(this.state.currentTable, this.state.currentSchema);
|
|
|
- }}>DELETE</Button>
|
|
|
- </div>
|
|
|
</div>
|
|
|
)
|
|
|
}
|
|
|
@@ -173,47 +192,226 @@ class SchemaChange extends Component {
|
|
|
export default SchemaChange;
|
|
|
|
|
|
|
|
|
-// 先搞定 showSchema 再来搞这个
|
|
|
+class UpdateTableButton extends Component {
|
|
|
+
|
|
|
+ render() {
|
|
|
+ let schemaID = this.props.schemaID;
|
|
|
+ let userID = this.props.userID;
|
|
|
+
|
|
|
+ let varobj = {
|
|
|
+ id: schemaID,
|
|
|
+ updatedAt: new Date().getTime(),
|
|
|
+ schemaState: 'updated-update-table',
|
|
|
+ };
|
|
|
+
|
|
|
+ return (
|
|
|
+ <Query query={SHOW_TABLE} variables={{schema_id: schemaID}}>
|
|
|
+
|
|
|
+ {
|
|
|
+ ({loading, error, data}) => {
|
|
|
+ if (loading)
|
|
|
+ return <Spin style={{marginLeft: 30, marginTop: 10}}/>;
|
|
|
+ if (error)
|
|
|
+ return 'error';
|
|
|
+
|
|
|
+ let schemaData = data;
|
|
|
+
|
|
|
+ return (
|
|
|
+ <Mutation
|
|
|
+ mutation={UPDATE_SCHEMA}
|
|
|
+ update={(cache, {data: {update_schema}}) => {
|
|
|
+ let data = cache.readQuery({
|
|
|
+ query: SHOW_TABLE,
|
|
|
+ variables: {schema_id: schemaID}
|
|
|
+ });
|
|
|
+ data.schema_by_id = update_schema;
|
|
|
+
|
|
|
+ let showSchemaData = cache.readQuery({
|
|
|
+ query: SHOW_SCHEMA,
|
|
|
+ variables: {user_id: userID}
|
|
|
+ });
|
|
|
+ let index = showSchemaData.schema_by_props.findIndex(obj => obj.schemaName === update_schema.schemaName);
|
|
|
+ showSchemaData.schema_by_props.splice(index, 1, update_schema);
|
|
|
|
|
|
-class UpdateSchemaButtons extends Component {
|
|
|
+ cache.writeQuery(
|
|
|
+ {
|
|
|
+ query: SHOW_TABLE,
|
|
|
+ variables: {schema_id: schemaID},
|
|
|
+ data
|
|
|
+ },
|
|
|
+ {
|
|
|
+ query: SHOW_SCHEMA,
|
|
|
+ variables: {user_id: userID},
|
|
|
+ showSchemaData
|
|
|
+ }
|
|
|
+ );
|
|
|
+ }}
|
|
|
+ >
|
|
|
|
|
|
-// update_schema(
|
|
|
-// createdAt: String
|
|
|
-// id: ID!
|
|
|
-// schemaData: String
|
|
|
-// schemaName: String
|
|
|
-// schemaState: String
|
|
|
-// updatedAt: String
|
|
|
-// user_id: String
|
|
|
-// ): Schema
|
|
|
+ {(update_schema, {loading, error}) => {
|
|
|
+ if (loading)
|
|
|
+ return <Spin style={{marginLeft: 30, marginTop: 10}}/>;
|
|
|
+ if (error)
|
|
|
+ return 'error';
|
|
|
|
|
|
+ // 更新代码
|
|
|
+ // 先 query 再 mutation,然后替换,做一个判断
|
|
|
+ let schemaCols;
|
|
|
+ if (schemaData.schema_by_id === null) schemaCols = [];
|
|
|
+ else schemaCols = JSON.parse(schemaData.schema_by_id.schemaData);
|
|
|
+ let newTable = {
|
|
|
+ name: this.props.currentTable,
|
|
|
+ remark: this.props.remark,
|
|
|
+ cols: this.props.columns
|
|
|
+ };
|
|
|
+
|
|
|
+ const index = this.props.currentTableIndex;
|
|
|
+ if (index === -2) {
|
|
|
+ schemaCols.push(newTable);
|
|
|
+ } else if (index === -1) {
|
|
|
+ console.log('进行了一次删除操作 或 未知错误,数据库信息不匹配');
|
|
|
+ } else {
|
|
|
+ schemaCols.splice(index, 1, newTable);
|
|
|
+ }
|
|
|
+
|
|
|
+ return (
|
|
|
+ <div style={{display: 'inline-block'}}>
|
|
|
+ <Button type="primary" onClick={() => {
|
|
|
+ update_schema({
|
|
|
+ variables: {
|
|
|
+ ...varobj,
|
|
|
+ schemaData: JSON.stringify(schemaCols)
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }}>
|
|
|
+ SUBMIT
|
|
|
+ </Button>
|
|
|
+ </div>
|
|
|
+ )
|
|
|
+ }}
|
|
|
+ </Mutation>
|
|
|
+ )
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ </Query>
|
|
|
+ )
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+class DeleteTableButton extends Component {
|
|
|
+ render() {
|
|
|
+ let schemaID = this.props.schemaID;
|
|
|
+ let userID = this.props.userID;
|
|
|
|
|
|
- render(){
|
|
|
let varobj = {
|
|
|
- user_id: this.props.userID,
|
|
|
+ id: schemaID,
|
|
|
updatedAt: new Date().getTime(),
|
|
|
- schemaState: 'updated',
|
|
|
- id: this.props.schemaID
|
|
|
+ schemaState: 'updated-delete-table',
|
|
|
};
|
|
|
|
|
|
- const ADD_SCHEMA = gql`
|
|
|
- mutation SCHEMA($id: ID!, $user_id: String!, $schemaName: String!, $schemaData: String!, $createdAt: String, $updatedAt: String, $schemaState: String) {
|
|
|
- update_schema(
|
|
|
- id: $id,
|
|
|
- user_id: $user_id,
|
|
|
- schemaName: $schemaName,
|
|
|
- createdAt: $createdAt,
|
|
|
- updatedAt: $updatedAt,
|
|
|
- schemaData: $schemaData,
|
|
|
- schemaState: $schemaState
|
|
|
- ) {
|
|
|
- id,
|
|
|
- schemaName
|
|
|
- }
|
|
|
- }
|
|
|
- `;
|
|
|
return (
|
|
|
- <div>1</div>
|
|
|
+ <Query query={SHOW_TABLE} variables={{schema_id: schemaID}}>
|
|
|
+
|
|
|
+ {
|
|
|
+ ({loading, error, data}) => {
|
|
|
+ if (loading)
|
|
|
+ return <Spin style={{marginLeft: 30, marginTop: 10}}/>;
|
|
|
+ if (error)
|
|
|
+ return 'error';
|
|
|
+
|
|
|
+ let schemaData = data;
|
|
|
+
|
|
|
+ return (
|
|
|
+ <Mutation
|
|
|
+ mutation={UPDATE_SCHEMA}
|
|
|
+ update={(cache, {data: {update_schema}}) => {
|
|
|
+ let data = cache.readQuery({
|
|
|
+ query: SHOW_TABLE,
|
|
|
+ variables: {schema_id: schemaID}
|
|
|
+ });
|
|
|
+ data.schema_by_id = update_schema;
|
|
|
+
|
|
|
+ let showSchemaData = cache.readQuery({
|
|
|
+ query: SHOW_SCHEMA,
|
|
|
+ variables: {user_id: userID}
|
|
|
+ });
|
|
|
+ let schemas = showSchemaData.schema_by_props;
|
|
|
+ // console.log('schemas', schemas);
|
|
|
+ let targetSchema = schemas.find(obj => obj.schemaName === update_schema.schemaName);
|
|
|
+ // console.log('targetSchema', targetSchema);
|
|
|
+ let targetSchemaIndex = schemas.findIndex(obj => obj.schemaName === update_schema.schemaName);
|
|
|
+ // console.log('targetSchemaIndex', targetSchemaIndex);
|
|
|
+ let targetTables = JSON.parse(schemas[targetSchemaIndex].schemaData);
|
|
|
+ // console.log('targetTables', targetTables);
|
|
|
+ let targetTableIndex = targetTables.findIndex(obj=> obj.name===this.props.currentTable);
|
|
|
+ // console.log('targetTableIndex', targetTableIndex);
|
|
|
+ targetTables.splice(targetTableIndex, 1);
|
|
|
+ // console.log('targetTablesAfterDelete', targetTables);
|
|
|
+ let temp = {schemaData: JSON.stringify(targetTables)};
|
|
|
+ // console.log('temp', temp);
|
|
|
+ let tempSchema = {...targetSchema, ...temp};
|
|
|
+ // console.log('tempSchema', tempSchema);
|
|
|
+ showSchemaData.schema_by_props.splice(targetSchemaIndex, 1, tempSchema);
|
|
|
+
|
|
|
+ cache.writeQuery(
|
|
|
+ {
|
|
|
+ query: SHOW_TABLE,
|
|
|
+ variables: {schema_id: schemaID},
|
|
|
+ data
|
|
|
+ },
|
|
|
+ {
|
|
|
+ query: SHOW_SCHEMA,
|
|
|
+ variables: {user_id: userID},
|
|
|
+ showSchemaData
|
|
|
+ }
|
|
|
+ );
|
|
|
+ }}
|
|
|
+ >
|
|
|
+
|
|
|
+ {(update_schema, {loading, error}) => {
|
|
|
+ if (loading)
|
|
|
+ return <Spin style={{marginLeft: 30, marginTop: 10}}/>;
|
|
|
+ if (error)
|
|
|
+ return 'error';
|
|
|
+
|
|
|
+ // 先 query 再 mutation,然后删除
|
|
|
+ let schemaCols;
|
|
|
+ if (schemaData.schema_by_id === null) schemaCols = [];
|
|
|
+ else schemaCols = JSON.parse(schemaData.schema_by_id.schemaData);
|
|
|
+
|
|
|
+ const index = this.props.currentTableIndex;
|
|
|
+ // 设置-2 标志为新建页面,防止通过 tablename 被修改导致每次 index 都为 -1 从而修改最后一个
|
|
|
+ if (index === -2) {
|
|
|
+ console.log('初始页面')
|
|
|
+ } else if (index === -1) {
|
|
|
+ console.log('进行了一次删除操作 或 未知错误,数据库信息不匹配');
|
|
|
+ } else {
|
|
|
+ schemaCols.splice(index, 1);
|
|
|
+ }
|
|
|
+
|
|
|
+ return (
|
|
|
+ <div style={{display: 'inline-block'}}>
|
|
|
+ <Button type="danger"
|
|
|
+ onClick={() => {
|
|
|
+ update_schema({
|
|
|
+ variables: {
|
|
|
+ ...varobj,
|
|
|
+ schemaData: JSON.stringify(schemaCols)
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }}>
|
|
|
+ DELETE
|
|
|
+ </Button>
|
|
|
+ </div>
|
|
|
+ )
|
|
|
+ }}
|
|
|
+ </Mutation>
|
|
|
+ )
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ </Query>
|
|
|
)
|
|
|
}
|
|
|
}
|