Last active
March 30, 2020 17:26
-
-
Save Porter97/a7a62f914a5b8b5aebfb04edd118a77b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { Component } from "react"; | |
import { HashRouter, Route, Switch } from 'react-router-dom'; | |
import { connect } from 'react-redux'; | |
import agent from './agent' | |
import store from './store' | |
import { push } from 'react-router-redux' | |
import Header from './components/Header' | |
import Register from './components/auth/Register.jsx' | |
const mapStateToProps = state => { | |
return { | |
appName: state.common.appName, | |
currentUser: state.common.currentUser, | |
redirectTo: state.common.redirectTo | |
}}; | |
const mapDispatchToProps = dispatch => ({ | |
onLoad: (payload, user) => | |
dispatch({ type: 'APP_LOAD', payload, user}), | |
onRedirect: () => | |
dispatch({ type: 'REDIRECT'}) | |
}); | |
class AppRouter extends Component { | |
componentWillReceiveProps(nextProps) { | |
if (nextProps.redirectTo) { | |
store.dispatch(push(nextProps.redirectTo)); | |
this.props.onRedirect(); | |
} | |
} | |
componentDidMount() { | |
const user = window.localStorage.getItem('currentUser'); | |
this.props.onLoad(user ? agent.Auth.current() : null, user) | |
} | |
render() { | |
return ( | |
<HashRouter> | |
<div className="app"> | |
<Header | |
currentUser={this.props.currentUser} | |
appName={this.props.appName}/> | |
<Switch> | |
<Route path="/register" component={Register} /> | |
</Switch> | |
</div> | |
</HashRouter> | |
) | |
} | |
} | |
export default connect(mapStateToProps, mapDispatchToProps)(AppRouter); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment