ソースを参照

Create combineMultipleItems functionality

Tatiana Inama 6 年 前
コミット
a2e1588fb8
1 ファイル変更12 行追加1 行削除
  1. 12 1
      recipes/src/containers/ShoppingCart/services.ts

+ 12 - 1
recipes/src/containers/ShoppingCart/services.ts

@@ -1,7 +1,7 @@
 import ShoppingItem, { ShoppingRecipe, DBShoppingCart } from 'types/shopping-cart';
 import { Ingredient } from 'types/recipes';
 import { Convert, GetMeasure, Measure } from 'services/measurements';
-import { update } from 'ramda';
+import { update, uniq } from 'ramda';
 import axios from 'axios';
 
 const SHOPPING: string = process.env.REACT_APP_API_SHOPPING || '';
@@ -34,6 +34,16 @@ const combineItems = (a: ShoppingItem, b: ShoppingItem): ShoppingItem => {
   }
 }
 
+const combineMultipleItems = (items: ShoppingItem[]): ShoppingItem => {
+  return items.reduce((combined, item) => ({
+    ...combineItems(combined, item),
+    recipeName: uniq([
+      ...combined.recipeName || [],
+      ...item.recipeName || []
+    ]),
+  }))
+}
+
 const createShoppingList = (initial: ShoppingItem[], newItems: ShoppingItem[]): ShoppingItem[] => {
   return newItems.reduce((shoppingCart, newItem) => {
     const existentItemIdx = shoppingCart.findIndex(item => item.name === newItem.name);
@@ -86,4 +96,5 @@ export {
   createShoppingItem,
   fetchShoppingCart,
   saveShoppingCart,
+  combineMultipleItems
 }