From 5514c5b1a063cffa85649a9e0a513e337ad50b02 Mon Sep 17 00:00:00 2001 From: samilabud Date: Thu, 26 Nov 2020 23:58:14 -0400 Subject: [PATCH] updating our app for redirect if user logged --- src/App.js | 24 +++++++++++++++--------- src/redux/user/user.actions.js | 4 +++- src/redux/user/user.reducer.js | 4 +++- src/redux/user/user.types.js | 3 +++ 4 files changed, 24 insertions(+), 11 deletions(-) create mode 100644 src/redux/user/user.types.js diff --git a/src/App.js b/src/App.js index 105339b..738f30d 100644 --- a/src/App.js +++ b/src/App.js @@ -1,5 +1,5 @@ import React from 'react'; -import { Route,Switch } from 'react-router-dom'; +import { Route,Switch, Redirect } from 'react-router-dom'; import {connect} from 'react-redux'; @@ -15,9 +15,6 @@ import { setCurrentUser } from './redux/user/user.actions'; class App extends React.Component { - - - unsubscribeFromAuth = null; componentWillUnmount(){ this.unsubscribeFromAuth(); @@ -26,7 +23,6 @@ class App extends React.Component { componentDidMount(){ this.unsubscribeFromAuth = auth.onAuthStateChanged(async userAuth=>{ const { setCurrentUser } = this.props; - //this.setState({currentUser:user}); if(userAuth){ const userRef = await createUserProfileDocument(userAuth); @@ -49,16 +45,26 @@ class App extends React.Component {
- - + + + this.props.currentUser?( + + ):( + + ) + } + /> ); } } - +const mapStateToProps = ({user}) => ({ + currentUser: user.currentUser +}) const mapDispatchProps = dispatch => ({ setCurrentUser: user => dispatch(setCurrentUser(user)) }) -export default connect(null,mapDispatchProps)(App); +export default connect(mapStateToProps,mapDispatchProps)(App); diff --git a/src/redux/user/user.actions.js b/src/redux/user/user.actions.js index 01ade73..81a62ee 100644 --- a/src/redux/user/user.actions.js +++ b/src/redux/user/user.actions.js @@ -1,4 +1,6 @@ +import {UserActionTypes} from './user.types'; + export const setCurrentUser = user =>({ - type: 'SET_CURRENT_USER', + type: UserActionTypes.SET_CURRENT_USER, payload: user }) \ No newline at end of file diff --git a/src/redux/user/user.reducer.js b/src/redux/user/user.reducer.js index bc89bc2..82b886b 100644 --- a/src/redux/user/user.reducer.js +++ b/src/redux/user/user.reducer.js @@ -1,10 +1,12 @@ +import {UserActionTypes} from './user.types'; + const INITIAL_STATE = { currentUser: null }; export const userReducer = (state = INITIAL_STATE, action) => { switch (action.type) { - case 'SET_CURRENT_USER': + case UserActionTypes.SET_CURRENT_USER: return { ...state, currentUser: action.payload diff --git a/src/redux/user/user.types.js b/src/redux/user/user.types.js new file mode 100644 index 0000000..4ae37a5 --- /dev/null +++ b/src/redux/user/user.types.js @@ -0,0 +1,3 @@ +export const UserActionTypes = ({ + SET_CURRENT_USER : 'SET_CURRENT_USER' +}) \ No newline at end of file