Schema.jsx 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. import React, {Component} from 'react';
  2. import {Row, Col, Input, Icon, Button, Spin} from 'antd';
  3. import './index.css';
  4. import Column from './Column';
  5. import {Mutation, Query} from "react-apollo";
  6. import gql from "graphql-tag";
  7. import {SHOW_SCHEMA, ADD_SCHEMA, DELETE_SCHEMA, SHOW_TABLE} from '../../gql'
  8. const Search = Input.Search;
  9. const idGen = (kind) => {
  10. return kind + '_' + Date.now() + '_' + Math.random().toString().slice(-8);
  11. };
  12. class Schema extends Component {
  13. constructor(props) {
  14. super(props);
  15. this.state = {
  16. currentSchema: '',
  17. currentTable: '',
  18. schemaID: ''
  19. };
  20. }
  21. findColumns = data => this.state.currentTable === '' ? [] : data.find(table => table.name === this.state.currentTable) ? data.find(table => table.name === this.state.currentTable).cols : [];
  22. findRemark = data => this.state.currentTable === '' ? '' : data.find(table => table.name === this.state.currentTable) ? data.find(table => table.name === this.state.currentTable).remark : '';
  23. switchSchema = (name, id) => {
  24. this.setState({
  25. currentSchema: name,
  26. schemaID: id
  27. });
  28. };
  29. switchTable = (table) => {
  30. this.setState({
  31. currentTable: table
  32. })
  33. };
  34. render() {
  35. let userID = this.props.userID;
  36. return (
  37. <div>
  38. <Row>
  39. <Col span={17} offset={1}>
  40. {
  41. this.state.currentTable === 'add' ?
  42. <Column
  43. currentSchema={this.state.currentSchema}
  44. currentTableIndex={-2}
  45. columns={[]}
  46. remark=''
  47. userID={userID}
  48. schemaID={this.state.schemaID}
  49. /> :
  50. <Query query={gql(SHOW_TABLE)}
  51. variables={{schema_id: this.state.schemaID}}>
  52. {
  53. ({loading, error, data}) => {
  54. if (loading)
  55. return <Spin style={{marginLeft: 30, marginTop: 10}}/>;
  56. if (error)
  57. return 'error';
  58. if (data.schema_by_id === null) data = [];
  59. else data = JSON.parse(data.schema_by_id.schemaData);
  60. return (
  61. <Column
  62. currentSchema={this.state.currentSchema}
  63. currentTable={this.state.currentTable}
  64. currentTableIndex={this.state.currentTable === '' ? -2 : data.findIndex(obj => obj.name === this.state.currentTable)}
  65. columns={this.findColumns(data)}
  66. remark={this.findRemark(data)}
  67. schemaID={this.state.schemaID}
  68. userID={userID}
  69. />
  70. )
  71. }
  72. }
  73. </Query>
  74. }
  75. </Col>
  76. </Row>
  77. </div>
  78. )
  79. }
  80. }
  81. export default Schema;
  82. // 备用代码
  83. class ShowSchemaList extends Component {
  84. render() {
  85. let userID = this.props.userID;
  86. return (
  87. <Query query={SHOW_SCHEMA} variables={{user_id: userID}}>
  88. {
  89. ({loading, error, data}) => {
  90. if (loading) {
  91. return <Spin style={{marginLeft: 3}}/>
  92. }
  93. if (error) {
  94. return 'error!';
  95. }
  96. if (data.schema_by_props.length === 0)
  97. return (
  98. <div>
  99. <span>no schemas, create one</span>
  100. </div>
  101. );
  102. else {
  103. return (
  104. <div>
  105. {
  106. data.schema_by_props.map((schema) => {
  107. return <div key={schema.schemaName} className='title'
  108. onClick={() => {
  109. this.props.switchSchema(schema.schemaName)
  110. }}>
  111. <Row>
  112. <Col span={20}>{schema.schemaName}</Col>
  113. <Col span={4}>
  114. <Button onClick={() => this.props.switchTable('add')}
  115. type="primary"
  116. shape="circle" icon="plus" size='small'/>
  117. <DeleteSchemaButton
  118. schemaName={schema.schemaName}
  119. userID={this.props.userID}
  120. />
  121. </Col>
  122. </Row>
  123. {
  124. JSON.parse(schema.schemaData).map(table => (
  125. <p
  126. onClick={() => {
  127. this.props.switchTable(table.name)
  128. }}
  129. key={table.name}
  130. className='show'>
  131. <Icon type="ordered-list" theme="outlined"/> {table.name}
  132. <span className='remark'><i> {table.remark}</i></span>
  133. </p>
  134. ))
  135. }
  136. </div>
  137. })
  138. }
  139. </div>
  140. );
  141. }
  142. }
  143. }
  144. </Query>
  145. )
  146. }
  147. }
  148. class DeleteSchemaButton extends Component {
  149. constructor(props) {
  150. super(props);
  151. this.state = {
  152. schemaName: props.schemaName
  153. }
  154. }
  155. render() {
  156. let userID = this.props.userID;
  157. return (
  158. <Mutation
  159. mutation={DELETE_SCHEMA}
  160. update={(cache) => {
  161. let data = cache.readQuery({query: SHOW_SCHEMA, variables: {user_id: userID}});
  162. data.schema_by_props.splice(data.schema_by_props.findIndex(obj => obj.schemaName === this.state.schemaName), 1);
  163. cache.writeQuery({
  164. query: SHOW_SCHEMA,
  165. variables: {user_id: userID},
  166. data
  167. });
  168. }}
  169. >
  170. {(delete_schema, {loading, error}) => {
  171. if (error)
  172. return 'error';
  173. if (loading)
  174. return <Spin style={{marginLeft: 3}}/>;
  175. return (
  176. <Button onClick={(e) => {
  177. delete_schema({variables: {schemaName: this.props.schemaName}});
  178. }} type="danger" shape="circle" icon="delete" size='small' style={{marginLeft: 3}}/>
  179. )
  180. }}
  181. </Mutation>
  182. )
  183. }
  184. }