From a4a824031af39960ea88aa51c6962c54d6c348b5 Mon Sep 17 00:00:00 2001 From: "yhui.yang" Date: Mon, 24 Jul 2017 11:35:31 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E6=9C=AA=E7=99=BB?= =?UTF-8?q?=E5=BD=95=E6=97=B6=E7=9A=84=E8=B7=AF=E7=94=B1=E5=9C=B0=E5=9D=80?= =?UTF-8?q?=E6=9C=AA=E6=94=B9=E5=8F=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- client/Application.js | 66 +++++++-------------- client/components/AuthenticatedComponent.js | 55 +++++++++++++++++ client/components/Header/Header.js | 10 +--- client/reducer/menu/menu.js | 2 +- 4 files changed, 81 insertions(+), 52 deletions(-) create mode 100644 client/components/AuthenticatedComponent.js diff --git a/client/Application.js b/client/Application.js index b207ccc5..72855d8a 100644 --- a/client/Application.js +++ b/client/Application.js @@ -1,19 +1,26 @@ import React, { Component } from 'react' import { connect } from 'react-redux' import PropTypes from 'prop-types' -import { Route, HashRouter, Redirect, Switch } from 'react-router-dom' +import { Route, HashRouter } from 'react-router-dom' import { Home, ProjectGroups, Interface, News, AddInterface } from './containers/index' import User from './containers/User/User.js' import Header from './components/Header/Header' import { checkLoginState } from './actions/login' +import { requireAuthentication } from './components/AuthenticatedComponent'; const LOADING_STATUS = 0; -const GUEST_STATUS = 1; -// const MEMBER_STATUS = 2; - - -class App extends Component { +@connect( + state => { + return{ + loginState:state.login.loginState + } + }, + { + checkLoginState + } +) +export default class App extends Component { constructor(props) { super(props); this.state = { @@ -24,59 +31,32 @@ class App extends Component { checkLoginState:PropTypes.func, loginState:PropTypes.number } + + componentDidMount() { + this.props.checkLoginState(); + } route = (status) => { let r; if (status === LOADING_STATUS) { return loading... - } else if (status === GUEST_STATUS) { - r = ( - -
-
- - - - -
-
- ) } else { r = (
- - - - - + + + + +
) } return r - } - - componentDidMount() { - this.props.checkLoginState(); - } - render() { - return this.route(this.props.loginState) + return this.route(this.props.loginState); } } - -export default connect( - state => { - return{ - loginState:state.login.loginState - } - }, - { - checkLoginState - } -)(App) diff --git a/client/components/AuthenticatedComponent.js b/client/components/AuthenticatedComponent.js new file mode 100644 index 00000000..2932c6ca --- /dev/null +++ b/client/components/AuthenticatedComponent.js @@ -0,0 +1,55 @@ +import React from 'react'; +import { connect } from 'react-redux'; +import PropTypes from 'prop-types' +import { changeMenuItem } from '../actions/menu' + + +export function requireAuthentication(Component) { + class AuthenticatedComponent extends React.Component { + constructor(props){ + super(props); + } + static propTypes ={ + isAuthenticated : PropTypes.bool, + location: PropTypes.object, + dispatch: PropTypes.func, + history: PropTypes.object, + changeMenuItem:PropTypes.func + } + componentWillMount() { + this.checkAuth(); + } + componentWillReceiveProps() { + this.checkAuth(); + } + checkAuth() { + if( !this.props.isAuthenticated ){ + this.props.history.push('/'); + this.props.changeMenuItem('/'); + } + } + render() { + return ( +
+ {this.props.isAuthenticated + ? + : null + } +
+ ) + + } + } + return connect( + (state) => { + return{ + isAuthenticated: state.login.isLogin + } + }, + { + changeMenuItem + } + )(AuthenticatedComponent); +} + + diff --git a/client/components/Header/Header.js b/client/components/Header/Header.js index fa252fc5..91816dde 100644 --- a/client/components/Header/Header.js +++ b/client/components/Header/Header.js @@ -85,16 +85,9 @@ class HeaderCom extends Component { } linkTo = (e) =>{ this.props.changeMenuItem(e.key); - // this.props.curKey = e.key; - // this.setState({ - // current : e.key - // }) } relieveLink = () => { this.props.changeMenuItem(""); - // this.setState({ - // current : "" - // }) } logout = (e) => { e.preventDefault(); @@ -129,9 +122,10 @@ class HeaderCom extends Component { } render () { const { login, user, msg, uid, curKey } = this.props; + console.log(curKey); return ( - +
diff --git a/client/reducer/menu/menu.js b/client/reducer/menu/menu.js index 858c7add..1231e1f2 100644 --- a/client/reducer/menu/menu.js +++ b/client/reducer/menu/menu.js @@ -3,7 +3,7 @@ import { } from '../../constants/action-types.js' const initialState = { - curKey: window.location.hash.split("#")[1] + curKey: window.location.hash.split("#")[1] || '/' } export default (state = initialState, action) => {