Skip to content

Commit c4cfee6

Browse files
refactor App and shop component with useEffects
1 parent 2cd1997 commit c4cfee6

File tree

3 files changed

+24
-54
lines changed

3 files changed

+24
-54
lines changed

src/App.js

+15-26
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React from "react";
1+
import React , {useEffect} from "react";
22
import HomePage from "./pages/homepage/homepage.component";
33
import { createStructuredSelector } from "reselect";
44
import { selectCurrentUser } from "./redux/user/user.selectors";
@@ -13,57 +13,46 @@ import {checkUserSession } from "./redux/user/user.actions";
1313
import { selectToArray } from "./redux/shop/shop.selectors";
1414

1515
import "./App.css";
16-
import {
17-
auth,
18-
createUserProfileDocument
19-
} from "../src/firebase/firebase.utils";
2016
import CheckOutPage from "./pages/checkout/checkout.component";
2117

22-
class App extends React.Component {
23-
//usa esta variable para cerrar la sesion...
24-
unsubscribeFromAuth = null;
18+
//Solo lo ejecuta una ves
19+
const App = ({checkUserSession,currentUser}) => {
2520

26-
componentDidMount() {
27-
console.log("Me estoy fijando que esta pasando");
28-
const {checkUserSession} = this.props;
29-
const aux = checkUserSession();
30-
console.log("vino : ",aux);
31-
32-
}
21+
useEffect( () => {
22+
checkUserSession();
3323

34-
componentWillUnmount() {
35-
//this.unsubscribeFromAuth();
36-
}
24+
},[checkUserSession])
3725

38-
handleRedirect = () => {
39-
console.log("Estoy justo aqui ");
40-
return this.props.currentUser ? (
26+
const handleRedirect = () => {
27+
28+
return currentUser ? (
4129
<Redirect to="/" />
4230
) : (
4331
<SignInAndSignUpPage />
4432
);
4533
};
4634

47-
render() {
35+
4836
return (
4937
<div>
5038
<Header />
5139
<Switch>
5240
<Route exact path="/" component={HomePage} />
5341
<Route path="/shop" component={ShopPage} />
5442
<Route exact path="/checkout" component={CheckOutPage} />
55-
<Route exact path="/signin" render={this.handleRedirect} />
43+
<Route exact path="/signin" render={handleRedirect} />
5644
</Switch>
5745
</div>
5846
);
47+
48+
5949
}
60-
}
50+
6151

6252

6353

6454
const mapStateToProps = createStructuredSelector({
65-
currentUser: selectCurrentUser,
66-
collectionsArray: selectToArray
55+
currentUser: selectCurrentUser
6756
});
6857

6958
const mapDispatchToProps = dispatch => ({

src/components/header/header.component.jsx

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import React from "react";
22
import { connect } from "react-redux";
33
import { createStructuredSelector } from "reselect";
44
import { ReactComponent as Logo } from "../../assets/crown.svg";
5-
import { auth } from "../../firebase/firebase.utils";
65
import CartIcon from "../cart-icon/cart-icon.component";
76
import CartDropdown from "../cart-dropdown/cart-dropdown";
87
import { selectCartHidden } from "../../redux/cart/cart.selectors";

src/pages/shop/shop.component.jsx

+9-27
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,18 @@
1-
import React from "react";
1+
import React,{useEffect} from "react";
22
import { Route } from "react-router-dom";
3-
import {createStructuredSelector} from "reselect";
4-
import CollectionsOverview from "../../components/collections-overview/collections-overview.component";
5-
import CollectionPage from "../collection/collection.component";
63
import {connect} from "react-redux";
74
import {fetchCollectionsStart} from "../../redux/shop/shop.actions";
8-
import withSpinner from "../../components/with-spinner/with-spinner.component";
9-
import {selectIsCollectionFetching,selectIsCollectionsLoaded} from "../../redux/shop/shop.selectors";
105
import {CollectionPageContainer} from "../collection/collection.container";
116
import {CollectionsOverviewContainer} from "../../components/collections-overview/collections-overview.container";
127

13-
class ShopPage extends React.Component{
14-
15-
16-
17-
componentDidMount() {
18-
const {fetchCollectionsStart} = this.props;
19-
fetchCollectionsStart();
20-
21-
}
8+
const ShopPage = ({fetchCollectionsStart,match}) => {
229

10+
useEffect(
11+
() => {
12+
fetchCollectionsStart()
2313

24-
render(){
25-
26-
const {match,isCollectionLoaded,isCollectionFetching,collections} = this.props;
27-
console.log("El momento de la verdad : ");
28-
console.log(isCollectionLoaded);
29-
30-
const CollectionPageSpinner = withSpinner(CollectionPage);
14+
},[fetchCollectionsStart]
15+
)
3116

3217
return (
3318
<div>
@@ -37,17 +22,14 @@ class ShopPage extends React.Component{
3722
return <CollectionPageContainer otherProps={props} />;}} />
3823
</div>
3924
);
40-
};
25+
4126

4227
}
4328

44-
const mapStateToProps = createStructuredSelector({
45-
isCollectionLoaded : selectIsCollectionsLoaded,
46-
})
4729

4830
const mapDispatchToProps = dispatch =>({
4931
fetchCollectionsStart : () => dispatch(fetchCollectionsStart())
5032
})
5133

5234

53-
export default connect(mapStateToProps,mapDispatchToProps)(ShopPage);
35+
export default connect(null,mapDispatchToProps)(ShopPage);

0 commit comments

Comments
 (0)