Create.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. // console.log('nextProps',nextProps);
  22. this.setState({
  23. visible: nextProps.visible,
  24. });
  25. }
  26. switchSchema = (name, id) => {
  27. this.setState({
  28. currentSchema: name,
  29. schemaID: id
  30. });
  31. };
  32. handleOk = (userID,create_schema) => {
  33. let varobj = {
  34. id: idGen('schema'),
  35. user_id: userID,
  36. createdAt: new Date().getTime(),
  37. updatedAt: '',
  38. schemaState: 'create',
  39. schemaData: JSON.stringify([]),
  40. reference: ''
  41. };
  42. this.setState({
  43. confirmLoading: true,
  44. });
  45. create_schema({
  46. variables: {
  47. ...varobj,
  48. schemaName: this.state.schemaName
  49. }
  50. });
  51. setTimeout(() => {
  52. this.setState({
  53. visible: false,
  54. confirmLoading: false,
  55. });
  56. }, 2000);
  57. };
  58. render() {
  59. let userID = this.props.userID || "user_1543305776799_25177768";
  60. console.log('userid',userID);
  61. const { visible, confirmLoading } = this.state;
  62. return (
  63. <div>
  64. <Mutation
  65. mutation={gql(ADD_SCHEMA)}
  66. refetchQueries={[{query:gql(SHOW_SCHEMA), variables:{user_id: userID}}]}
  67. // update={(cache, {data: {create_schema}}) => {
  68. // console.log('cache',cache);
  69. // console.log('data1',cache.readQuery({query: gql(SHOW_SCHEMA), variables: {user_id: userID}}));
  70. // let data = cache.readQuery({query: gql(SHOW_SCHEMA), variables: {user_id: userID}});
  71. // console.log('data',data);
  72. // data.schema_by_props.push(create_schema);
  73. // cache.writeQuery({query: gql(SHOW_SCHEMA), variables: {user_id: userID}, data});
  74. // }}
  75. >
  76. {(create_schema, {loading, error}) => {
  77. if (loading)
  78. return <Spin style={{marginLeft: 30, marginTop: 10}}/>;
  79. if (error)
  80. return 'error';
  81. return (
  82. <Modal title="Create Graphql Service"
  83. centered
  84. visible={visible}
  85. onOk={()=>this.handleOk(userID,create_schema)}
  86. confirmLoading={confirmLoading}
  87. onCancel={()=>this.props.hideModal()}
  88. >
  89. <div>
  90. <p>schema name</p>
  91. <Input
  92. className='add-input'
  93. placeholder="input schema_name"
  94. onChange={e => {
  95. e.persist();
  96. console.log('schema name',e.target.value);
  97. this.setState({
  98. schemaName: e.target.value,
  99. });
  100. }}
  101. />
  102. </div>
  103. </Modal>
  104. )
  105. }}
  106. </Mutation>
  107. </div>
  108. )
  109. }
  110. }
  111. export default Create;