11import React , { Component } from 'react' ;
2+ import { connect } from 'react-redux'
23import './App.css' ;
34import { Route , Switch } from 'react-router-dom' ;
45import Homepage from './Pages/homepage/Homepage.component'
56import ShopPage from './Pages/shop/Shop.component'
67import signInAndSignUpPage from './Pages/sign-in-and-sign-up/sign-in-and-sign-up.component'
78import Header from './components/header/header.component'
89import { auth , createUserProfileDocument } from './firebase/firebase.utils'
9-
10+ import { setCurrentUser } from './redux/user/user.actions'
1011class 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 ) ;
0 commit comments