Browse Source

Create shopping cart reducer

Tatiana Inama 7 years ago
parent
commit
cb1e1b4610

+ 1 - 1
recipes/src/containers/Recipes/List/index.tsx

@@ -159,7 +159,7 @@ class RecipeList extends Component<RecipeListProps, {phoneDisplay: boolean, sear
 }
 
 const mapStateToProps = ({ recipes }: any, ownProps: any) => {
-  return recipes.list;
+  return recipes;
 }
 
 const mapDispatchToProps = (dispatch: any) => {

+ 39 - 0
recipes/src/containers/ShoppingCart/actions.ts

@@ -0,0 +1,39 @@
+import { DBRecipe } from 'types/recipes';
+import ShoppingItem from 'types/shopping-cart';
+
+export const ADD_TO_CART = 'ADD_TO_CART';
+export const REMOVE_FROM_CART = 'REMOVE_FROM_CART';
+
+const getIngredients = (recipe: DBRecipe): ShoppingItem[] => {
+  return recipe.ingredients.reduce((ingredients, subRecipe) => {
+    return [
+      ...ingredients,
+      ...subRecipe.ingredients.map(ingredient => ({
+        ...ingredient,
+        _id: recipe._id
+      })),
+    ]
+  }, [] as ShoppingItem[])
+} 
+
+export const addToCart = (recipe: DBRecipe): AddToCart => ({
+  type: ADD_TO_CART,
+  payload: getIngredients(recipe)
+})
+
+export const removeFromCart = (recipe: DBRecipe): RemoveFromCart => ({
+  type: REMOVE_FROM_CART,
+  payload: recipe._id
+})
+
+export type AddToCart = {
+  type: typeof ADD_TO_CART,
+  payload: ShoppingItem[]
+}
+
+export type RemoveFromCart = {
+  type: typeof REMOVE_FROM_CART,
+  payload: string
+}
+
+export type ActionTypes = AddToCart | RemoveFromCart

+ 35 - 0
recipes/src/containers/ShoppingCart/reducers.ts

@@ -0,0 +1,35 @@
+import ShoppingItem from 'types/shopping-cart';
+import { ActionTypes, ADD_TO_CART, REMOVE_FROM_CART } from './actions';
+
+type ShoppingCartState = {
+  cart: ShoppingItem[]
+}
+
+const initialState: ShoppingCartState = {
+  cart: []
+};
+
+const shoppingCartReducer = (
+  state = initialState,
+  action: ActionTypes
+): ShoppingCartState => {
+  switch (action.type) {
+    case ADD_TO_CART:
+      return {
+        cart: [
+          ...state.cart,
+          ...action.payload
+        ]
+      }
+    case REMOVE_FROM_CART: 
+      return {
+        cart: state.cart.filter(
+          item => item._id !== action.payload
+        )
+      }
+    default:
+      return state
+  }
+}
+
+export default shoppingCartReducer;

+ 3 - 3
recipes/src/store/configureStore.ts

@@ -2,12 +2,12 @@ import { createStore, applyMiddleware, combineReducers } from 'redux';
 import thunk from 'redux-thunk';
 import { composeWithDevTools } from 'redux-devtools-extension';
 import { recipesReducer } from 'containers/Recipes/List/reducers';
+import shoppingCartReducer from 'containers/ShoppingCart/reducers';
 
 const composeEnhancers = composeWithDevTools({});
 const rootReducer = combineReducers({
-  recipes: combineReducers({
-    list: recipesReducer,
-  })
+  recipes: recipesReducer,
+  shoppingCart: shoppingCartReducer,
 })
 
 const configureStore = () => {

+ 7 - 0
recipes/src/types/shopping-cart.ts

@@ -0,0 +1,7 @@
+import Recipe, { Ingredient } from './recipes';
+
+interface ShoppingItem extends Ingredient {
+  _id: string,
+}
+
+export default ShoppingItem;