Skip to content

Commit 74d0ade

Browse files
added toggling features of the cart into redux
1 parent d49fb9d commit 74d0ade

File tree

7 files changed

+57
-9
lines changed

7 files changed

+57
-9
lines changed

src/components/cart-dropdown/cart-dropdown.styles.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
.cart-dropdown {
22
position: absolute;
33
width: 240px;
4-
height: 340px;
4+
height: auto;
55
display: flex;
66
flex-direction: column;
77
padding: 20px;
Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,22 @@
11
import React from 'react'
2+
import { connect } from 'react-redux'
3+
4+
import { toggleCartHidden } from '../../redux/cart/cart.actions'
5+
26
import './cart-icon.styles.scss'
37
import { ReactComponent as ShoppingIcon } from '../../assets/shopping-bag.svg'
4-
export default function CartIcon() {
8+
9+
function CartIcon({ toggleCartHidden }) {
510
return (
6-
<div className="cart-icon">
11+
<div className="cart-icon" onClick={toggleCartHidden}>
712
<ShoppingIcon className="shopping-icon" />
813
<span className="item-count">0</span>
914
</div>
1015
)
1116
}
17+
18+
const mapDispatchToProps = dispatch => ({
19+
toggleCartHidden: () => dispatch(toggleCartHidden())
20+
})
21+
22+
export default connect(null, mapDispatchToProps)(CartIcon)

src/components/header/header.component.jsx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { Link } from 'react-router-dom'
66
import { auth } from '../../firebase/firebase.utils'
77
import CartIcon from '../cart-icon/Cart-icon'
88
import CartDropdown from '../cart-dropdown/cart-dropdown.component'
9-
function Header({ currentUser }) {
9+
function Header({ currentUser, hidden }) {
1010
return (
1111
<div className="header">
1212
<Link to="/" className="logo-container">
@@ -31,13 +31,17 @@ function Header({ currentUser }) {
3131
}
3232
<CartIcon />
3333
</div>
34-
<CartDropdown />
34+
{
35+
hidden ? null : <CartDropdown />
36+
}
37+
3538
</div>
3639
)
3740
}
3841

39-
const mapStatetoProps = state => ({
40-
currentUser: state.user.currentUser
42+
const mapStatetoProps = ({ user: { currentUser }, cart: { hidden } }) => ({
43+
currentUser,
44+
hidden
4145
})
4246

4347
export default connect(mapStatetoProps)(Header)

src/redux/cart/cart.actions.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import CartActionTypes from './cart.types'
2+
3+
export const toggleCartHidden = () => ({
4+
type: CartActionTypes.TOGGLE_CART_HIDDEN
5+
})

src/redux/cart/cart.reducer.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import CartActionTypes from './cart.types'
2+
3+
4+
const INITIAL_STATE = {
5+
hidden: true
6+
}
7+
8+
9+
const cartReducer = (state = INITIAL_STATE, action) => {
10+
11+
switch (action.type) {
12+
case CartActionTypes.TOGGLE_CART_HIDDEN:
13+
return {
14+
...state,
15+
hidden: !state.hidden
16+
}
17+
default:
18+
return state
19+
}
20+
}
21+
22+
export default cartReducer

src/redux/cart/cart.types.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const CartActionTypes = {
2+
TOGGLE_CART_HIDDEN: 'TOGGLE_CART_HIDDEN'
3+
}
4+
5+
export default CartActionTypes

src/redux/root-reducer.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { combineReducers } from 'redux'
22
import userReducer from './user/user.reducer'
3-
3+
import cartReducer from './cart/cart.reducer'
44

55
export default combineReducers({
6-
user: userReducer
6+
user: userReducer,
7+
cart: cartReducer
78
})

0 commit comments

Comments
 (0)