Schema.jsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. import React, {Component} from 'react';
  2. import {Row, Col, Input, Icon, Button, Spin} from 'antd';
  3. import './index.css';
  4. import SchemaChange from './change/SchemaChange';
  5. import {Mutation, Query} from "react-apollo";
  6. import {SHOW_SCHEMA, ADD_SCHEMA, DELETE_SCHEMA, SHOW_TABLE} from './gql'
  7. const Search = Input.Search;
  8. const idGen = (kind) => {
  9. return kind + '_' + Date.now() + '_' + Math.random().toString().slice(-8);
  10. };
  11. class Schema extends Component {
  12. constructor(props) {
  13. super(props);
  14. this.state = {
  15. currentSchema: '',
  16. currentTable: '',
  17. schemaID: '',
  18. userID: 'xy_1'
  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. return (
  36. <div>
  37. <Row>
  38. <Col span={6}>
  39. <div className='wrapper'>
  40. <div className='current'>{this.state.currentSchema}</div>
  41. <AddSchemaInput userID={this.state.userID}/>
  42. </div>
  43. <ShowSchemaList
  44. userID={this.state.userID}
  45. switchTable={this.switchTable}
  46. switchSchema={this.switchSchema}
  47. />
  48. </Col>
  49. <Col span={17} offset={1}>
  50. {
  51. this.state.currentTable === 'add' ?
  52. <SchemaChange
  53. currentSchema={this.state.currentSchema}
  54. currentTableIndex={-2}
  55. columns={[]}
  56. remark=''
  57. userID={this.state.userID}
  58. schemaID={this.state.schemaID}
  59. /> :
  60. <Query query={SHOW_TABLE}
  61. variables={{schema_id: this.state.schemaID}}>
  62. {
  63. ({loading, error, data}) => {
  64. if (loading)
  65. return <Spin style={{marginLeft: 30, marginTop: 10}}/>;
  66. if (error)
  67. return 'error';
  68. if (data.schema_by_id === null) data = [];
  69. else data = JSON.parse(data.schema_by_id.schemaData);
  70. return (
  71. <SchemaChange
  72. currentSchema={this.state.currentSchema}
  73. currentTable={this.state.currentTable}
  74. currentTableIndex={this.state.currentTable === '' ? -2 : data.findIndex(obj => obj.name === this.state.currentTable)}
  75. columns={this.findColumns(data)}
  76. remark={this.findRemark(data)}
  77. schemaID={this.state.schemaID}
  78. userID={this.state.userID}
  79. />
  80. )
  81. }
  82. }
  83. </Query>
  84. }
  85. </Col>
  86. </Row>
  87. </div>
  88. )
  89. }
  90. }
  91. export default Schema;
  92. class AddSchemaInput extends Component {
  93. render() {
  94. let userID = this.props.userID;
  95. let varobj = {
  96. id: idGen('schema'),
  97. user_id: userID,
  98. createdAt: new Date().getTime(),
  99. updatedAt: '',
  100. schemaState: 'create',
  101. schemaData: JSON.stringify([])
  102. };
  103. return (
  104. <Mutation
  105. mutation={ADD_SCHEMA}
  106. update={(cache, {data: {create_schema}}) => {
  107. let data = cache.readQuery({query: SHOW_SCHEMA, variables: {user_id: userID}});
  108. data.schema_by_props.push(create_schema);
  109. cache.writeQuery({
  110. query: SHOW_SCHEMA,
  111. variables: {user_id: userID},
  112. data
  113. });
  114. }}
  115. >
  116. {(create_schema, {loading, error}) => {
  117. if (loading)
  118. return <Spin style={{marginLeft: 30, marginTop: 10}}/>;
  119. if (error)
  120. return 'error';
  121. return (
  122. <div>
  123. <Search
  124. className='add-input'
  125. placeholder="input schema_name"
  126. enterButton="Confirm"
  127. onSearch={value => {
  128. this.setState({
  129. switch: true,
  130. currentSchema: value,
  131. currentTable: ''
  132. });
  133. create_schema({
  134. variables: {
  135. ...varobj,
  136. schemaName: value
  137. }
  138. });
  139. }}
  140. />
  141. </div>)
  142. }}
  143. </Mutation>
  144. )
  145. }
  146. }
  147. class ShowSchemaList extends Component {
  148. render() {
  149. let userID = this.props.userID;
  150. return (
  151. <Query query={SHOW_SCHEMA} variables={{user_id: userID}}>
  152. {
  153. ({loading, error, data}) => {
  154. if (loading) {
  155. return <Spin style={{marginLeft: 3}}/>
  156. }
  157. if (error) {
  158. return 'error!';
  159. }
  160. if (data.schema_by_props.length === 0)
  161. return (
  162. <div>
  163. <span>no schemas, create one</span>
  164. </div>
  165. );
  166. else {
  167. return (
  168. <div>
  169. {
  170. data.schema_by_props.map((schema) => {
  171. return <div key={schema.schemaName} className='title'
  172. onClick={() => {
  173. this.props.switchSchema(schema.schemaName, schema.id);
  174. }}>
  175. <Row>
  176. <Col span={20}>{schema.schemaName}</Col>
  177. <Col span={4}>
  178. <Button onClick={() => this.props.switchTable('add')}
  179. type="primary"
  180. shape="circle" icon="plus" size='small'/>
  181. <DeleteSchemaButton
  182. schemaName={schema.schemaName}
  183. userID={this.props.userID}
  184. />
  185. </Col>
  186. </Row>
  187. {
  188. JSON.parse(schema.schemaData).map(table => (
  189. <p
  190. onClick={() => {
  191. this.props.switchTable(table.name)
  192. }}
  193. key={table.name}
  194. className='show'>
  195. <Icon type="ordered-list" theme="outlined"/> {table.name}
  196. <span className='remark'><i> {table.remark}</i></span>
  197. </p>
  198. ))
  199. }
  200. </div>
  201. })
  202. }
  203. </div>
  204. );
  205. }
  206. }
  207. }
  208. </Query>
  209. )
  210. }
  211. }
  212. class DeleteSchemaButton extends Component {
  213. constructor(props) {
  214. super(props);
  215. this.state = {
  216. schemaName: props.schemaName
  217. }
  218. }
  219. render() {
  220. let userID = this.props.userID;
  221. return (
  222. <Mutation
  223. mutation={DELETE_SCHEMA}
  224. update={(cache) => {
  225. let data = cache.readQuery({query: SHOW_SCHEMA, variables: {user_id: userID}});
  226. data.schema_by_props.splice(data.schema_by_props.findIndex(obj => obj.schemaName === this.state.schemaName), 1);
  227. cache.writeQuery({
  228. query: SHOW_SCHEMA,
  229. variables: {user_id: userID},
  230. data
  231. });
  232. }}
  233. >
  234. {(delete_schema, {loading, error}) => {
  235. if (error)
  236. return 'error';
  237. if (loading)
  238. return <Spin style={{marginLeft: 3}}/>;
  239. return (
  240. <Button onClick={(e) => {
  241. delete_schema({variables: {schemaName: this.props.schemaName}});
  242. }} type="danger" shape="circle" icon="delete" size='small' style={{marginLeft: 3}}/>
  243. )
  244. }}
  245. </Mutation>
  246. )
  247. }
  248. }