Skip to content

Commit

Permalink
updating our app for redirect if user logged
Browse files Browse the repository at this point in the history
  • Loading branch information
samilabud committed Nov 27, 2020
1 parent 625df6b commit 5514c5b
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 11 deletions.
24 changes: 15 additions & 9 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -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';


Expand All @@ -15,9 +15,6 @@ import { setCurrentUser } from './redux/user/user.actions';


class App extends React.Component {



unsubscribeFromAuth = null;
componentWillUnmount(){
this.unsubscribeFromAuth();
Expand All @@ -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);

Expand All @@ -49,16 +45,26 @@ class App extends React.Component {
<Header/>
<Switch>
<Route exact path="/" component={HomePage} />
<Route exact path="/shop" component={ShopPage} />
<Route exact path="/signin" component={SignInAndSignUp} />
<Route path="/shop" component={ShopPage} />
<Route exact path="/signin"
render={()=>
this.props.currentUser?(
<Redirect to='/' />
):(
<SignInAndSignUp/>
)
}
/>
</Switch>
</div>
);
}
}

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);
4 changes: 3 additions & 1 deletion src/redux/user/user.actions.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import {UserActionTypes} from './user.types';

export const setCurrentUser = user =>({
type: 'SET_CURRENT_USER',
type: UserActionTypes.SET_CURRENT_USER,
payload: user
})
4 changes: 3 additions & 1 deletion src/redux/user/user.reducer.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down
3 changes: 3 additions & 0 deletions src/redux/user/user.types.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const UserActionTypes = ({
SET_CURRENT_USER : 'SET_CURRENT_USER'
})

0 comments on commit 5514c5b

Please sign in to comment.