Schema.jsx 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. import React, {Component} from 'react';
  2. import {Row, Col, Input, Icon, Button, Spin} from 'antd';
  3. import './index.css';
  4. import {Mutation, Query} from "react-apollo";
  5. import gql from "graphql-tag";
  6. import {SHOW_SCHEMA, DELETE_SCHEMA, SHOW_TABLE} from '../../gql'
  7. import Table from "./Table";
  8. class Schema extends Component {
  9. constructor(props) {
  10. super(props);
  11. console.log('Schema props',props);
  12. this.state = {
  13. currentTable: props.schemaID ?'':'add',
  14. // default schemaID and schemaName
  15. schemaID: props.schemaID || props.history.location.state.schemaID,
  16. schemaName: props.schemaName || props.history.location.state.schemaName,
  17. };
  18. }
  19. switchTable = (table) => {
  20. this.setState({
  21. currentTable: table
  22. })
  23. };
  24. findColumns = data => this.state.currentTable === '' ? [] : data.find(table => table.name === this.state.currentTable) ? data.find(table => table.name === this.state.currentTable).cols : [];
  25. findRemark = data => this.state.currentTable === '' ? '' : data.find(table => table.name === this.state.currentTable) ? data.find(table => table.name === this.state.currentTable).remark : '';
  26. componentWillReceiveProps(next) {
  27. this.setState({
  28. currentTable: '',
  29. schemaID: next.schemaID,
  30. schemaName: next.schemaName
  31. });
  32. }
  33. render() {
  34. let userID = this.props.userID;
  35. console.log('this.state.currentTable',this.state.currentTable);
  36. return (
  37. <Query query={gql(SHOW_TABLE)} variables={{schema_id: this.state.schemaID}}>
  38. {
  39. ({loading, error, data}) => {
  40. if (loading) {
  41. return <Spin style={{marginLeft: 3}}/>
  42. }
  43. if (error) {
  44. return 'error!';
  45. }
  46. // let schemaName = data.schema_by_id.schemaName;
  47. if (data.schema_by_id === null) data = [];
  48. else data = JSON.parse(data.schema_by_id.schemaData);
  49. return (
  50. <div>
  51. {
  52. this.state.currentTable === '' ?
  53. <div>
  54. <div onClick={() => {
  55. this.setState({
  56. currentTable: ''
  57. })
  58. }}>
  59. {this.state.schemaName}
  60. </div>
  61. <div>
  62. <Row>
  63. <Col span={8}><span>Name</span></Col>
  64. <Col span={8}><span>Remark</span></Col>
  65. <Col span={8}>
  66. <span onClick={() => {
  67. this.setState({
  68. currentTable: 'add'
  69. })
  70. }}>
  71. <Icon type="plus"/>
  72. </span>
  73. </Col>
  74. </Row>
  75. </div>
  76. {
  77. data.map(table => (
  78. <Row key={table.name} className='show'>
  79. <Col span={8}
  80. onClick={() => this.switchTable(table.name)}><span>{table.name}</span></Col>
  81. <Col span={8}><span>{table.remark}</span></Col>
  82. <Col span={8}><Icon type="delete"/></Col>
  83. </Row>
  84. ))
  85. }
  86. </div>
  87. :
  88. this.state.currentTable === 'add' ?
  89. <Table
  90. currentTableIndex={-2}
  91. columns={[]}
  92. remark=''
  93. schemaID={this.state.schemaID}
  94. schemaName={this.state.schemaName}
  95. userID={userID}
  96. /> :
  97. <Table
  98. currentTable={this.state.currentTable}
  99. currentTableIndex={this.state.currentTable === '' ? -2 : data.findIndex(obj => obj.name === this.state.currentTable)}
  100. columns={this.findColumns(data)}
  101. remark={this.findRemark(data)}
  102. schemaID={this.state.schemaID}
  103. schemaName={this.state.schemaName}
  104. userID={userID}
  105. />
  106. }
  107. </div>
  108. );
  109. }
  110. }
  111. </Query>
  112. )
  113. }
  114. }
  115. export default Schema;
  116. // 备用代码
  117. class DeleteSchemaButton extends Component {
  118. constructor(props) {
  119. super(props);
  120. this.state = {
  121. schemaName: props.schemaName
  122. }
  123. }
  124. render() {
  125. let userID = this.props.userID;
  126. return (
  127. <Mutation
  128. mutation={DELETE_SCHEMA}
  129. update={(cache) => {
  130. let data = cache.readQuery({query: SHOW_SCHEMA, variables: {user_id: userID}});
  131. data.schema_by_props.splice(data.schema_by_props.findIndex(obj => obj.schemaName === this.state.schemaName), 1);
  132. cache.writeQuery({
  133. query: SHOW_SCHEMA,
  134. variables: {user_id: userID},
  135. data
  136. });
  137. }}
  138. >
  139. {(delete_schema, {loading, error}) => {
  140. if (error)
  141. return 'error';
  142. if (loading)
  143. return <Spin style={{marginLeft: 3}}/>;
  144. return (
  145. <Button onClick={(e) => {
  146. delete_schema({variables: {schemaName: this.props.schemaName}});
  147. }} type="danger" shape="circle" icon="delete" size='small' style={{marginLeft: 3}}/>
  148. )
  149. }}
  150. </Mutation>
  151. )
  152. }
  153. }