|
|
@@ -1,21 +1,37 @@
|
|
|
import React, {Component} from 'react';
|
|
|
import GenerateJs from "../common/generateJs/GenerateJs";
|
|
|
-import {Layout, Menu } from 'antd';
|
|
|
+import {Layout, Menu, Input, Button, Spin} from 'antd';
|
|
|
|
|
|
import Deploy from '../common/deploy/Deploy';
|
|
|
import Schema from '../common/schema/Schema';
|
|
|
import Graphql from "../common/graphql/Graphql";
|
|
|
import CaseNotification from "../common/caseNotification/CaseNotification";
|
|
|
import CaseMetabase from "../common/caseMetabase/CaseMetabase";
|
|
|
+import {ADD_USER, GET_USER} from "../gql";
|
|
|
+import {Query, Mutation} from "react-apollo";
|
|
|
+import fetch from "isomorphic-fetch";
|
|
|
|
|
|
-const { Content } = Layout;
|
|
|
+const {Content} = Layout;
|
|
|
+const idGen = (kind) => {
|
|
|
+ return kind + '_' + Date.now() + '_' + Math.random().toString().slice(-8);
|
|
|
+};
|
|
|
|
|
|
class TrialCase extends Component {
|
|
|
constructor() {
|
|
|
super();
|
|
|
this.state = {
|
|
|
- userID: 'xy_1',
|
|
|
menuLevel2: "schema",
|
|
|
+ shouldShow: false,
|
|
|
+ userID: '',
|
|
|
+ hasLogin: false,
|
|
|
+ hasRegister: false,
|
|
|
+ user: {
|
|
|
+ nickname: '',
|
|
|
+ avatar: ''
|
|
|
+ },
|
|
|
+ username: '',
|
|
|
+ password: '',
|
|
|
+ url: 'http://localhost:8999/resetpassword'
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -26,6 +42,130 @@ class TrialCase extends Component {
|
|
|
});
|
|
|
};
|
|
|
|
|
|
+
|
|
|
+ login = () => {
|
|
|
+ let id = this.state.userID;
|
|
|
+ return (
|
|
|
+ <Query query={GET_USER} variables={{id}}>
|
|
|
+ {
|
|
|
+ ({loading, error, data}) => {
|
|
|
+ if (loading) {
|
|
|
+ return <Spin style={{marginLeft: 3}}/>
|
|
|
+ }
|
|
|
+ if (error) {
|
|
|
+ return 'error!';
|
|
|
+ }
|
|
|
+
|
|
|
+ if (data.user_by_id === null) {
|
|
|
+
|
|
|
+ return (
|
|
|
+ <div>
|
|
|
+ <span style={{marginRight: '10px'}}>no such person</span>
|
|
|
+ <Button onClick={() => {
|
|
|
+ this.setState({hasLogin: false})
|
|
|
+ }}>relogin</Button>
|
|
|
+ </div>
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ else {
|
|
|
+ // 不可使用 this.setState, 会报错。
|
|
|
+ // Warning: Cannot update during an existing state transition (such as within `render`).
|
|
|
+ // Render methods should be a pure function of props and state.
|
|
|
+ // this.state.user = {
|
|
|
+ // avatar: data.user_by_id.avatar,
|
|
|
+ // nickname: data.user_by_id.nickname
|
|
|
+ // };
|
|
|
+
|
|
|
+ return (
|
|
|
+ <div>
|
|
|
+ <span style={{marginRight: '10px'}}>welcome, {data.user_by_id.nickname}</span>
|
|
|
+ <Button onClick={() => {
|
|
|
+ this.setState({hasLogin: false})
|
|
|
+ }}>exit</Button>
|
|
|
+ <Button onClick={() => {
|
|
|
+ this.setState({shouldShow: true})
|
|
|
+ }}>go to use</Button>
|
|
|
+ </div>
|
|
|
+ );
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ </Query>
|
|
|
+ )
|
|
|
+ };
|
|
|
+
|
|
|
+ register = () => {
|
|
|
+ return (
|
|
|
+ <Mutation mutation={ADD_USER}>
|
|
|
+ {(create_user, {loading, error}) => {
|
|
|
+ if (error)
|
|
|
+ return 'error';
|
|
|
+ if (loading)
|
|
|
+ return <Spin style={{marginLeft: 3}}/>;
|
|
|
+ return (
|
|
|
+ <div style={{marginTop: 20}}>
|
|
|
+ <span style={{marginRight: 20}}><b>register:</b></span>
|
|
|
+ <div style={{marginTop: 10}}>
|
|
|
+ <span>username: </span>
|
|
|
+ <Input
|
|
|
+ placeholder=""
|
|
|
+ onChange={(e) => {
|
|
|
+ // antd 获取不到 target,百度来的下面这句代码
|
|
|
+ e.persist();
|
|
|
+ this.setState({username: e.target.value});
|
|
|
+ }}
|
|
|
+ style={{width: 200}}
|
|
|
+ />
|
|
|
+ </div>
|
|
|
+ <div style={{marginTop: 10}}>
|
|
|
+ <span>password: </span>
|
|
|
+ <Input
|
|
|
+ placeholder=""
|
|
|
+ onChange={(e) => {
|
|
|
+ // antd 获取不到 target,百度来的下面这句代码
|
|
|
+ e.persist();
|
|
|
+ this.setState({password: e.target.value});
|
|
|
+ }}
|
|
|
+ style={{width: 200}}
|
|
|
+ />
|
|
|
+ </div>
|
|
|
+ <Button type='primary' onClick={() => {
|
|
|
+ let id = idGen('userID');
|
|
|
+ create_user({
|
|
|
+ variables: {
|
|
|
+ id,
|
|
|
+ email: '',
|
|
|
+ updatedAt: '',
|
|
|
+ password: '',
|
|
|
+ telephone: '',
|
|
|
+ nickname: '',
|
|
|
+ username: this.state.username,
|
|
|
+ createdAt: new Date().getTime(),
|
|
|
+ openid: '',
|
|
|
+ avatar: ''
|
|
|
+ }
|
|
|
+ });
|
|
|
+ fetch(this.state.url,{
|
|
|
+ method: 'POST',
|
|
|
+ mode: 'cors',
|
|
|
+ body: {
|
|
|
+ 'user-id': id,
|
|
|
+ 'password': this.state.password,
|
|
|
+ 'token': 'WXgraphql4Io',
|
|
|
+ }
|
|
|
+ }).then(res => res.json())
|
|
|
+ .catch(error => console.error('Error:', error))
|
|
|
+ .then(response => console.log('Success:', response));
|
|
|
+ }}>ok</Button>
|
|
|
+ </div>
|
|
|
+ );
|
|
|
+ }}
|
|
|
+ </Mutation>
|
|
|
+
|
|
|
+ )
|
|
|
+ };
|
|
|
+
|
|
|
render() {
|
|
|
return (
|
|
|
<div>
|
|
|
@@ -44,26 +184,65 @@ class TrialCase extends Component {
|
|
|
<Menu.Item key="notification">notification</Menu.Item>
|
|
|
</Menu>
|
|
|
|
|
|
- <Layout style={{ padding: '24px', zIndex: '0'}}>
|
|
|
- <Content style={{ padding: '24px', minHeight: 280, background: '#fff',marginTop: '48px' }}>
|
|
|
- {(() => {
|
|
|
- switch (this.state.menuLevel2) {
|
|
|
- case 'schema':
|
|
|
- return <Schema userID={this.state.userID}/>;
|
|
|
- case 'deploy':
|
|
|
- return <Deploy userID={this.state.userID}/>;
|
|
|
- case 'graphql':
|
|
|
- return <GenerateJs />;
|
|
|
- case 'graphiql':
|
|
|
- return <Graphql />;
|
|
|
- case 'metabase':
|
|
|
- return <CaseMetabase />;
|
|
|
- case 'notification':
|
|
|
- return <CaseNotification />;
|
|
|
- default:
|
|
|
- return <Graphql />
|
|
|
- }
|
|
|
- })()}
|
|
|
+
|
|
|
+ <Layout style={{padding: '24px', zIndex: '0'}}>
|
|
|
+ <Content style={{padding: '24px', minHeight: 280, background: '#fff', marginTop: '48px'}}>
|
|
|
+ {
|
|
|
+ this.state.shouldShow === false ?
|
|
|
+ <div>
|
|
|
+ <div className='login'>
|
|
|
+ {
|
|
|
+ this.state.hasLogin ?
|
|
|
+ this.login()
|
|
|
+ :
|
|
|
+ <div>
|
|
|
+ <span style={{marginRight: 20}}><b>login:</b></span>
|
|
|
+ <Input
|
|
|
+ placeholder="id, eg: 'xy_1'"
|
|
|
+ onChange={(e) => {
|
|
|
+ // antd 获取不到 target,百度来的下面这句代码
|
|
|
+ e.persist();
|
|
|
+ this.setState({userID: e.target.value});
|
|
|
+ }}
|
|
|
+ onPressEnter={() => {
|
|
|
+ this.setState({hasLogin: true})
|
|
|
+ }}
|
|
|
+ style={{width: 200}}
|
|
|
+ />
|
|
|
+ </div>
|
|
|
+ }
|
|
|
+ </div>
|
|
|
+ <div className='register'>
|
|
|
+ {
|
|
|
+ this.register()
|
|
|
+ }
|
|
|
+ </div>
|
|
|
+
|
|
|
+ </div>
|
|
|
+
|
|
|
+ :
|
|
|
+
|
|
|
+ (() => {
|
|
|
+ switch (this.state.menuLevel2) {
|
|
|
+ case 'schema':
|
|
|
+ return <Schema userID={this.state.userID}/>;
|
|
|
+ case 'deploy':
|
|
|
+ return <Deploy userID={this.state.userID}/>;
|
|
|
+ case 'graphql':
|
|
|
+ return <GenerateJs/>;
|
|
|
+ case 'graphiql':
|
|
|
+ return <Graphql/>;
|
|
|
+ case 'metabase':
|
|
|
+ return <CaseMetabase/>;
|
|
|
+ case 'notification':
|
|
|
+ return <CaseNotification/>;
|
|
|
+ default:
|
|
|
+ return <Graphql/>
|
|
|
+ }
|
|
|
+
|
|
|
+ })()
|
|
|
+ }
|
|
|
+
|
|
|
</Content>
|
|
|
</Layout>
|
|
|
</div>
|
|
|
@@ -71,4 +250,5 @@ class TrialCase extends Component {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-export default TrialCase;
|
|
|
+export default TrialCase;
|
|
|
+
|