Create.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. import React, {Component} from 'react';
  2. import {withRouter} from "react-router-dom";
  3. import {Modal, Input, notification, Spin} from 'antd';
  4. import {Mutation} from "react-apollo";
  5. import gql from "graphql-tag";
  6. import {ADD_PROJECT_AND_SCHEMA, SHOW_PROJECT} from '../../../../../gql'
  7. import './index.css';
  8. import {getCookie} from "../../../../../cookie";
  9. import {idGen, removeSpace} from "../../../../../func";
  10. import {FormattedMessage} from 'react-intl';
  11. class Create extends Component {
  12. constructor(props) {
  13. super(props);
  14. this.state = {
  15. schemaName: '',
  16. schemaID: '',
  17. visible: false,
  18. confirmLoading: false,
  19. };
  20. }
  21. redirectToLogin = () => {
  22. this.props.history.push({
  23. pathname: `/login`,
  24. });
  25. };
  26. componentWillReceiveProps(nextProps) {
  27. this.setState({
  28. visible: nextProps.visible,
  29. });
  30. }
  31. handleOk = (userID, create_project_and_schema) => {
  32. let schemaId = idGen('schema');
  33. let projectId = idGen('project');
  34. let schemaName = this.state.schemaName;
  35. let createdAt = new Date().getTime();
  36. let schemaVarObj = {
  37. schemaId,
  38. schemaName,
  39. user_id: userID,
  40. schemaCreatedAt: createdAt,
  41. schemaUpdatedAt: '',
  42. schemaState: 'create',
  43. schemaData: JSON.stringify([]),
  44. reference: ''
  45. };
  46. let projectVarObj = {
  47. projectCreatedAt: createdAt,
  48. projectUpdatedAt: '',
  49. database_id: '',
  50. apiGWGroup_id: '',
  51. projectName: removeSpace(schemaName),
  52. deploy_id: '',
  53. case_id: '',
  54. projectId,
  55. projectType: 'graphql',
  56. cloud_id: '',
  57. user_id: userID,
  58. wxConfig_id: '',
  59. schema_id: schemaId,
  60. projectStatus: 'created'
  61. };
  62. this.setState({
  63. confirmLoading: true,
  64. });
  65. create_project_and_schema({
  66. variables: {...schemaVarObj, ...projectVarObj},
  67. refetchQueries: [{query: gql(SHOW_PROJECT), variables: {projectType: 'graphql', user_id: userID}}]
  68. });
  69. setTimeout(() => {
  70. this.setState({
  71. visible: false,
  72. confirmLoading: false,
  73. });
  74. }, 1000);
  75. this.props.hideModal();
  76. this.props.switchSidebar(schemaName);
  77. this.props.history.push({
  78. pathname: `/graphql-service/my-create/${schemaName}`,
  79. state: {
  80. schemaName,
  81. schemaID: schemaId,
  82. projectID: projectId,
  83. create: true
  84. }
  85. });
  86. };
  87. render() {
  88. let userID = this.props.userID || getCookie('user_id');
  89. const {visible, confirmLoading} = this.state;
  90. return (
  91. <div>
  92. <Mutation
  93. mutation={gql(ADD_PROJECT_AND_SCHEMA)}
  94. >
  95. {(create_project_and_schema, {loading, error}) => {
  96. if (loading)
  97. return <Spin style={{marginLeft: 30, marginTop: 10}}/>;
  98. if (error)
  99. return 'error';
  100. return (
  101. <FormattedMessage id="Create Graphql Service">
  102. {(msg) => (
  103. <Modal title={msg}
  104. centered
  105. visible={visible}
  106. onOk={() => {
  107. if (userID !== '' && userID !== undefined)
  108. this.handleOk(userID, create_project_and_schema)
  109. }}
  110. confirmLoading={confirmLoading}
  111. onCancel={() => {
  112. this.props.hideModal();
  113. }}
  114. >
  115. <div>
  116. <p><FormattedMessage id="schema name"/></p>
  117. <FormattedMessage id="input schema name">
  118. {(msg) => (
  119. <Input
  120. className='add-input'
  121. placeholder={msg}
  122. onChange={e => {
  123. e.persist();
  124. if (userID === '' || undefined) {
  125. notification.open({
  126. message: '提醒',
  127. description: '需要登录.',
  128. });
  129. this.redirectToLogin();
  130. this.props.hideModal();
  131. } else {
  132. this.setState({
  133. schemaName: e.target.value,
  134. });
  135. }
  136. }}
  137. />
  138. )}
  139. </FormattedMessage>
  140. </div>
  141. </Modal>
  142. )}
  143. </FormattedMessage>
  144. )
  145. }}
  146. </Mutation>
  147. </div>
  148. )
  149. }
  150. }
  151. export default withRouter(Create);