Skip to content

Commit 340e09d

Browse files
configured app to store and get user data from redux store
1 parent ddc8763 commit 340e09d

File tree

2 files changed

+21
-15
lines changed

2 files changed

+21
-15
lines changed

src/App.js

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,32 @@
11
import React, { Component } from 'react';
2+
import { connect } from 'react-redux'
23
import './App.css';
34
import { Route, Switch } from 'react-router-dom';
45
import Homepage from './Pages/homepage/Homepage.component'
56
import ShopPage from './Pages/shop/Shop.component'
67
import signInAndSignUpPage from './Pages/sign-in-and-sign-up/sign-in-and-sign-up.component'
78
import Header from './components/header/header.component'
89
import { auth, createUserProfileDocument } from './firebase/firebase.utils'
9-
10+
import { setCurrentUser } from './redux/user/user.actions'
1011
class App extends Component {
11-
constructor(props) {
12-
super(props)
13-
14-
this.state = {
15-
currentUser: null
16-
}
17-
}
1812
unsubscribeFromAuth = null
1913
componentDidMount() {
14+
const { setCurrentUser } = this.props
2015
this.unsubscribeFromAuth = auth.onAuthStateChanged(async userAuth => {
2116
if (userAuth) {
2217
const userRef = await createUserProfileDocument(userAuth)
2318

2419
userRef.onSnapshot(snapshot => {
25-
this.setState({
20+
setCurrentUser({
2621
currentUser: {
2722
id: snapshot.id,
2823
...snapshot.data()
2924
}
3025
})
3126
})
3227
}
33-
else{
34-
this.setState({ currentUser: null })
28+
else {
29+
setCurrentUser(userAuth)
3530
}
3631
})
3732
}
@@ -40,7 +35,7 @@ class App extends Component {
4035
}
4136
render() {
4237
return <div className='App'>
43-
<Header currentUser={this.state.currentUser} />
38+
<Header />
4439
<Switch>
4540
<Route exact path='/' component={Homepage} />
4641
<Route exact path='/shop' component={ShopPage} />
@@ -50,4 +45,7 @@ class App extends Component {
5045
}
5146
}
5247

53-
export default App;
48+
const mapDispatchToProps = dispatch => ({
49+
setCurrentUser: user => dispatch(setCurrentUser(user))
50+
})
51+
export default connect(null, mapDispatchToProps)(App);

src/components/header/header.component.jsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import React from 'react'
2+
import { connect } from 'react-redux'
23
import './header.styles.scss'
34
import { ReactComponent as Logo } from '../../assets/newLogo.svg'
45
import { Link } from 'react-router-dom'
56
import { auth } from '../../firebase/firebase.utils'
6-
export default function Header({ currentUser }) {
7+
8+
function Header({ currentUser }) {
79
return (
810
<div className="header">
911
<Link to="/" className="logo-container">
@@ -18,7 +20,7 @@ export default function Header({ currentUser }) {
1820
</Link>
1921
{
2022
currentUser ?
21-
<div className="option" onClick={()=> auth.signOut()}>
23+
<div className="option" onClick={() => auth.signOut()}>
2224
SIGN OUT
2325
</div>
2426
:
@@ -31,3 +33,9 @@ export default function Header({ currentUser }) {
3133
</div>
3234
)
3335
}
36+
37+
const mapStatetoProps = state => ({
38+
currentUser: state.user.currentUser
39+
})
40+
41+
export default connect(mapStatetoProps)(Header)

0 commit comments

Comments
 (0)