Skip to content

Commit b4a9e0c

Browse files
refactor current user, now using context hooks...
1 parent 5887227 commit b4a9e0c

File tree

8 files changed

+18316
-42
lines changed

8 files changed

+18316
-42
lines changed

package-lock.json

+18,018
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
{
22
"name": "crwn-clothing",
33
"version": "0.1.0",
4-
"proxy": "http://localhost:5000",
54
"private": true,
65
"dependencies": {
76
"firebase": "6.0.2",
8-
"node-sass": "4.12.0",
7+
"node-sass": "^4.13.1",
98
"react": "^16.8.6",
109
"react-dom": "^16.8.6",
1110
"react-redux": "7.0.3",
@@ -17,7 +16,7 @@
1716
"reselect": "4.0.0"
1817
},
1918
"devDependencies": {
20-
"react-scripts": "3.0.0"
19+
"react-scripts": "^3.3.0"
2120
},
2221
"resolutions": {
2322
"babel-jest": "24.7.1"

src/App.js

+20-27
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,41 @@
11
import React from 'react';
2+
import CurrentUserContext from './contexts/current-user/current-user.context';
23
import { Switch, Route, Redirect } from 'react-router-dom';
3-
import { connect } from 'react-redux';
4-
import { createStructuredSelector } from 'reselect';
5-
64
import './App.css';
7-
85
import HomePage from './pages/homepage/homepage.component';
96
import ShopPage from './pages/shop/shop.component';
107
import SignInAndSignUpPage from './pages/sign-in-and-sign-up/sign-in-and-sign-up.component';
118
import CheckoutPage from './pages/checkout/checkout.component';
12-
139
import Header from './components/header/header.component';
14-
1510
import { auth, createUserProfileDocument } from './firebase/firebase.utils';
1611

17-
import { setCurrentUser } from './redux/user/user.actions';
18-
import { selectCurrentUser } from './redux/user/user.selectors';
19-
2012
class App extends React.Component {
13+
14+
constructor(){
15+
super();
16+
this.state = {
17+
currentUser : null
18+
}
19+
}
20+
2121
unsubscribeFromAuth = null;
2222

2323
componentDidMount() {
24-
const { setCurrentUser } = this.props;
25-
24+
console.log("Estoy seteando el estado...");
2625
this.unsubscribeFromAuth = auth.onAuthStateChanged(async userAuth => {
2726
if (userAuth) {
2827
const userRef = await createUserProfileDocument(userAuth);
2928

3029
userRef.onSnapshot(snapShot => {
31-
setCurrentUser({
30+
this.setState({currentUser : {
3231
id: snapShot.id,
3332
...snapShot.data()
34-
});
33+
}});
3534
});
3635
}
3736

38-
setCurrentUser(userAuth);
37+
this.setState({currentUser : userAuth});
38+
console.log("termine de setear el estado...");
3939
});
4040
}
4141

@@ -44,9 +44,12 @@ class App extends React.Component {
4444
}
4545

4646
render() {
47+
console.log("El valor de current user en APP.JS es : ",this.state.currentUser);
4748
return (
4849
<div>
49-
<Header />
50+
<CurrentUserContext.Provider value={this.state.currentUser}>
51+
<Header />
52+
</CurrentUserContext.Provider>
5053
<Switch>
5154
<Route exact path='/' component={HomePage} />
5255
<Route path='/shop' component={ShopPage} />
@@ -55,7 +58,7 @@ class App extends React.Component {
5558
exact
5659
path='/signin'
5760
render={() =>
58-
this.props.currentUser ? (
61+
this.state.currentUser ? (
5962
<Redirect to='/' />
6063
) : (
6164
<SignInAndSignUpPage />
@@ -68,15 +71,5 @@ class App extends React.Component {
6871
}
6972
}
7073

71-
const mapStateToProps = createStructuredSelector({
72-
currentUser: selectCurrentUser
73-
});
74-
75-
const mapDispatchToProps = dispatch => ({
76-
setCurrentUser: user => dispatch(setCurrentUser(user))
77-
});
7874

79-
export default connect(
80-
mapStateToProps,
81-
mapDispatchToProps
82-
)(App);
75+
export default App;

src/components/header/header.component.jsx

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React from 'react';
1+
import React, {useContext} from 'react';
22
import { Link } from 'react-router-dom';
33
import { connect } from 'react-redux';
44
import { createStructuredSelector } from 'reselect';
@@ -7,13 +7,17 @@ import { auth } from '../../firebase/firebase.utils';
77
import CartIcon from '../cart-icon/cart-icon.component';
88
import CartDropdown from '../cart-dropdown/cart-dropdown.component';
99
import { selectCartHidden } from '../../redux/cart/cart.selectors';
10-
import { selectCurrentUser } from '../../redux/user/user.selectors';
10+
import CurrentUserContext from "../../contexts/current-user/current-user.context";
11+
1112

1213
import { ReactComponent as Logo } from '../../assets/crown.svg';
1314

1415
import './header.styles.scss';
1516

16-
const Header = ({ currentUser, hidden }) => (
17+
const Header = ({ hidden }) =>{
18+
const currentUser = useContext(CurrentUserContext);
19+
console.log("El valor del current user en el header es : ",currentUser);
20+
return(
1721
<div className='header'>
1822
<Link className='logo-container' to='/'>
1923
<Logo className='logo' />
@@ -39,9 +43,9 @@ const Header = ({ currentUser, hidden }) => (
3943
{hidden ? null : <CartDropdown />}
4044
</div>
4145
);
46+
}
4247

4348
const mapStateToProps = createStructuredSelector({
44-
currentUser: selectCurrentUser,
4549
hidden: selectCartHidden
4650
});
4751

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import {createContext} from 'react';
2+
3+
import SHOP_DATA from './shop.data';
4+
5+
const ContextCollection = createContext(SHOP_DATA);
6+
7+
export default ContextCollection;

0 commit comments

Comments
 (0)