Schema.jsx 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. import React, {Component} from 'react';
  2. import {Row, Col, Input, Icon, Button, Spin, Pagination, Modal} 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, UPDATE_SCHEMA} from '../../gql'
  7. import Table from "./Table";
  8. const confirm = Modal.confirm;
  9. class Schema extends Component {
  10. constructor(props) {
  11. super(props);
  12. this.state = {
  13. currentTable: props.location.state === undefined ? '' : props.location.state.create ? 'add' : '',
  14. // default schemaID and schemaName
  15. schemaID: props.schemaID || props.location.state.schemaID,
  16. schemaName: props.schemaName || props.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. goBack = () => {
  27. this.setState({
  28. currentTable: ''
  29. })
  30. };
  31. componentWillReceiveProps(next) {
  32. this.setState({
  33. currentTable: next.location.state === undefined ? '' : next.location.state.create ? 'add' : '',
  34. schemaID: next.schemaID,
  35. schemaName: next.schemaName
  36. });
  37. }
  38. render() {
  39. let userID = this.props.userID;
  40. return (
  41. <Query query={gql(SHOW_TABLE)} variables={{schema_id: this.state.schemaID}}>
  42. {
  43. ({loading, error, data}) => {
  44. if (loading) {
  45. return <Spin style={{marginLeft: 3}}/>
  46. }
  47. if (error) {
  48. return 'error!';
  49. }
  50. // let schemaName = data.schema_by_id.schemaName;
  51. if (data.schema_by_id === null) data = [];
  52. else data = JSON.parse(data.schema_by_id.schemaData);
  53. return (
  54. <div>
  55. {
  56. this.state.currentTable === '' ?
  57. <div>
  58. <div className={'schema'} onClick={() => {
  59. this.setState({
  60. currentTable: ''
  61. })
  62. }}>
  63. <span className={'schema-name'}>{this.state.schemaName}</span>
  64. <Icon style={{marginLeft: 15}} type="edit" theme="twoTone"/>
  65. </div>
  66. <div className={'schema-table-list-title'}>
  67. <Row>
  68. <Col span={10}><span
  69. className={'schema-table-title'}>Name</span></Col>
  70. <Col span={10}><span
  71. className={'schema-table-title'}>Remark</span></Col>
  72. <Col span={2} offset={2}>
  73. <span
  74. className={'schema-table-title'}
  75. onClick={() => {
  76. this.setState({
  77. currentTable: 'add'
  78. })
  79. }}>
  80. <Icon type="plus"/>
  81. </span>
  82. </Col>
  83. </Row>
  84. </div>
  85. <div>
  86. {
  87. data.map(table => (
  88. <div key={table.name} className={'schema-table-list-content'}>
  89. <Row>
  90. <Col
  91. span={10}
  92. onClick={() => this.switchTable(table.name)}
  93. >
  94. <span
  95. className={'schema-table-content'}>{table.name}</span>
  96. </Col>
  97. <Col span={10}>
  98. <span
  99. className={'schema-table-content'}>{table.remark}</span>
  100. </Col>
  101. <Col span={2} offset={2}>
  102. <span className={'schema-table-content'}>
  103. <DeleteTableButton
  104. currentTable={table.name}
  105. currentTableIndex={data.findIndex(obj => obj.name === table.name)}
  106. schemaID={this.state.schemaID}
  107. userID={userID}
  108. />
  109. </span>
  110. </Col>
  111. </Row>
  112. </div>
  113. ))
  114. }
  115. </div>
  116. <div className={'schema-bottom'}>
  117. <Row>
  118. <Col span={4}>
  119. <div className={'delete-schema'}>
  120. <DeleteSchemaButton
  121. userID={userID}
  122. schemaName={this.state.schemaName}
  123. />
  124. {/*<Button type="danger">delete</Button>*/}
  125. </div>
  126. </Col>
  127. <Col span={4} offset={16}>
  128. <div className={'pagination'}>
  129. <Pagination simple defaultCurrent={2} total={50}/>
  130. </div>
  131. </Col>
  132. </Row>
  133. </div>
  134. </div>
  135. :
  136. this.state.currentTable === 'add' ?
  137. <Table
  138. currentTable={''}
  139. currentTableIndex={-2}
  140. columns={[]}
  141. remark=''
  142. schemaID={this.state.schemaID}
  143. schemaName={this.state.schemaName}
  144. userID={userID}
  145. goBack={this.goBack}
  146. /> :
  147. <Table
  148. currentTable={this.state.currentTable}
  149. currentTableIndex={this.state.currentTable === '' ? -2 : data.findIndex(obj => obj.name === this.state.currentTable)}
  150. columns={this.findColumns(data)}
  151. remark={this.findRemark(data)}
  152. schemaID={this.state.schemaID}
  153. schemaName={this.state.schemaName}
  154. userID={userID}
  155. goBack={this.goBack}
  156. />
  157. }
  158. </div>
  159. );
  160. }
  161. }
  162. </Query>
  163. )
  164. }
  165. }
  166. export default Schema;
  167. class DeleteSchemaButton extends Component {
  168. constructor(props) {
  169. super(props);
  170. this.state = {
  171. schemaName: props.schemaName
  172. }
  173. }
  174. showConfirm = (delete_schema, schemaName) => {
  175. confirm({
  176. title: 'Do you want to delete this schema?',
  177. content: 'It cannot be find back!',
  178. onOk() {
  179. delete_schema({variables: {schemaName}});
  180. },
  181. onCancel() {
  182. },
  183. });
  184. };
  185. render() {
  186. let userID = this.props.userID;
  187. return (
  188. <Mutation
  189. mutation={gql(DELETE_SCHEMA)}
  190. update={(cache) => {
  191. let data = cache.readQuery({query: gql(SHOW_SCHEMA), variables: {user_id: userID}});
  192. data.schema_by_props.splice(data.schema_by_props.findIndex(obj => obj.schemaName === this.state.schemaName), 1);
  193. cache.writeQuery({
  194. query: gql(SHOW_SCHEMA),
  195. variables: {user_id: userID},
  196. data
  197. });
  198. }}
  199. >
  200. {(delete_schema, {loading, error}) => {
  201. if (error)
  202. return 'error';
  203. if (loading)
  204. return <Spin style={{marginLeft: 3}}/>;
  205. return (
  206. <Button
  207. type="danger"
  208. onClick={() => {
  209. this.showConfirm(delete_schema, this.props.schemaName);
  210. }}
  211. >
  212. delete
  213. </Button>
  214. )
  215. }}
  216. </Mutation>
  217. )
  218. }
  219. }
  220. class DeleteTableButton extends Component {
  221. render() {
  222. let schemaID = this.props.schemaID;
  223. let userID = this.props.userID;
  224. let varobj = {
  225. id: schemaID,
  226. updatedAt: new Date().getTime(),
  227. schemaState: 'updated-delete-table',
  228. };
  229. return (
  230. <Query query={gql(SHOW_TABLE)} variables={{schema_id: schemaID}}>
  231. {
  232. ({loading, error, data}) => {
  233. if (loading)
  234. return <Spin style={{marginLeft: 30, marginTop: 10}}/>;
  235. if (error)
  236. return 'error';
  237. let schemaData = data;
  238. return (
  239. <Mutation
  240. mutation={gql(UPDATE_SCHEMA)}
  241. update={(cache, {data: {update_schema}}) => {
  242. let data = cache.readQuery({
  243. query: gql(SHOW_TABLE),
  244. variables: {schema_id: schemaID}
  245. });
  246. data.schema_by_id = update_schema;
  247. let showSchemaData = cache.readQuery({
  248. query: gql(SHOW_SCHEMA),
  249. variables: {user_id: userID}
  250. });
  251. let schemas = showSchemaData.schema_by_props;
  252. // console.log('schemas', schemas);
  253. let targetSchema = schemas.find(obj => obj.schemaName === update_schema.schemaName);
  254. // console.log('targetSchema', targetSchema);
  255. let targetSchemaIndex = schemas.findIndex(obj => obj.schemaName === update_schema.schemaName);
  256. // console.log('targetSchemaIndex', targetSchemaIndex);
  257. let targetTables = JSON.parse(schemas[targetSchemaIndex].schemaData);
  258. // console.log('targetTables', targetTables);
  259. let targetTableIndex = targetTables.findIndex(obj => obj.name === this.props.currentTable);
  260. // console.log('targetTableIndex', targetTableIndex);
  261. targetTables.splice(targetTableIndex, 1);
  262. // console.log('targetTablesAfterDelete', targetTables);
  263. let temp = {schemaData: JSON.stringify(targetTables)};
  264. // console.log('temp', temp);
  265. let tempSchema = {...targetSchema, ...temp};
  266. // console.log('tempSchema', tempSchema);
  267. showSchemaData.schema_by_props.splice(targetSchemaIndex, 1, tempSchema);
  268. cache.writeQuery(
  269. {
  270. query: gql(SHOW_TABLE),
  271. variables: {schema_id: schemaID},
  272. data
  273. },
  274. {
  275. query: gql(SHOW_SCHEMA),
  276. variables: {user_id: userID},
  277. showSchemaData
  278. }
  279. );
  280. }}
  281. >
  282. {(update_schema, {loading, error}) => {
  283. if (loading)
  284. return <Spin style={{marginLeft: 30, marginTop: 10}}/>;
  285. if (error)
  286. return 'error';
  287. // 先 query 再 mutation,然后删除
  288. let schemaCols;
  289. if (schemaData.schema_by_id === null) schemaCols = [];
  290. else schemaCols = JSON.parse(schemaData.schema_by_id.schemaData);
  291. const index = this.props.currentTableIndex;
  292. if (index === -1) {
  293. console.log('数据库信息不匹配');
  294. } else {
  295. schemaCols.splice(index, 1);
  296. }
  297. return (
  298. <Icon type="delete"
  299. onClick={() => {
  300. update_schema({
  301. variables: {
  302. ...varobj,
  303. schemaData: JSON.stringify(schemaCols)
  304. }
  305. });
  306. }}>
  307. </Icon>
  308. )
  309. }}
  310. </Mutation>
  311. )
  312. }
  313. }
  314. </Query>
  315. )
  316. }
  317. }