1+ import { useState , useEffect } from "react" ;
12import CookbookBar from "../../cookbook_components/CookbookBar" ;
3+ import ImageCard from "../../shared/components/ImageCard" ;
4+ import GroceryListIngredientCard from "../../cookbook_components/GroceryListIngredientCard" ;
5+ import { recipes } from "../../shared/data/dummyRecipes" ;
6+ import { RecipeIngredient } from "../../shared/types" ;
27
38export default function GroceryList ( ) {
4- return (
5- < div className = "p-10" >
6- < CookbookBar showMyRecipes = { false } />
7- < div className = "mt-5 flex flex-row" >
8- </ div >
9+ // Track recipe quantities
10+ const [ quantities , setQuantities ] = useState < Record < string , number > > (
11+ recipes . reduce ( ( acc , recipe ) => {
12+ acc [ recipe . id ] = 0 ;
13+ return acc ;
14+ } , { } as Record < string , number > )
15+ ) ;
16+
17+ // Track ingredient totals separately
18+ const [ ingredientTotals , setIngredientTotals ] = useState < Record < string , RecipeIngredient > > ( { } ) ;
19+ console . log ( ingredientTotals )
20+
21+ const increaseQuantity = ( id : string ) => setQuantities ( prev => ( { ...prev , [ id ] : prev [ id ] + 1 } ) ) ;
22+ const decreaseQuantity = ( id : string ) =>
23+ setQuantities ( prev => ( { ...prev , [ id ] : prev [ id ] > 0 ? prev [ id ] - 1 : 0 } ) ) ;
24+
25+ // Recompute ingredient totals whenever recipe quantities change
26+ useEffect ( ( ) => {
27+ const totals : Record < string , RecipeIngredient > = { } ;
28+ console . log ( totals )
29+
30+ recipes . forEach ( recipe => {
31+ const recipeQty = quantities [ recipe . id ] || 0 ;
32+ if ( recipeQty > 0 ) {
33+ recipe . ingredients . forEach ( ri => {
34+ const totalQty = ri . storeQuantity * recipeQty ;
35+ if ( ! totals [ ri . ingredient . id ] ) {
36+ totals [ ri . ingredient . id ] = {
37+ ingredient : ri . ingredient ,
38+ quantity : totalQty . toString ( ) ,
39+ storeQuantity : totalQty ,
40+ } ;
41+ } else {
42+ const prev = totals [ ri . ingredient . id ] ;
43+ const newStoreQty = prev . storeQuantity + totalQty ;
44+ totals [ ri . ingredient . id ] = {
45+ ingredient : ri . ingredient ,
46+ quantity : newStoreQty . toString ( ) ,
47+ storeQuantity : newStoreQty ,
48+ } ;
49+ }
50+ } ) ;
51+ }
52+ } ) ;
53+
54+ setIngredientTotals ( totals ) ;
55+ } , [ quantities ] ) ;
56+
57+ const uniqueIngredients = Object . values ( ingredientTotals ) ;
58+
59+ const handleIngredientChange = ( id : string , newQty : number ) => {
60+ setIngredientTotals ( prev => {
61+ const updated = { ...prev } ;
62+ if ( updated [ id ] ) {
63+ updated [ id ] = {
64+ ...updated [ id ] ,
65+ storeQuantity : newQty ,
66+ quantity : newQty . toString ( ) ,
67+ } ;
68+ }
69+ return updated ;
70+ } ) ;
71+ } ;
72+
73+ return (
74+ < div className = "p-10" >
75+ < CookbookBar showMyRecipes = { false } />
76+
77+ < div className = "mt-5 flex flex-row gap-5" >
78+ { /* Recipes */ }
79+ < div className = "bg-white flex-[2] h-[70vh] rounded-xl overflow-y-auto p-4" >
80+ < div className = "flex flex-col items-center gap-6" >
81+ { recipes . map ( recipe => (
82+ < div key = { recipe . id } className = "w-full max-w-[90%] relative" >
83+ < ImageCard
84+ src = { recipe . image_id || "" }
85+ alt = { recipe . title }
86+ caption = { recipe . title }
87+ height = { 150 }
88+ className = "w-full border-black border-2 relative"
89+ />
90+ < div className = "absolute top-10 right-2 flex flex-col items-center gap-1" >
91+ < button
92+ onClick = { ( ) => increaseQuantity ( recipe . id ) }
93+ className = "px-2 py-1 bg-[#EB5904] text-white font-bold rounded hover:bg-orange-600 transition"
94+ >
95+ +
96+ </ button >
97+ < span className = "bg-white px-2 py-1 rounded font-semibold text-black text-sm" >
98+ { quantities [ recipe . id ] }
99+ </ span >
100+ < button
101+ onClick = { ( ) => decreaseQuantity ( recipe . id ) }
102+ className = "px-2 py-1 bg-[#EB5904] text-white font-bold rounded hover:bg-orange-600 transition"
103+ >
104+ -
105+ </ button >
106+ </ div >
107+ </ div >
108+ ) ) }
109+ </ div >
110+ </ div >
111+
112+ { /* Grocery List */ }
113+ < div className = "bg-white flex-[1] h-[70vh] rounded-xl p-4 overflow-y-auto" >
114+ < div className = "flex flex-col gap-4" >
115+ { uniqueIngredients . map ( item => (
116+ < GroceryListIngredientCard
117+ key = { item . ingredient . id }
118+ item = { {
119+ ingredient : item . ingredient ,
120+ quantity : Math . ceil ( item . storeQuantity ) . toString ( ) ,
121+ purchased_status : false ,
122+ } }
123+ startingQuantity = { Math . ceil ( item . storeQuantity ) }
124+ onQuantityChange = { newQty => handleIngredientChange ( item . ingredient . id , newQty ) }
125+ />
126+ ) ) }
127+ </ div >
9128 </ div >
10- ) ;
11- } ;
129+ </ div >
130+ </ div >
131+ ) ;
132+ }
0 commit comments