yapi/client/Application.js

144 lines
4.2 KiB
JavaScript
Raw Normal View History

import React, { Component } from 'react';
2017-09-29 13:53:53 +08:00
import ReactDOM from 'react-dom';
import { connect } from 'react-redux';
import PropTypes from 'prop-types';
2017-08-11 11:26:49 +08:00
import { Route, BrowserRouter as Router } from 'react-router-dom';
2017-08-20 18:56:31 +08:00
import { Home, Group, Project, Follows, AddProject, Login } from './containers/index';
2017-10-12 17:01:58 +08:00
import { Alert } from 'antd';
import User from './containers/User/User.js';
import Header from './components/Header/Header';
2017-09-21 14:15:42 +08:00
import Footer from './components/Footer/Footer';
import Loading from './components/Loading/Loading';
2017-09-29 13:53:53 +08:00
import MyPopConfirm from './components/MyPopConfirm/MyPopConfirm';
2017-10-27 12:13:59 +08:00
// import statisticsPage from '../exts/yapi-plugin-statistics/statisticsClientPage/index';
2017-08-10 20:08:23 +08:00
import { checkLoginState } from './reducer/modules/user';
import { requireAuthentication } from './components/AuthenticatedComponent';
2017-10-27 12:13:59 +08:00
const plugin = require('client/plugin.js');
2017-07-19 16:24:37 +08:00
const LOADING_STATUS = 0;
2017-10-12 17:01:58 +08:00
const alertContent = () => {
const ua = window.navigator.userAgent,
2017-10-27 12:13:59 +08:00
isChrome = ua.indexOf("Chrome") && window.chrome;
2017-10-12 17:01:58 +08:00
if (!isChrome) {
2017-10-27 12:13:59 +08:00
return <Alert style={{ zIndex: 99 }} message={'YApi 的接口测试等功能仅支持 Chrome 浏览器,请使用 Chrome 浏览器获得完整功能。'} banner closable />
2017-10-12 17:01:58 +08:00
}
}
2017-10-27 12:13:59 +08:00
let AppRoute = {
home: {
path: '/',
component: Home
},
group: {
path: '/group',
component: Group
},
project: {
path: '/project/:id',
component: Project
},
user: {
path: '/user',
component: User
},
follow: {
path: '/follow',
component: Follows
},
addProject: {
path: '/add-project',
component: AddProject
},
login: {
path: '/login',
component: Login
}
};
// 增加路由钩子
plugin.emitHook('app_route', AppRoute);
@connect(
state => {
return {
loginState: state.user.loginState
};
},
{
checkLoginState
}
)
export default class App extends Component {
2017-07-19 16:24:37 +08:00
constructor(props) {
super(props);
2017-07-19 16:24:37 +08:00
this.state = {
login: LOADING_STATUS
};
2017-07-19 16:24:37 +08:00
}
static propTypes = {
checkLoginState: PropTypes.func,
loginState: PropTypes.number
};
2017-08-10 20:08:23 +08:00
componentDidMount() {
this.props.checkLoginState();
}
2017-07-19 16:24:37 +08:00
route = (status) => {
let r;
if (status === LOADING_STATUS) {
return <Loading visible />;
2017-07-19 16:24:37 +08:00
} else {
r = (
2017-09-29 13:53:53 +08:00
<Router getUserConfirmation={(msg, callback) => {
2017-09-29 14:44:00 +08:00
// 自定义 window.confirm
// http://reacttraining.cn/web/api/BrowserRouter/getUserConfirmation-func
2017-09-29 13:53:53 +08:00
let container = document.createElement('div');
document.body.appendChild(container);
ReactDOM.render((
<MyPopConfirm msg={msg} callback={callback} />
), container);
}}>
2017-08-21 15:13:52 +08:00
<div className="g-main">
<div className="router-main">
2017-10-12 17:01:58 +08:00
{alertContent()}
2017-08-21 15:13:52 +08:00
{this.props.loginState !== 1 ? <Header /> : null}
<div className="router-container">
2017-10-27 12:13:59 +08:00
{Object.keys(AppRoute).map(key => {
let item = AppRoute[key];
return (
key === 'login' ?
<Route key={key} path={item.path} component={item.component} />
: key === 'home' ? <Route key={key} exact path={item.path} component={item.component} />
: <Route key={key} path={item.path} component={requireAuthentication(item.component)} />
)
})
}
</div>
{/* <div className="router-container">
2017-08-21 15:13:52 +08:00
<Route exact path="/" component={Home} />
<Route path="/group" component={requireAuthentication(Group)} />
<Route path="/project/:id" component={requireAuthentication(Project)} />
<Route path="/user" component={requireAuthentication(User)} />
<Route path="/follow" component={requireAuthentication(Follows)} />
<Route path="/add-project" component={requireAuthentication(AddProject)} />
<Route path="/login" component={Login} />
2017-10-27 12:13:59 +08:00
{/* <Route path="/statistic" component={statisticsPage} /> */}
{/* </div> */}
2017-07-26 16:20:20 +08:00
</div>
2017-10-27 12:13:59 +08:00
<Footer />
2017-07-19 16:24:37 +08:00
</div>
2017-10-27 12:13:59 +08:00
</Router >
2017-09-19 16:27:52 +08:00
2017-07-19 16:24:37 +08:00
)
}
return r;
2017-07-19 16:24:37 +08:00
}
2017-07-10 19:50:11 +08:00
render() {
return this.route(this.props.loginState);
2017-07-10 19:50:11 +08:00
}
}