Create.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import React, {Component} from 'react';
  2. import {Modal, Row, Col, Input, Icon, Button, Spin} from 'antd';
  3. import {Mutation, Query} from "react-apollo";
  4. import gql from "graphql-tag";
  5. import {SHOW_SCHEMA, ADD_SCHEMA} from '../../gql'
  6. import './index.css';
  7. const idGen = (kind) => {
  8. return kind + '_' + Date.now() + '_' + Math.random().toString().slice(-8);
  9. };
  10. class Create extends Component {
  11. constructor(props) {
  12. super(props);
  13. this.state = {
  14. schemaName: '',
  15. schemaID: '',
  16. visible: false,
  17. confirmLoading: false,
  18. };
  19. }
  20. componentWillReceiveProps(nextProps) {
  21. this.setState({
  22. visible: nextProps.visible,
  23. });
  24. }
  25. handleOk = (userID, create_schema) => {
  26. let varobj = {
  27. id: idGen('schema'),
  28. user_id: userID,
  29. createdAt: new Date().getTime(),
  30. updatedAt: '',
  31. schemaState: 'create',
  32. schemaData: JSON.stringify([]),
  33. reference: ''
  34. };
  35. this.setState({
  36. confirmLoading: true,
  37. });
  38. create_schema({
  39. variables: {
  40. ...varobj,
  41. schemaName: this.state.schemaName
  42. }
  43. });
  44. setTimeout(() => {
  45. this.setState({
  46. visible: false,
  47. confirmLoading: false,
  48. });
  49. }, 2000);
  50. };
  51. render() {
  52. let userID = this.props.userID || "user_1543305776799_25177768";
  53. console.log('userid', userID);
  54. const {visible, confirmLoading} = this.state;
  55. return (
  56. <div>
  57. <Mutation
  58. mutation={gql(ADD_SCHEMA)}
  59. refetchQueries={[{query: gql(SHOW_SCHEMA), variables: {user_id: userID}}]}
  60. // update={(cache, {data: {create_schema}}) => {
  61. // console.log('cache',cache);
  62. // console.log('data1',cache.readQuery({query: gql(SHOW_SCHEMA), variables: {user_id: userID}}));
  63. // let data = cache.readQuery({query: gql(SHOW_SCHEMA), variables: {user_id: userID}});
  64. // console.log('data',data);
  65. // data.schema_by_props.push(create_schema);
  66. // cache.writeQuery({query: gql(SHOW_SCHEMA), variables: {user_id: userID}, data});
  67. // }}
  68. >
  69. {(create_schema, {loading, error}) => {
  70. if (loading)
  71. return <Spin style={{marginLeft: 30, marginTop: 10}}/>;
  72. if (error)
  73. return 'error';
  74. return (
  75. <Modal title="Create Graphql Service"
  76. centered
  77. visible={visible}
  78. onOk={() => this.handleOk(userID, create_schema)}
  79. confirmLoading={confirmLoading}
  80. onCancel={() => this.props.hideModal()}
  81. >
  82. <div>
  83. <p>schema name</p>
  84. <Input
  85. className='add-input'
  86. placeholder="input schema_name"
  87. onChange={e => {
  88. e.persist();
  89. console.log('schema name', e.target.value);
  90. this.setState({
  91. schemaName: e.target.value,
  92. });
  93. }}
  94. />
  95. </div>
  96. </Modal>
  97. )
  98. }}
  99. </Mutation>
  100. </div>
  101. )
  102. }
  103. }
  104. export default Create;