Explorar o código

Merge remote-tracking branch 'origin/master'

# Conflicts:
#	src/components/common/schema/Column.jsx
Csy817 %!s(int64=7) %!d(string=hai) anos
pai
achega
d093d72b36

+ 1 - 2
src/components/app/App.jsx

@@ -287,7 +287,6 @@ class GraphqlSidebar extends Component {
         let _this = this;
         axios.get(this.state.getID_url)
             .then((res) => {
-                console.log(res);
                 if (res.data !== '') {
                     _this.setState({
                         userID: res.data,
@@ -340,7 +339,7 @@ class GraphqlSidebar extends Component {
                                         <Menu.Item key="e-commerce"><Link
                                             to="/graphql-service/trial-case/e-commerce">e-commerce</Link></Menu.Item>
                                         <Menu.Item key="keep accounts"><Link
-                                            to="/graphql-service/trial-case/keep accounts">keep
+                                            to="/graphql-service/trial-case/keep-accounts">keep
                                             accounts</Link></Menu.Item>
                                         <Menu.Item key="appointment"><Link
                                             to="/graphql-service/trial-case/appointment">appointment</Link></Menu.Item>

+ 19 - 11
src/components/common/deploy/Deploy.jsx

@@ -31,22 +31,30 @@ class Deploy extends Component {
             tencentCloudID: '',
             aliyunCloudID: '',
             amazonCloudID: '',
-            // todo: 该schema应该由props传入,无论是哪一层,反正不是这一层。(路由传入吧)
-            // 有fc的schema -- 测试数据
-            // schemaID: 'schema_1542243424669_92094965',
-            // 无fc的schema -- 测试数据
-            schemaID: 'schema_1542967129456_05958413',
             schemaName: '',
             tencentFC: '',
             aliyunFC: '',
-            amazonFC: ''
+            amazonFC: '',
+            schemaID: props.schemaID
         };
+        this.hello();
+    }
+
+    componentWillReceiveProps(next) {
+        this.setState({
+            schemaID: next.schemaID
+        }, this.hello)
+    }
 
+    hello = () => {
         request('http://123.206.193.98:3000/graphql', SHOW_FC, {schema_id: this.state.schemaID}).then(
+            // 根据 schemaID 查询 fc 是否成功
             data => {
                 request('http://123.206.193.98:3000/graphql', SEARCH_SCHEMA, {id: this.state.schemaID}).then(
+                    // 根据 schemaID 查询 schemaName
                     _data => {
                         if (data.fc_by_props.length === 0) {
+                            // 如果没有 fc,根据 userID 查 cloudId
                             request('http://123.206.193.98:3000/graphql', SHOW_CLOUD, {user_id: this.props.userID}).then(
                                 __data => {
                                     __data.cloud_by_props.forEach(cloud => {
@@ -82,6 +90,7 @@ class Deploy extends Component {
                                 }
                             );
                         } else {
+                            // 如果有 fc, 则获取 cloudID
                             data.fc_by_props.forEach(cloud => {
                                 switch (cloud.cloud_id.cloudName) {
                                     case 'tencent':
@@ -114,16 +123,15 @@ class Deploy extends Component {
                 );
             }
         );
-    }
+    };
+
 
     render() {
         const contentListNoTitle = {
             tencent: <TencentConfig cloudID={this.state.tencentCloudID} fc={this.tencentFC}
                                     schemaName={this.state.schemaName} userID={this.props.userID}/>,
-            aliyun: <AliConfig cloudID={this.state.aliyunCloudID} fc={this.aliyunFC} schemaName={this.state.schemaName}
-                               userID={this.props.userID}/>,
-            amazon: <AmazonConfig cloudID={this.state.amazonCloudID} fc={this.amazonFC}
-                                  schemaName={this.state.schemaName} userID={this.props.userID}/>,
+            aliyun: <AliConfig/>,
+            amazon: <AmazonConfig/>,
         };
 
         return (

+ 0 - 463
src/components/common/schema/Column.jsx

@@ -1,463 +0,0 @@
-import React, {Component} from 'react';
-
-import {Layout,Select, Input, Icon, Button, notification, Spin} from 'antd';
-import {UPDATE_SCHEMA, SHOW_SCHEMA, SHOW_TABLE} from "../../gql";
-import gql from "graphql-tag";
-
-import {Mutation, Query} from "react-apollo";
-import {getCookie} from "../../cookie";
-
-const Option = Select.Option;
-const {Content} = Layout;
-
-class Column extends Component {
-    constructor(props) {
-        super(props);
-        console.log('column props',props);
-        this.state = {
-            currentSchema: props.currentSchema || [{}],
-            currentTable: props.currentTable || ' ',
-            remark: props.remark || '',
-            columns: props.columns || [],
-            newColName: '',
-            newColType: 'type',
-            types: ['ID', 'String', 'Int', 'Float', 'Boolean', 'DateTime'],
-            descriptions: ['description', 'key', 'non-null', 'non-null-list', 'list']
-        }
-    }
-
-
-    // 下面的 handlexxxx 全是 state 内部方法,用于操作视图
-    // cache 仅在提交删除整体使用
-
-    handleNameChange = (index) => {
-        return (e) => {
-            let columns = this.state.columns;
-            columns[index].name = e.target.value;
-            this.setState({
-                columns
-            })
-        };
-    };
-
-    handleNameNew = (e) => {
-        this.setState({
-            newColName: e.target.value,
-            showNewColumn: false
-        })
-    };
-
-    handleTypeChange = (index) => {
-        return (value) => {
-            let columns = this.state.columns;
-            columns[index].type = value;
-            this.setState({
-                columns
-            });
-        }
-    };
-
-    handleTypeNew = (value) => {
-        if (this.state.newColName !== '') {
-            let columns = this.state.columns;
-            columns.push({name: this.state.newColName, type: value, description: 'description'});
-            this.setState({
-                columns,
-                newColName: '',
-                newColType: 'type'
-            })
-        } else {
-            notification['warning']({
-                message: 'Notification',
-                description: 'Input a field name first.',
-            });
-        }
-    };
-
-    handleDescChange = (index) => {
-        return (value) => {
-            let columns = this.state.columns;
-            columns[index].description = value;
-            this.setState({
-                columns
-            });
-        };
-    };
-
-    handleColDelete = (index) => {
-        return () => {
-            let columns = this.state.columns;
-            columns.splice(index, 1);
-            this.setState({
-                columns
-            });
-        }
-    };
-
-    componentWillReceiveProps(next) {
-        this.setState({
-            currentTable: next.currentTable,
-            currentSchema: next.currentSchema,
-            columns: next.columns || [],
-            remark: next.remark
-        });
-    };
-
-    render() {
-        console.log('columns',this.state.columns);
-        let schemaID = this.props.location.search.substr(this.props.location.search.indexOf("=")+1);
-        let userID = this.props.userID || getCookie('user_id');
-        console.log('schemaID',schemaID,'userID',userID);
-
-        return (
-            <div>
-                <div className="column-menu" onClick={()=>this.props.history.goBack()}>
-                    <Icon type="left" style={{color:'#3187FA'}} /> {this.props.match.params.name}
-                </div>
-
-                <Layout style={{padding: '24px', zIndex: '0'}}>
-                    <Content style={{padding: '24px', minHeight: 280, background: '#fff', marginTop: '48px'}}>
-                        <div className="column-content">
-                            <span className='table-title'> Table name</span>
-                            <Input
-                                value={this.state.currentTable}
-                                placeholder="please input table name"
-                                style={{width: 200, margin:'10px 0'}}
-                                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="please input table remark"
-                                style={{width: 250, margin:'10px 0'}}
-                                onChange={(e) => {
-                                    this.setState({remark: e.target.value})
-                                }}/>
-                        </div>
-
-                        <div>
-                            <span className='table-title'> Table fields</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
-                                            className="column-details"
-                                            value={col.name}
-                                            onChange={this.handleNameChange(index)}
-                                        />
-                                        <Select
-                                            className="column-details"
-                                            defaultValue={col.type}
-                                            onChange={this.handleTypeChange(index)}>
-                                            {
-                                                this.state.types.map((value) =>
-                                                    <Option key={Math.random()+'types'} value={value.toLowerCase()}>{value}</Option>
-                                                )
-                                            }
-                                        </Select>
-                                        <Select
-                                            className="column-details"
-                                            defaultValue={col.description}
-                                            onChange={this.handleDescChange(index)}>
-                                            {
-                                                this.state.descriptions.map((value) =>
-                                                    <Option key={Math.random()+'descriptions'} value={value.toLowerCase()}>{value}</Option>
-                                                )
-                                            }
-                                        </Select>
-                                        <Icon type="delete" theme="twoTone" style={{cursor: 'pointer'}} onClick={this.handleColDelete(index)}/>
-                                    </div>
-                                )
-                            }
-                            <div>
-                                <Input
-                                    className="column-details"
-                                    placeholder="field name"
-                                    onChange={this.handleNameNew}
-                                    value={this.state.newColName}
-                                />
-                                <Select
-                                    className="column-details"
-                                    defaultValue="type"
-                                    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={schemaID}
-                                userID={userID}
-                            />
-                            {/*{*/}
-                                {/*// undefined的时候 上层未传进来 currentTable === 'add',为的是输入框内不默认显示 add*/}
-                                {/*this.state.currentTable === '' ? '' : this.state.currentTable === undefined ? '' :*/}
-                                    {/*<DeleteTableButton*/}
-                                        {/*currentTable={this.state.currentTable}*/}
-                                        {/*currentTableIndex={this.props.currentTableIndex}*/}
-                                        {/*schemaID={schemaID}*/}
-                                        {/*userID={userID}*/}
-                                    {/*/>*/}
-                            {/*}*/}
-                        </div>
-                    </Content>
-                </Layout>
-            </div>
-        )
-    }
-}
-
-export default Column;
-
-
-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={gql(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={gql(UPDATE_SCHEMA)}
-                                update={(cache, {data: {update_schema}}) => {
-                                    let data = cache.readQuery({
-                                        query: gql(SHOW_TABLE),
-                                        variables: {schema_id: schemaID}
-                                    });
-
-                                    data.schema_by_id = update_schema;
-
-                                    let showSchemaData = cache.readQuery({
-                                        query: gql(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);
-
-                                    cache.writeQuery(
-                                        {
-                                            query: gql(SHOW_TABLE),
-                                            variables: {schema_id: schemaID},
-                                            data
-                                        },
-                                        {
-                                            query: gql(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);
-
-                                    // 处理一下description的问题
-                                    let cols = this.props.columns;
-                                    cols.map(obj => {
-                                        if (obj.description === 'description')
-                                            obj.description = '';
-                                        return obj
-                                    });
-
-                                    let newTable = {
-                                        name: this.props.currentTable,
-                                        remark: this.props.remark,
-                                        cols
-                                    };
-
-                                    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)
-                                                    }
-                                                });
-                                            }}>
-                                                save
-                                            </Button>
-                                        </div>
-                                    )
-                                }}
-                            </Mutation>
-                        )
-                    }
-                }
-            </Query>
-        )
-    }
-}
-
-class DeleteTableButton extends Component {
-    render() {
-        let schemaID = this.props.schemaID;
-        let userID = this.props.userID;
-
-        let varobj = {
-            id: schemaID,
-            updatedAt: new Date().getTime(),
-            schemaState: 'updated-delete-table',
-        };
-
-        return (
-            <Query query={gql(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={gql(UPDATE_SCHEMA)}
-                                update={(cache, {data: {update_schema}}) => {
-                                    let data = cache.readQuery({
-                                        query: gql(SHOW_TABLE),
-                                        variables: {schema_id: schemaID}
-                                    });
-                                    data.schema_by_id = update_schema;
-
-                                    let showSchemaData = cache.readQuery({
-                                        query: gql(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: gql(SHOW_TABLE),
-                                            variables: {schema_id: schemaID},
-                                            data
-                                        },
-                                        {
-                                            query: gql(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>
-        )
-    }
-}

+ 95 - 59
src/components/common/schema/Schema.jsx

@@ -4,8 +4,9 @@ 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, ADD_SCHEMA, DELETE_SCHEMA, SHOW_TABLE} from '../../gql'
-import Table from './Table';
+import {SHOW_SCHEMA, DELETE_SCHEMA, SHOW_TABLE} from '../../gql'
+import Table from "./Table";
+
 
 const Search = Input.Search;
 
@@ -19,8 +20,8 @@ class Schema extends Component {
         super(props);
         this.state = {
             currentTable: '',
-            // 下面这两个参数跟 props 拿
-            schemaID: 'schema_1542243424669_92094965',
+            // default schemaID and schemaName
+            schemaID: props.schemaID,
             schemaName: 'ecommerce'
         };
     }
@@ -32,62 +33,99 @@ class Schema extends Component {
         })
     };
 
+    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({
+            schemaID: next.schemaID
+        });
+    }
+
     render() {
         let userID = this.props.userID;
         return (
-            <div>
-                <div>
-                    {this.state.schemaName}
-                </div>
-                <div>
-                    <Row>
-                        <Col span={8}><span>Name</span></Col>
-                        <Col span={8}><span>Remark</span></Col>
-                        <Col span={8}><span><Icon type="plus"/></span></Col>
-                    </Row>
-                    <Table schemaID={this.state.schemaID}/>
-                </div>
-
-                {/*<Row>*/}
-                {/*<Col span={17} offset={1}>*/}
-                {/*{*/}
-                {/*this.state.currentTable === 'add' ?*/}
-                {/*<Column*/}
-                {/*currentSchema={this.state.currentSchema}*/}
-                {/*currentTableIndex={-2}*/}
-                {/*columns={[]}*/}
-                {/*remark=''*/}
-                {/*userID={userID}*/}
-                {/*schemaID={this.state.schemaID}*/}
-                {/*/> :*/}
-                {/*<Query query={gql(SHOW_TABLE)}*/}
-                {/*variables={{schema_id: this.state.schemaID}}>*/}
-                {/*{*/}
-                {/*({loading, error, data}) => {*/}
-                {/*if (loading)*/}
-                {/*return <Spin style={{marginLeft: 30, marginTop: 10}}/>;*/}
-                {/*if (error)*/}
-                {/*return 'error';*/}
-                {/*if (data.schema_by_id === null) data = [];*/}
-                {/*else data = JSON.parse(data.schema_by_id.schemaData);*/}
-                {/*return (*/}
-                {/*<Column*/}
-                {/*currentSchema={this.state.currentSchema}*/}
-                {/*currentTable={this.state.currentTable}*/}
-                {/*currentTableIndex={this.state.currentTable === '' ? -2 : data.findIndex(obj => obj.name === this.state.currentTable)}*/}
-                {/*columns={this.findColumns(data)}*/}
-                {/*remark={this.findRemark(data)}*/}
-                {/*schemaID={this.state.schemaID}*/}
-                {/*userID={userID}*/}
-                {/*/>*/}
-                {/*)*/}
-                {/*}*/}
-                {/*}*/}
-                {/*</Query>*/}
-                {/*}*/}
-                {/*</Col>*/}
-                {/*</Row>*/}
-            </div>
+            <Query query={gql(SHOW_TABLE)} variables={{schema_id: this.state.schemaID}}>
+
+                {
+                    ({loading, error, data}) => {
+                        if (loading) {
+                            return <Spin style={{marginLeft: 3}}/>
+                        }
+                        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 (
+                            <div>
+                                <div onClick={() => {
+                                    this.setState({
+                                        currentTable: ''
+                                    })
+                                }}>
+                                    {schemaName || this.state.schemaName}
+                                </div>
+                                {
+                                    this.state.currentTable === '' ?
+                                        <div>
+                                            <div>
+                                                <Row>
+                                                    <Col span={8}><span>Name</span></Col>
+                                                    <Col span={8}><span>Remark</span></Col>
+                                                    <Col span={8}>
+                                                        <span onClick={() => {
+                                                            this.setState({
+                                                                currentTable: 'add'
+                                                            })
+                                                        }}>
+                                                            <Icon type="plus"/>
+                                                        </span>
+                                                    </Col>
+                                                </Row>
+                                            </div>
+                                            {
+                                                data.map(table => (
+                                                    <Row key={table.name} className='show'>
+                                                        <Col span={8}
+                                                             onClick={() => this.switchTable(table.name)}><span>{table.name}</span></Col>
+                                                        <Col span={8}><span>{table.remark}</span></Col>
+                                                        <Col span={8}><Icon type="delete"/></Col>
+                                                    </Row>
+                                                ))
+                                            }
+                                        </div>
+                                        :
+                                        this.state.currentTable === 'add' ?
+                                            <Table
+                                                currentTableIndex={-2}
+                                                columns={[]}
+                                                remark=''
+                                                schemaID={this.state.schemaID}
+                                                userID={userID}
+                                            /> :
+                                            <Table
+                                                currentTable={this.state.currentTable}
+                                                currentTableIndex={this.state.currentTable === '' ? -2 : data.findIndex(obj => obj.name === this.state.currentTable)}
+                                                columns={this.findColumns(data)}
+                                                remark={this.findRemark(data)}
+                                                schemaID={this.state.schemaID}
+                                                userID={userID}
+                                            />
+                                }
+
+                            </div>
+                        );
+                    }
+                }
+            </Query>
+
         )
     }
 }
@@ -95,8 +133,6 @@ class Schema extends Component {
 export default Schema;
 
 
-
-
 // 备用代码
 
 class DeleteSchemaButton extends Component {

+ 406 - 26
src/components/common/schema/Table.js

@@ -1,41 +1,307 @@
 import React, {Component} from 'react';
 
-import {Row, Col, Icon, Spin} from 'antd';
-import './index.css';
-import Column from './Column';
-import {Query} from "react-apollo";
+import {Select, Input, Icon, Button, notification, Spin} from 'antd';
+import {UPDATE_SCHEMA, SHOW_SCHEMA, SHOW_TABLE} from "../../gql";
 import gql from "graphql-tag";
-import {SHOW_TABLE} from '../../gql'
-import Schema from "./Schema";
 
+import {Mutation, Query} from "react-apollo";
+
+const Option = Select.Option;
 
 class Table extends Component {
+    constructor(props) {
+        super(props);
+        this.state = {
+            currentTable: props.currentTable,
+            remark: props.remark,
+            columns: props.columns,
+            newColName: '',
+            newColType: 'type',
+            types: ['ID', 'String', 'Int', 'Float', 'Boolean', 'DateTime'],
+            descriptions: ['description', 'key', 'non-null', 'non-null-list', 'list']
+        }
+    }
+
+
+    // 下面的 handlexxxx 全是 state 内部方法,用于操作视图
+    // cache 仅在提交删除整体使用
+
+    handleNameChange = (index) => {
+        return (e) => {
+            let columns = this.state.columns;
+            columns[index].name = e.target.value;
+            this.setState({
+                columns
+            })
+        };
+    };
+
+    handleNameNew = (e) => {
+        this.setState({
+            newColName: e.target.value,
+            showNewColumn: false
+        })
+    };
+
+    handleTypeChange = (index) => {
+        return (value) => {
+            let columns = this.state.columns;
+            columns[index].type = value;
+            this.setState({
+                columns
+            });
+        }
+    };
+
+    handleTypeNew = (value) => {
+        if (this.state.newColName !== '') {
+            let columns = this.state.columns;
+            columns.push({name: this.state.newColName, type: value, description: 'description'});
+            this.setState({
+                columns,
+                newColName: '',
+                newColType: 'type'
+            })
+        } else {
+            notification['warning']({
+                message: 'Notification',
+                description: 'Input a name first.',
+            });
+        }
+    };
+
+    handleDescChange = (index) => {
+        return (value) => {
+            let columns = this.state.columns;
+            columns[index].description = value;
+            this.setState({
+                columns
+            });
+        };
+    };
+
+    handleColDelete = (index) => {
+        return () => {
+            let columns = this.state.columns;
+            columns.splice(index, 1);
+            this.setState({
+                columns
+            });
+        }
+    };
+
+    componentWillReceiveProps(next) {
+        this.setState({
+            currentTable: next.currentTable,
+            columns: next.columns,
+            remark: next.remark
+        });
+    };
+
+    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.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>
+        )
+    }
+}
+
+export default Table;
+
+
+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={gql(SHOW_TABLE)} variables={{schema_id: this.props.schemaID}}>
+            <Query query={gql(SHOW_TABLE)} variables={{schema_id: schemaID}}>
 
                 {
                     ({loading, error, data}) => {
-                        if (loading) {
-                            return <Spin style={{marginLeft: 3}}/>
-                        }
-                        if (error) {
-                            return 'error!';
-                        }
+                        if (loading)
+                            return <Spin style={{marginLeft: 30, marginTop: 10}}/>;
+                        if (error)
+                            return 'error';
+
+                        let schemaData = data;
+
                         return (
-                            <div>
-                                {
-                                    JSON.parse(data.schema_by_id.schemaData).map(table => (
-                                        <Row key={table.name} className='show'>
-                                            <Col span={8}><span>{table.name}</span></Col>
-                                            <Col span={8}><span>{table.remark}</span></Col>
-                                            <Col span={8}><Icon type="delete"/></Col>
-                                        </Row>
-                                    ))
-                                }
-                            </div>
-                        );
+                            <Mutation
+                                mutation={gql(UPDATE_SCHEMA)}
+                                update={(cache, {data: {update_schema}}) => {
+                                    let data = cache.readQuery({
+                                        query: gql(SHOW_TABLE),
+                                        variables: {schema_id: schemaID}
+                                    });
+
+                                    data.schema_by_id = update_schema;
+
+                                    let showSchemaData = cache.readQuery({
+                                        query: gql(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);
+
+                                    cache.writeQuery(
+                                        {
+                                            query: gql(SHOW_TABLE),
+                                            variables: {schema_id: schemaID},
+                                            data
+                                        },
+                                        {
+                                            query: gql(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);
+
+                                    // 处理一下description的问题
+                                    let cols = this.props.columns;
+                                    cols.map(obj => {
+                                        if (obj.description === 'description')
+                                            obj.description = '';
+                                        return obj
+                                    });
+
+                                    let newTable = {
+                                        name: this.props.currentTable,
+                                        remark: this.props.remark,
+                                        cols
+                                    };
+
+                                    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>
@@ -43,5 +309,119 @@ class Table extends Component {
     }
 }
 
-export default Table;
+class DeleteTableButton extends Component {
+    render() {
+        let schemaID = this.props.schemaID;
+        let userID = this.props.userID;
+
+        let varobj = {
+            id: schemaID,
+            updatedAt: new Date().getTime(),
+            schemaState: 'updated-delete-table',
+        };
+
+        return (
+            <Query query={gql(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={gql(UPDATE_SCHEMA)}
+                                update={(cache, {data: {update_schema}}) => {
+                                    let data = cache.readQuery({
+                                        query: gql(SHOW_TABLE),
+                                        variables: {schema_id: schemaID}
+                                    });
+                                    data.schema_by_id = update_schema;
+
+                                    let showSchemaData = cache.readQuery({
+                                        query: gql(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: gql(SHOW_TABLE),
+                                            variables: {schema_id: schemaID},
+                                            data
+                                        },
+                                        {
+                                            query: gql(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>
+        )
+    }
+}

+ 1 - 0
src/components/gql.js

@@ -113,6 +113,7 @@ const SHOW_TABLE = `
             query TABLE($schema_id: ID!) {
                 schema_by_id(id: $schema_id) {
                     schemaData
+                    schemaName
                 }
             }
         `;

+ 26 - 4
src/components/trialCase/TrialCase.jsx

@@ -20,8 +20,10 @@ class TrialCase extends Component {
 
         this.state = {
             menuLevel2: "schema",
+            // default user
             userID: 'ioobot',
-            getID_url: 'http://123.206.193.98:8999/getuserid'
+            getID_url: 'http://123.206.193.98:8999/getuserid',
+            schemaID: ''
         }
     }
 
@@ -41,6 +43,27 @@ class TrialCase extends Component {
             });
     }
 
+    componentWillReceiveProps(nextProps) {
+        let schemaID;
+        switch (nextProps.match.params.caseId) {
+            case 'e-commerce':
+                schemaID = 'schema_1542243424669_92094965';
+                break;
+            case 'keep-accounts':
+                schemaID = 'schema_1542257622953_79276889';
+                break;
+            case 'appointment':
+                schemaID = 'schema_1542246903496_97115305';
+                break;
+            default:
+                schemaID = nextProps.match.params.caseId || 'schema_1542243424669_92094965';
+        }
+        this.setState({
+            schemaID
+        });
+    }
+
+
     switchMenu = (menuName, e) => {
         // console.log('menuName', menuName, 'e', e);
         this.setState({
@@ -73,9 +96,9 @@ class TrialCase extends Component {
                             (() => {
                                 switch (this.state.menuLevel2) {
                                     case 'schema':
-                                        return <Schema userID={this.state.userID}/>;
+                                        return <Schema userID={this.state.userID} schemaID={this.state.schemaID}/>;
                                     case 'deploy':
-                                        return <Deploy userID={this.state.userID}/>;
+                                        return <Deploy userID={this.state.userID} schemaID={this.state.schemaID}/>;
                                     case 'graphql':
                                         return <GenerateJs/>;
                                     case 'graphiql':
@@ -85,7 +108,6 @@ class TrialCase extends Component {
                                     default:
                                         return <Graphql/>
                                 }
-
                             })()
                         }