瀏覽代碼

add route

xy 7 年之前
父節點
當前提交
ed95467f27
共有 4 個文件被更改,包括 261 次插入230 次删除
  1. 4 44
      README.md
  2. 2 0
      package.json
  3. 254 184
      src/components/app/App.jsx
  4. 1 2
      src/index.js

+ 4 - 44
README.md

@@ -1,46 +1,6 @@
-This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).
+# online
 
-## Available Scripts
 
-In the project directory, you can run:
-
-### `npm install`
-
-### `npm start`
-
-Runs the app in the development mode.<br>
-Open [http://localhost:3000](http://localhost:3000) to view it in the browser.
-
-The page will reload if you make edits.<br>
-You will also see any lint errors in the console.
-
-### `npm test`
-
-Launches the test runner in the interactive watch mode.<br>
-See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.
-
-### `npm run build`
-
-Builds the app for production to the `build` folder.<br>
-It correctly bundles React in production mode and optimizes the build for the best performance.
-
-The build is minified and the filenames include the hashes.<br>
-Your app is ready to be deployed!
-
-See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.
-
-### `npm run eject`
-
-**Note: this is a one-way operation. Once you `eject`, you can’t go back!**
-
-If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project.
-
-Instead, it will copy all the configuration files and the transitive dependencies (Webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own.
-
-You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it.
-
-## Learn More
-
-You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started).
-
-To learn React, check out the [React documentation](https://reactjs.org/).
+## 注意
+1. index.js 里面的 localhost 端口号记得修改
+2. 需要重新 npm install / yarn

+ 2 - 0
package.json

@@ -51,6 +51,8 @@
     "react-app-polyfill": "^0.1.3",
     "react-dev-utils": "^6.1.0",
     "react-dom": "^16.6.0",
+    "react-router": "^4.3.1",
+    "react-router-dom": "^4.3.1",
     "resolve": "1.8.1",
     "sass-loader": "7.1.0",
     "style-loader": "0.23.0",

+ 254 - 184
src/components/app/App.jsx

@@ -2,24 +2,26 @@ import React, {Component} from 'react';
 import Deploy from '../common/deploy/Deploy';
 import Schema from '../common/schema/Schema';
 import Graphql from "../common/graphql/Graphql";
-import {Layout, Menu, Input, Button, Spin,Breadcrumb, Icon } from 'antd';
+
+import {Layout, Menu, Input, Button, Spin, Breadcrumb, Icon} from 'antd';
+import {BrowserRouter as Router, Route, Link} from "react-router-dom";
+
+import {Query} from "react-apollo";
+import gql from "graphql-tag";
 
 import '../common/graphql/index.css';
 import logo from '../../images/logo.png';
 import './index.css'
-import gql from "graphql-tag";
-import {Query} from "react-apollo";
 
-const { SubMenu } = Menu;
-const { Header, Content, Sider } = Layout;
+const {SubMenu} = Menu;
+const {Header, Content, Sider} = Layout;
 
 class App extends Component {
     constructor() {
         super();
         this.state = {
-            menuLevel1:"cloud-function",
-            menuLevel2:"schema",
-            sideBar:"e-commerce",
+            menuLevel1: "cloud-function",
+            sideBar: "e-commerce",
             userID: '',
             hasLogin: false,
             user: {
@@ -27,16 +29,17 @@ class App extends Component {
                 avatar: ''
             },
             collapsed: false,
-            inlineCollapsed:false
+            inlineCollapsed: false
         };
     }
+
     onCollapse = (collapsed) => {
         console.log(collapsed);
-        this.setState({ collapsed });
+        this.setState({collapsed});
     };
 
-    switchMenu = (menuName,e) => {
-        console.log('menuName',menuName,'e',e);
+    switchMenu = (menuName, e) => {
+        console.log('menuName', menuName, 'e', e);
         this.setState({
             [menuName]: e.key,
         });
@@ -65,10 +68,10 @@ class App extends Component {
                         if (data.user_by_id === null)
                             return (
                                 <div>
-                                  <span style={{marginRight: '10px'}}>no such person</span>
-                                  <Button onClick={() => {
-                                      this.setState({hasLogin: false})
-                                  }}>relogin</Button>
+                                    <span style={{marginRight: '10px'}}>no such person</span>
+                                    <Button onClick={() => {
+                                        this.setState({hasLogin: false})
+                                    }}>relogin</Button>
                                 </div>
                             );
                         else {
@@ -82,10 +85,10 @@ class App extends Component {
                             console.log(data);
                             return (
                                 <div>
-                                  <span style={{marginRight: '10px'}}>welcome, {data.user_by_id.nickname}</span>
-                                  <Button onClick={() => {
-                                      this.setState({hasLogin: false})
-                                  }}>exit</Button>
+                                    <span style={{marginRight: '10px'}}>welcome, {data.user_by_id.nickname}</span>
+                                    <Button onClick={() => {
+                                        this.setState({hasLogin: false})
+                                    }}>exit</Button>
                                 </div>
                             );
                         }
@@ -97,182 +100,249 @@ class App extends Component {
 
     render() {
         return (
-            <Layout style={{ minHeight: '100vh' }}>
-              <Header className="header" style={{ position: 'fixed', zIndex: 1, width: '100%' }}>
-                <div className="logo" />
+            <Router>
+                <Layout style={{minHeight: '100vh'}}>
+
+                    <Header className="header" style={{position: 'fixed', zIndex: 1, width: '100%'}}>
+                        <div className="logo"/>
+                        <Menu
+                            theme="dark"
+                            mode="horizontal"
+                            defaultSelectedKeys={['cloud-function']}
+                            style={{lineHeight: '64px'}}
+                            onClick={(e) => this.switchMenu('menuLevel1', e)}
+                        >
+                            <Menu.Item key="cloud-function"><Link to="/cloud-function/">Cloud Function</Link></Menu.Item>
+                            <Menu.Item key="data-storage"><Link to="/data-storage/">Data Storage</Link></Menu.Item>
+                            <Menu.Item key="data-analysis"><Link to="/data-analysis/">Data Analysis</Link></Menu.Item>
+                            <Menu.Item key="monitor-notification"><Link to="/monitor-notification/">Monitor Notification</Link></Menu.Item>
+                        </Menu>
+
+                        {/*<div className='login'>*/}
+                            {/*{*/}
+                                {/*this.state.hasLogin ?*/}
+                                    {/*this.login(this.state.userID)*/}
+                                    {/*:*/}
+                                    {/*<div>*/}
+                                        {/*<span style={{marginRight: 20}}>login:</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>*/}
+                    </Header>
+
+                    <Layout>
+                        <Sider
+                            width={200}
+                            style={{background: '#fff', marginTop: '64px', zIndex: '0'}}
+                            collapsible
+                            collapsed={this.state.collapsed}
+                            onCollapse={this.onCollapse}
+                        >
+                            <Menu
+                                theme="dark"
+                                mode="inline"
+                                inlineCollapsed={this.state.inlineCollapsed}
+                                defaultSelectedKeys={['e-commerce']}
+                                defaultOpenKeys={['trial-case']}
+                                // openKeys={['cloud-function']}
+                                onClick={(e) => this.switchMenu('sideBar', e)}
+                                selectedKeys={[this.state.sideBar]}
+                                style={{
+                                    borderRight: 0,
+                                    overflow: 'auto',
+                                    height: '100vh',
+                                    left: '0',
+                                    width: '200px',
+                                    position: 'fixed'
+                                }}
+                            >
+                                <SubMenu key="trial-case" title={<span><Icon type="appstore" theme="twoTone"/><span>Trial Case</span></span>}>
+                                    <Menu.Item key="e-commerce">e-commerce</Menu.Item>
+                                    <Menu.Item key="keep accounts">keep accounts</Menu.Item>
+                                    <Menu.Item key="appointment">appointment</Menu.Item>
+                                </SubMenu>
+
+                                {(() => {
+                                    switch (this.state.menuLevel1) {
+                                        case 'cloud-function':
+                                            return (
+                                                <SubMenu
+                                                    key="cloud-function"
+                                                    title={<span><Icon type="cloud"
+                                                                       theme="twoTone"/><span>Cloud Function</span></span>}>
+                                                    <Menu.Item key="wechat-service">wechat-service</Menu.Item>
+                                                    <Menu.Item key="graphql-service">graphql-service</Menu.Item>
+                                                    <Menu.Item
+                                                        key="quantization-service">quantization-service</Menu.Item>
+                                                </SubMenu>
+                                            );
+                                        case 'data-storage':
+                                            return (
+                                                <SubMenu
+                                                    key="data-storage"
+                                                    title={<span><Icon type="database"
+                                                                       theme="twoTone"/><span>Data Storage</span></span>}>
+                                                    <Menu.Item key="snbl-mongo">snbl-mongo</Menu.Item>
+                                                </SubMenu>
+                                            );
+                                        case 'data-analysis':
+                                            return (
+                                                <SubMenu
+                                                    key="data-analysis"
+                                                    title={<span><Icon type="fund"
+                                                                       theme="twoTone"/><span>data Analysis</span></span>}>
+                                                    <Menu.Item key="metabase">metabase</Menu.Item>
+                                                </SubMenu>
+                                            );
+                                        case 'monitor-notification':
+                                            return (
+                                                <SubMenu
+                                                    key="monitor-notification"
+                                                    title={<span><Icon type="alert" theme="twoTone"/><span>Monitor Notification</span></span>}>
+                                                    <Menu.Item key="monitor">monitor</Menu.Item>
+                                                    <Menu.Item key="graphql-events">graphql-events</Menu.Item>
+                                                    <Menu.Item key="stock-signal">stock-signal</Menu.Item>
+                                                </SubMenu>
+                                            );
+                                        default:
+                                            return (
+                                                <SubMenu
+                                                    key="cloud-function"
+                                                    title={<span><Icon type="cloud"
+                                                                       theme="twoTone"/><span>Cloud Function</span></span>}>
+                                                    <Menu.Item key="wechat-service">wechat-service</Menu.Item>
+                                                    <Menu.Item key="graphql-service">graphql-service</Menu.Item>
+                                                    <Menu.Item
+                                                        key="quantization-service">quantization-service</Menu.Item>
+                                                </SubMenu>
+                                            );
+                                    }
+                                })()}
+                                <SubMenu key="my-create"
+                                         title={<span><Icon type="user"
+                                                            theme="outlined"/><span>My Create</span></span>}>
+                                    <Menu.Item key="8">option9</Menu.Item>
+                                </SubMenu>
+                            </Menu>
+                        </Sider>
+
+                        <Layout style={{marginTop: '64px'}}>
+                            <Route path="/" exact component={CloudFunction}/>
+                            <Route path="/cloud-function/" component={CloudFunction}/>
+                            <Route path="/data-storage/" component={DataStorge}/>
+                            <Route path="/data-analysis/" component={DataAnalysis}/>
+                            <Route path="/monitor-notification/" component={MonitorNntification}/>
+                        </Layout>
+
+                    </Layout>
+                </Layout>
+            </Router>
+        );
+    }
+}
+
+export default App;
+
+class CloudFunction extends Component {
+    constructor() {
+        super();
+        this.state = {
+            menuLevel2: "schema",
+        }
+    }
+
+    switchMenu = (menuName, e) => {
+        console.log('menuName', menuName, 'e', e);
+        this.setState({
+            [menuName]: e.key,
+        });
+    };
+
+    render() {
+        return (
+            <div>
                 <Menu
-                    theme="dark"
                     mode="horizontal"
-                    defaultSelectedKeys={['cloud-function']}
-                    style={{ lineHeight: '64px' }}
-                    onClick={(e)=>this.switchMenu('menuLevel1',e)}
-                >
-                  <Menu.Item key="cloud-function">Cloud Function</Menu.Item>
-                  <Menu.Item key="data-storage">Data Storage</Menu.Item>
-                  <Menu.Item key="data-analysis">Data Analysis</Menu.Item>
-                  <Menu.Item key="monitor-notification">Monitor Notification</Menu.Item>
-                </Menu>
-                {/*<div className='login'>*/}
-                    {/*{*/}
-                        {/*this.state.hasLogin ?*/}
-                            {/*this.login(this.state.userID)*/}
-                            {/*:*/}
-                            {/*<div>*/}
-                              {/*<span style={{marginRight: 20}}>login:</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>*/}
-              </Header>
-
-              <Layout>
-                <Sider
-                    width={200}
-                    style={{ background: '#fff',marginTop:'64px'}}
-                    collapsible
-                    collapsed={this.state.collapsed}
-                    onCollapse={this.onCollapse}
+                    defaultSelectedKeys={['schema']}
+                    style={{padding: '0 24px', position: 'fixed', width: '100%', zIndex: '1'}}
+                    onClick={(e) => this.switchMenu('menuLevel2', e)}
+                    selectedKeys={[this.state.menuLevel2]}
                 >
-                  <Menu
-                      theme="dark"
-                      mode="inline"
-                      inlineCollapsed={this.state.inlineCollapsed}
-                      defaultSelectedKeys={['e-commerce']}
-                      defaultOpenKeys={['trial-case']}
-                      // openKeys={['cloud-function']}
-                      onClick={(e)=>this.switchMenu('sideBar',e)}
-                      selectedKeys={[this.state.sideBar]}
-                      style={{
-                          borderRight: 0,
-                          overflow: 'auto',
-                          height: '100vh',
-                          left: '0',
-                          width: '200px',
-                          position: 'fixed'}}
-                  >
-                    <SubMenu key="trial-case" title={<span><Icon type="appstore" theme="twoTone" /><span>Trial Case</span></span>}>
-                      <Menu.Item key="e-commerce">e-commerce</Menu.Item>
-                      <Menu.Item key="keep accounts">keep accounts</Menu.Item>
-                      <Menu.Item key="appointment">appointment</Menu.Item>
-                    </SubMenu>
-                      {(() => {
-                          switch (this.state.menuLevel1) {
-                              case 'cloud-function':
-                                  return (
-                                      <SubMenu
-                                          key="cloud-function"
-                                          title={<span><Icon type="cloud" theme="twoTone" /><span>Cloud Function</span></span>}>
-                                        <Menu.Item key="wechat-service">wechat-service</Menu.Item>
-                                        <Menu.Item key="graphql-service">graphql-service</Menu.Item>
-                                        <Menu.Item key="quantization-service">quantization-service</Menu.Item>
-                                      </SubMenu>
-                                  );
-                              case 'data-storage':
-                                  return (
-                                      <SubMenu
-                                          key="data-storage"
-                                          title={<span><Icon type="database" theme="twoTone" /><span>Data Storage</span></span>}>
-                                        <Menu.Item key="snbl-mongo">snbl-mongo</Menu.Item>
-                                      </SubMenu>
-                                  );
-                              case 'data-analysis':
-                                  return (
-                                      <SubMenu
-                                          key="data-analysis"
-                                          title={<span><Icon type="fund" theme="twoTone" /><span>data Analysis</span></span>}>
-                                        <Menu.Item key="metabase">metabase</Menu.Item>
-                                      </SubMenu>
-                                  );
-                              case 'monitor-notification':
-                                  return (
-                                      <SubMenu
-                                          key="monitor-notification"
-                                          title={<span><Icon type="alert" theme="twoTone" /><span>Monitor Notification</span></span>}>
-                                        <Menu.Item key="monitor">monitor</Menu.Item>
-                                        <Menu.Item key="graphql-events">graphql-events</Menu.Item>
-                                        <Menu.Item key="stock-signal">stock-signal</Menu.Item>
-                                      </SubMenu>
-                                  );
-                              default:
-                                  return (
-                                      <SubMenu
-                                          key="cloud-function"
-                                          title={<span><Icon type="cloud" theme="twoTone" /><span>Cloud Function</span></span>}>
-                                        <Menu.Item key="wechat-service">wechat-service</Menu.Item>
-                                        <Menu.Item key="graphql-service">graphql-service</Menu.Item>
-                                        <Menu.Item key="quantization-service">quantization-service</Menu.Item>
-                                      </SubMenu>
-                                  );
-                          }
-                      })()}
-                    <SubMenu key="my-create" title={<span><Icon type="user" theme="outlined" /><span>My Create</span></span>}>
-                      <Menu.Item key="8">option9</Menu.Item>
-                    </SubMenu>
-                  </Menu>
-                </Sider>
-
-                <Layout style={{marginTop: '64px' }}>
-                  <Menu
-                      mode="horizontal"
-                      defaultSelectedKeys={['schema']}
-                      style={{padding: '0 24px',position: 'fixed', width: '100%', zIndex: '1' }}
-                      onClick={(e)=>this.switchMenu('menuLevel2',e)}
-                      selectedKeys={[this.state.menuLevel2]}
-                  >
                     <Menu.Item key="schema">schema</Menu.Item>
                     <Menu.Item key="deploy">deploy</Menu.Item>
                     <Menu.Item key="graphql">graphql</Menu.Item>
                     <Menu.Item key="graphiql">graphiql</Menu.Item>
                     <Menu.Item key="metabase">metabase</Menu.Item>
                     <Menu.Item key="notification">notification</Menu.Item>
-                  </Menu>
+                </Menu>
 
-                  <Layout style={{ padding: '24px', zIndex: '0'}}>
-                      {/*<Sider width={200} style={{ background: '#fff' }}>*/}
-                      {/*<Menu*/}
-                      {/*mode="inline"*/}
-                      {/*defaultSelectedKeys={['1']}*/}
-                      {/*defaultOpenKeys={['sub1']}*/}
-                      {/*style={{ height: '100%' }}*/}
-                      {/*>*/}
-                      {/*<Menu.Item key="1">Query</Menu.Item>*/}
-                      {/*<Menu.Item key="2">Mutation</Menu.Item>*/}
-                      {/*</Menu>*/}
-                      {/*</Sider>*/}
-                    <Content style={{ padding: '24px', minHeight: 280, background: '#fff',marginTop: '48px'  }}>
-                      <div>
-                          {(() => {
-                              switch (this.state.menuLevel2) {
-                                  case 'schema':
-                                      return <Schema userID={this.state.userID}/>;
-                                  case 'deploy':
-                                      return <Deploy />;
-                                  case 'graphiql':
-                                      return <Graphql />;
-                                  default:
-                                      return <Graphql />
-                              }
-                          })()}
-                      </div>
+                <Layout style={{padding: '24px', zIndex: '0'}}>
+                    {/*<Sider width={200} style={{ background: '#fff' }}>*/}
+                    {/*<Menu*/}
+                    {/*mode="inline"*/}
+                    {/*defaultSelectedKeys={['1']}*/}
+                    {/*defaultOpenKeys={['sub1']}*/}
+                    {/*style={{ height: '100%' }}*/}
+                    {/*>*/}
+                    {/*<Menu.Item key="1">Query</Menu.Item>*/}
+                    {/*<Menu.Item key="2">Mutation</Menu.Item>*/}
+                    {/*</Menu>*/}
+                    {/*</Sider>*/}
+                    <Content
+                        style={{padding: '24px', minHeight: 280, background: '#fff', marginTop: '48px'}}>
+                        <div>
+                            {(() => {
+                                switch (this.state.menuLevel2) {
+                                    case 'schema':
+                                        return <Schema userID={this.state.userID}/>;
+                                    case 'deploy':
+                                        return <Deploy/>;
+                                    case 'graphiql':
+                                        return <Graphql/>;
+                                    default:
+                                        return <Graphql/>
+                                }
+                            })()}
+                        </div>
                     </Content>
-                  </Layout>
-
                 </Layout>
-              </Layout>
-            </Layout>
-        );
+            </div>
+        )
     }
 }
 
-export default App;
+function DataStorge() {
+    return (
+        <div>
+            DataStorge
+        </div>
+    )
+}
+
+function DataAnalysis() {
+    return (
+        <div>
+            DataAnalysis
+        </div>
+    )
+}
+
+function MonitorNntification() {
+    return (
+        <div>
+            MonitorNntification
+        </div>
+    )
+}

+ 1 - 2
src/index.js

@@ -8,9 +8,8 @@ import ApolloClient from "apollo-boost";
 import { ApolloProvider } from "react-apollo";
 
 const client = new ApolloClient({
-  // xySchema api
   // uri: "http://service-eucrnpse-1254337200.ap-guangzhou.apigateway.myqcloud.com/release/graphql"
-  uri: "http://localhost:3000/graphql"
+  uri: "http://localhost:4000/graphql"
 });
 
 ReactDOM.render(<ApolloProvider client={client}><App /></ApolloProvider>, document.getElementById('root'));