-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Facundo Martinez
authored and
Facundo Martinez
committed
Feb 16, 2021
1 parent
1e9aab7
commit 99f7b20
Showing
11 changed files
with
118 additions
and
21 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }]; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters