| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375 |
- 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 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'],
- characterTips: false
- }
- }
- // 下面的 handlexxxx 全是 state 内部方法,用于操作视图
- // cache 仅在提交删除整体使用
- handleNameChange = (index) => {
- return (e) => {
- let columns = this.state.columns;
- columns[index].name = e.target.value;
- this.setState({
- columns
- })
- };
- };
- handleNameNew = (e) => {
- let r = /^[^\u4e00-\u9fa5]*$/;
- if(r.test(e.target.value)){
- this.setState({
- newColName: e.target.value,
- })
- } else {
- this.setState({
- characterTips: true
- });
- setTimeout(()=>{
- this.setState({
- characterTips: false
- })
- }, 2000)
- }
- };
- 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,
- columns: next.columns,
- remark: next.remark
- });
- };
- render() {
- // console.log('columns',this.state.columns);
- // console.log('this.props',this.props);
- let schemaID = this.props.schemaID || this.props.history.location.state.schemaID;
- let schemaName = this.props.schemaName || this.props.history.location.state.schemaName;
- // console.log('schemaID',schemaID,'schemaName',schemaName);
- let userID = this.props.userID || getCookie('user_id');
- // console.log('schemaID',schemaID,'userID',userID);
- let trialcase = this.props.trialcase;
- return (
- <div>
- <div className="column-menu" onClick={this.props.goBack}>
- {/*()=>this.props.history.goBack()*/}
- <Icon type="left" style={{color: '#3187FA'}}/> {schemaName}
- </div>
- <Layout style={{zIndex: '0'}}>
- <Content style={{padding: '24px', minHeight: 280, background: '#fff'}}>
- <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>
- {
- trialcase ?
- ''
- :
- <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>
- {
- trialcase ?
- ''
- :
- <div style={{marginTop: 20}}>
- <UpdateTableButton
- currentTable={this.state.currentTable}
- columns={this.state.columns}
- remark={this.state.remark}
- schemaID={schemaID}
- userID={userID}
- schemaData={this.props.schemaData}
- fetchData={this.props.fetchData}
- showTablePagination={this.props.showTablePagination}
- page={this.props.page}
- pageSize={this.props.pageSize}
- history={this.props.history}
- schemaName={schemaName}
- add={this.props.add}
- />
- </div>
- }
- </Content>
- </Layout>
- </div>
- )
- }
- }
- export default Table;
- class UpdateTableButton extends Component {
- constructor(props) {
- super(props);
- this.state = {
- originTableName: props.currentTable
- }
- }
- 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;
- let referenceID;
- if(data.schema_by_id)
- referenceID = data.schema_by_id.reference;
- return (
- <Mutation
- mutation={gql(UPDATE_SCHEMA)}
- refetchQueries={[{query: gql(SHOW_TABLE), variables: {schema_id: schemaID}}]}
- >
- {(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.state.originTableName === '' ? -2 : this.props.schemaData.findIndex(obj => obj.name === this.state.originTableName);
- if (index === -2) {
- if(referenceID !== '' && schemaCols.length === 0) {
- this.props.fetchData(referenceID).then(value => {
- schemaCols = JSON.parse(value);
- schemaCols.push(newTable);
- });
- } else {
- schemaCols.push(newTable);
- }
- } else if (index === -1) {
- // 先取数据,然后替换,然后填充
- this.props.fetchData(referenceID).then(value => {
- schemaCols = JSON.parse(value);
- const index = schemaCols.findIndex(obj => obj.name === this.state.originTableName);
- schemaCols.splice(index, 1, newTable);
- });
- } else {
- schemaCols.splice(index, 1, newTable);
- }
- return (
- <div style={{display: 'inline-block'}}>
- <Button type="primary" onClick={() => {
- update_schema({
- variables: {
- ...varobj,
- schemaData: JSON.stringify(schemaCols)
- }
- });
- this.props.showTablePagination(this.props.page, this.props.pageSize, schemaCols)
- if(this.props.add !== 'add') {
- this.props.history.push({
- pathname: `/graphql-service/my-create/${this.props.schemaName}`,
- state:{
- schemaName:this.props.schemaName,
- schemaID:this.props.schemaID
- }
- });
- }
- }}>
- save
- </Button>
- </div>
- )
- }}
- </Mutation>
- )
- }
- }
- </Query>
- )
- }
- }
|