Skip to content

Commit

Permalink
add to cart functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
Facundo Martinez authored and Facundo Martinez committed Feb 16, 2021
1 parent 1e9aab7 commit 99f7b20
Show file tree
Hide file tree
Showing 11 changed files with 118 additions and 21 deletions.
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"redux-logger": "^3.0.6",
"redux-saga": "^1.1.3",
"redux-thunk": "^2.3.0",
"reselect": "^4.0.0",
"web-vitals": "^1.1.0"
},
"scripts": {
Expand Down
27 changes: 15 additions & 12 deletions src/components/Header/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ import React from "react";
import { Link } from "react-router-dom";
import { useSelector, useDispatch } from "react-redux";
import { signOutUserStart } from "./../../redux/User/user.actions";
import { selectCartItemsCount } from "./../../redux/Cart/cart.selectors";
import "./styles.scss";
import Logo from "./../../assets/logo.png";

const Header = () => {
const dispatch = useDispatch();
const currentUser = useSelector((state) => state.user.currentUser);
const totalNumCartItems = useSelector((state) => selectCartItemsCount(state));
const signOut = () => {
dispatch(signOutUserStart());
};
Expand All @@ -30,26 +32,27 @@ const Header = () => {
</ul>
</nav>
<div className="callToActions">
{currentUser && (
<ul>
<ul>
<li>
<Link to="/">Your Cart: ({totalNumCartItems})</Link>
</li>
{currentUser && [
<li>
<Link to="/dashboard">My Account</Link>
</li>
</li>,
<li>
<span onClick={signOut}>LOGOUT</span>
</li>
</ul>
)}
{!currentUser && (
<ul>
</li>,
]}
{!currentUser && [
<li>
<Link to="/registration">Register</Link>
</li>
</li>,
<li>
<Link to="/login">Login</Link>
</li>
</ul>
)}
</li>,
]}
</ul>
</div>
</div>
</header>
Expand Down
13 changes: 12 additions & 1 deletion src/components/ProductCard/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
fetchProductStart,
setProduct,
} from "./../../redux/Product/products.action";
import { addProduct } from "./../../redux/Cart/cart.actions";
import Button from "./../forms/Button";
import "./styles.scss";

Expand All @@ -31,6 +32,11 @@ const ProductCard = ({}) => {
type: "button",
};

const handleAddToCart = (product) => {
if (!product) return;
dispatch(addProduct(product));
};

return (
<div className="productCard">
<div className="hero">
Expand All @@ -46,7 +52,12 @@ const ProductCard = ({}) => {
</li>
<li>
<div className="addToCart">
<Button {...configAddToCartBtn}>Add to cart</Button>
<Button
{...configAddToCartBtn}
onClick={() => handleAddToCart(product)}
>
Add to cart
</Button>
</div>
</li>
<li>
Expand Down
22 changes: 15 additions & 7 deletions src/components/ProductsResults/Product/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import React from "react";
import { Link } from "react-router-dom";
import Button from "../../forms/Button";
import { useDispatch } from "react-redux";
import { addProduct } from "./../../../redux/Cart/cart.actions";

const Product = ({
productThumbnail,
productName,
productPrice,
documentID,
}) => {
const Product = (product) => {
const dispatch = useDispatch();
const { productThumbnail, productName, productPrice, documentID } = product;
if (
!documentID ||
!productThumbnail ||
Expand All @@ -18,6 +17,10 @@ const Product = ({
const configAddToCartBtn = {
type: "button",
};
const handleAddToCart = (product) => {
if (!product) return;
dispatch(addProduct(product));
};
return (
<div className="product">
<div className="thumb">
Expand All @@ -37,7 +40,12 @@ const Product = ({
</li>
<li>
<div className="addToCart">
<Button {...configAddToCartBtn}>Add To Cart</Button>
<Button
{...configAddToCartBtn}
onClick={() => handleAddToCart(product)}
>
Add To Cart
</Button>
</div>
</li>
</ul>
Expand Down
6 changes: 6 additions & 0 deletions src/redux/Cart/cart.actions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import cartTypes from "./cart.types";

export const addProduct = (nextCartItem) => ({
type: cartTypes.ADD_TO_CART,
payload: nextCartItem,
});
23 changes: 23 additions & 0 deletions src/redux/Cart/cart.reducers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import cartTypes from "./cart.types";
import { handleAddToCart } from "./cart.utils";

const INITIAL_STATE = {
cartItems: [],
};

const cartReducer = (state = INITIAL_STATE, action) => {
switch (action.type) {
case cartTypes.ADD_TO_CART:
return {
...state,
cartItems: handleAddToCart({
prevCartItems: state.cartItems,
nextCartItem: action.payload,
}),
};
default:
return state;
}
};

export default cartReducer;
14 changes: 14 additions & 0 deletions src/redux/Cart/cart.selectors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { createSelector } from "reselect";

export const selectCartData = (state) => state.cartData;

export const selectCartItems = createSelector(
[selectCartData],
(cartData) => cartData.cartItems
);

export const selectCartItemsCount = createSelector(
[selectCartItems],
(cartItems) =>
cartItems.reduce((quantity, cartItem) => quantity + cartItem.quantity, 0)
);
5 changes: 5 additions & 0 deletions src/redux/Cart/cart.types.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const cartTypes = {
ADD_TO_CART: "ADD_TO_CART",
};

export default cartTypes;
19 changes: 19 additions & 0 deletions src/redux/Cart/cart.utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export const existingCartItem = ({ prevCartItems, nextCartItem }) => {
return prevCartItems.find(
(cartItem) => cartItem.documentID === nextCartItem.documentID
);
};

export const handleAddToCart = ({ prevCartItems, nextCartItem }) => {
const quantityIncrement = 1;
const cartItemExist = existingCartItem({ prevCartItems, nextCartItem });

if (cartItemExist) {
return prevCartItems.map((cartItem) =>
cartItem.documentID === nextCartItem.documentID
? { ...cartItem, quantity: cartItem.quantity + quantityIncrement }
: cartItem
);
}
return [...prevCartItems, { ...nextCartItem, quantity: quantityIncrement }];
};
4 changes: 3 additions & 1 deletion src/redux/rootReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import { combineReducers } from "redux";

import productsReducer from "./Product/products.reducers";
import userReducer from "./User/user.reducer";
import cartReducer from "./Cart/cart.reducers";

export default combineReducers({
user: userReducer,
productsData: productsReducer
productsData: productsReducer,
cartData: cartReducer,
});

0 comments on commit 99f7b20

Please sign in to comment.