import React, {Component} from 'react';
import {Layout, Select, Input, Icon, Button, notification, Spin, Modal} 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;
const confirm = Modal.confirm;
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() {
let schemaID = this.props.schemaID || this.props.history.location.state.schemaID;
let schemaName = this.props.schemaName || this.props.history.location.state.schemaName;
let userID = this.props.userID || getCookie('user_id');
let trialcase = this.props.trialcase;
return (
)
}
}
export default Table;
class UpdateTableButton extends Component {
constructor(props) {
super(props);
this.state = {
originTableName: props.currentTable
}
}
showConfirm = (schemaName, schemaID) => {
let _this = this;
if (this.props.add !== 'add') {
this.props.history.push({
pathname: `/graphql-service/my-create/${schemaName}`,
state: {
schemaName,
schemaID
}
});
} else {
confirm({
title: '添加成功',
content: '继续添加数据表?',
onOk() {
console.log('继续添加');
},
onCancel() {
_this.props.history.push({
pathname: `/graphql-service/my-create/${schemaName}`,
state: {
schemaName,
schemaID
}
});
}
});
}
};
render() {
let schemaID = this.props.schemaID;
let schemaName = this.props.schemaName;
let userID = this.props.userID;
let varobj = {
id: schemaID,
updatedAt: new Date().getTime(),
schemaState: 'updated-update-table',
};
return (
{
({loading, error, data}) => {
if (loading)
return ;
if (error)
return 'error';
let schemaData = data;
let referenceID = '';
if (data.schema_by_id)
referenceID = data.schema_by_id.reference;
return (
this.showConfirm(schemaName, schemaID)}
refetchQueries={[{query: gql(SHOW_TABLE), variables: {schema_id: schemaID}}]}
>
{(update_schema, {loading, error}) => {
if (loading)
return ;
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 (
)
}}
)
}
}
)
}
}