Skip to content

Commit

Permalink
uploading cart items, checkout page and using reselect for selectors
Browse files Browse the repository at this point in the history
  • Loading branch information
samilabud committed Mar 25, 2021
1 parent 3ba2dae commit b996d22
Show file tree
Hide file tree
Showing 12 changed files with 187 additions and 23 deletions.
9 changes: 7 additions & 2 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
import React from 'react';
import { Route,Switch, Redirect } from 'react-router-dom';
import {connect} from 'react-redux';
import { createStructuredSelector } from 'reselect';


import './App.css';

import HomePage from './pages/homepage/homepage.component';
import Header from './components/header/header.component';
import ShopPage from './pages/shop/shop.component';
import CheckoutPage from './pages/checkout/checkout.component';
import SignInAndSignUp from './pages/signin-and-singup/signin-and-singup.component';
import { auth,createUserProfileDocument } from './fireabase/firebase.utils';
import { setCurrentUser } from './redux/user/user.actions';
import { selectCurrentUser } from './redux/user/user.selectors';




Expand Down Expand Up @@ -46,6 +50,7 @@ class App extends React.Component {
<Switch>
<Route exact path="/" component={HomePage} />
<Route path="/shop" component={ShopPage} />
<Route exact path="/checkout" component={CheckoutPage} />
<Route exact path="/signin"
render={()=>
this.props.currentUser?(
Expand All @@ -60,8 +65,8 @@ class App extends React.Component {
);
}
}
const mapStateToProps = ({user}) => ({
currentUser: user.currentUser
const mapStateToProps = createStructuredSelector ({
currentUser: selectCurrentUser
})
const mapDispatchProps = dispatch => ({
setCurrentUser: user => dispatch(setCurrentUser(user))
Expand Down
8 changes: 0 additions & 8 deletions src/App.test.js

This file was deleted.

22 changes: 14 additions & 8 deletions src/components/cart-dropdown/cart-dropdown.component.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,31 @@ import { connect } from 'react-redux';
import CustomButton from '../custom-button/custom-button.component';
import CartItem from '../cart-item/cart-item-component';
import { selectCartItems } from '../../redux/cart/cart.selector';
import { createStructuredSelector } from 'reselect';
import { withRouter } from 'react-router-dom'
import { toggleCartHidden } from '../../redux/cart/cart.actions'

import './cart-dropdown.styles.scss';

const CartDropDown = ({cartItems}) => (
const CartDropDown = ({cartItems, history, dispatch}) => (
<div className='cart-dropdown'>
<div className='cart-items'>
{
cartItems.map(cartItem=>(
<CartItem key={cartItem.id} item={cartItem} />
cartItems.length ?
cartItems.map(cartItem=>(
<CartItem key={cartItem.id} item={cartItem} />
)
)
)
:
<span className='empty-message'>Tu carrito esta vacio.</span>
}
</div>
<CustomButton>GO TO CHECKOUT</CustomButton>
<CustomButton onClick={()=> {history.push('/checkout'); dispatch(toggleCartHidden())}}>GO TO CHECKOUT</CustomButton>
</div>
)

const mapStateToProps = (state) => ({
cartItems: selectCartItems (state)
const mapStateToProps = createStructuredSelector ({
cartItems: selectCartItems
})

export default connect(mapStateToProps)(CartDropDown);
export default withRouter(connect(mapStateToProps)(CartDropDown));
4 changes: 4 additions & 0 deletions src/components/cart-dropdown/cart-dropdown.styles.scss
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
right: 40px;
z-index: 5;

.empty-message {
font-size: 18px;
margin: 50px auto;
}
.cart-items {
height: 240px;
display: flex;
Expand Down
6 changes: 4 additions & 2 deletions src/components/cart-icon/cart-icon.component.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { selectCartItemsCount } from '../../redux/cart/cart.selector'

import { ReactComponent as ShoppingIcon } from '../../assets/shopping-bag.svg';

import {createStructuredSelector} from 'reselect';

import './cart-icon.styles.scss';

const CartIcon = ({toggleCartHidden, itemCount}) => (
Expand All @@ -19,8 +21,8 @@ const mapDispatchToProps = dispatch =>({
toggleCartHidden: ()=> dispatch(toggleCartHidden())
});

const mapStateToProps = state => ({
itemCount: selectCartItemsCount(state)
const mapStateToProps = createStructuredSelector ({
itemCount: selectCartItemsCount
})

export default connect(mapStateToProps, mapDispatchToProps)(CartIcon);
17 changes: 17 additions & 0 deletions src/components/checkout-item/checkout-item.component.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import React from 'react';

import './checkout-item.styles.scss';

const CheckoutItem = ({cartItem:{name, imageUrl, price, quantity}}) => (
<div className='checkout-item'>
<div className='image-container'>
<img src={imageUrl} alt='item' />
</div>
<span className='name'>{name}</span>
<span className='quantity'>{quantity}</span>
<span className='price'>{price}</span>
<div className='remove-button'>&#10005;</div>
</div>
)

export default CheckoutItem;
33 changes: 33 additions & 0 deletions src/components/checkout-item/checkout-item.styles.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
.checkout-item {
width: 100%;
display: flex;
min-height: 100px;
border-bottom: 1px solid darkgrey;
padding: 15px 0;
font-size: 20px;
align-items: center;

.image-container {
width: 23%;
padding-right: 15px;

img {
width: 100%;
height: 100%;
}
}
.name,
.quantity,
.price {
width: 23%;
}

.quantity {
padding-left: 20px;
}

.remove-button {
padding-left: 12px;
cursor: pointer;
}
}
10 changes: 8 additions & 2 deletions src/components/header/header.component.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@ import {ReactComponent as Logo} from '../../assets/crown.svg';
import CartIcon from '../cart-icon/cart-icon.component';
import CartDropDown from '../cart-dropdown/cart-dropdown.component';

import {createStructuredSelector} from 'reselect';

import {auth} from '../../fireabase/firebase.utils';
import './header.styles.scss';
import { connect } from 'react-redux';
import { selectCurrentUser } from '../../redux/user/user.selectors'
import { selectCartHidden } from '../../redux/cart/cart.selector'


const Header = ({currentUser, hidden}) => (
<div className='header'>
Expand All @@ -32,8 +37,9 @@ const Header = ({currentUser, hidden}) => (

</div>
)
const mapStateToProps = ({user:{currentUser},cart:{hidden}}) => ({
currentUser, hidden
const mapStateToProps = createStructuredSelector ({
currentUser: selectCurrentUser,
hidden: selectCartHidden
})

export default connect(mapStateToProps)(Header);
45 changes: 45 additions & 0 deletions src/pages/checkout/checkout.component.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import React from 'react';
import { connect } from 'react-redux';
import { createStructuredSelector } from 'reselect'

import { selectCartItems, selectCartTotal } from '../../redux/cart/cart.selector';

import CheckoutItem from '../../components/checkout-item/checkout-item.component'

import './checkout.styles.scss';

const CheckoutPage = ({cartItems, cartTotal}) => (
<div className='checkout-page'>
<div className='checkout-header'>
<div className='header-block'>
<span>Producto</span>
</div>
<div className='header-block'>
<span>Descripcion</span>
</div>
<div className='header-block'>
<span>Cantidad</span>
</div>
<div className='header-block'>
<span>Precio</span>
</div>
<div className='header-block'>
<span>Remover</span>
</div>
</div>
{
cartItems.map(
cartItem=> <CheckoutItem key={cartItem.id} cartItem={cartItem} />
)
}
<div className='total'>
<span>TOTAL: ${cartTotal}</span>
</div>
</div>
)
const mapStateToProps = createStructuredSelector({
cartItems: selectCartItems,
cartTotal: selectCartTotal
})

export default connect(mapStateToProps)(CheckoutPage);
31 changes: 31 additions & 0 deletions src/pages/checkout/checkout.styles.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
.checkout-page {
width: 55%;
min-height: 90vh;
display: flex;
flex-direction: column;
align-items: center;
margin: 50px auto 0;

.checkout-header {
width: 100%;
padding: 10px 0;
display: flex;
justify-content: space-between;
border-bottom: 1px solid darkgrey;

.header-block {
text-transform: capitalize;
width: 23%;

&:last-child {
width: 8%;
}
}
}

.total {
margin-top: 30px;
margin-left: auto;
font-size: 36px;
}
}
17 changes: 16 additions & 1 deletion src/redux/cart/cart.selector.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,19 @@ export const selectCartItemsCount = createSelector(
cartItems.reduce(
(accumulateQuantity, cartItem) => accumulateQuantity + cartItem.quantity, 0
)
);
);

export const selectCartTotal = createSelector(
[selectCartItems],
cartItems=>
cartItems.reduce(
(accumulateQuantity, cartItem) =>
accumulateQuantity + cartItem.quantity * cartItem.price,
0
)
)

export const selectCartHidden = createSelector(
[selectCart],
cart=>cart.hidden
)
8 changes: 8 additions & 0 deletions src/redux/user/user.selectors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { createSelector } from 'reselect';

const selectUser = state => state.user

export const selectCurrentUser = createSelector (
[selectUser],
user=>user.currentUser
)

0 comments on commit b996d22

Please sign in to comment.