@@ -32,6 +32,8 @@ export function AddItemForm({ listPath, data: unfilteredListItems }: Props) {
3232 PurchaseTime . soon ,
3333 ) ;
3434
35+ const [ addedQuantity , setAddedQuantity ] = useState ( 1 ) ;
36+
3537 const handleItemNameTextChange = ( e : ChangeEvent < HTMLInputElement > ) => {
3638 setItemName ( e . target . value ) ;
3739 } ;
@@ -40,6 +42,11 @@ export function AddItemForm({ listPath, data: unfilteredListItems }: Props) {
4042 setItemNextPurchaseTimeline ( changed ) ;
4143 } ;
4244
45+ const handleItemQuantityChange = ( e : ChangeEvent < HTMLInputElement > ) => {
46+ setAddedQuantity ( Number ( e . target . value ) ) ;
47+ console . log ( "Quantity captured in Add Item input:" , addedQuantity ) ;
48+ } ;
49+
4350 const handleSubmit = async (
4451 e : FormEvent < HTMLFormElement > ,
4552 listPath : string ,
@@ -69,19 +76,21 @@ export function AddItemForm({ listPath, data: unfilteredListItems }: Props) {
6976
7077 try {
7178 await toast . promise (
72- addItem ( listPath , itemName , daysUntilNextPurchase ) , // saving original input
79+ addItem ( listPath , itemName , daysUntilNextPurchase , addedQuantity ) , // saving original input
7380 {
7481 loading : "Adding item to list." ,
7582 success : ( ) => {
7683 setItemName ( "" ) ;
7784 setItemNextPurchaseTimeline ( PurchaseTime . soon ) ;
78- return `${ itemName } successfully added to your list!` ; // showing original input
85+ setAddedQuantity ( 1 ) ;
86+ return `${ addedQuantity } ${ itemName } successfully added to your list!` ; // showing original input
7987 } ,
8088 error : ( ) => {
81- return `${ itemName } failed to add to your list. Please try again!` ;
89+ return `Failed to add ${ addedQuantity } ${ itemName } to your list. Please try again!` ;
8290 } ,
8391 } ,
8492 ) ;
93+ console . log ( "Quantity added from Add Item form:" , addedQuantity ) ;
8594 } catch ( error ) {
8695 console . error ( "Failed to add item:" , error ) ;
8796 }
@@ -96,80 +105,85 @@ export function AddItemForm({ listPath, data: unfilteredListItems }: Props) {
96105
97106 return (
98107 < section >
99- { listPath && (
100- < >
101- < Form onSubmit = { ( e ) => handleSubmit ( e , listPath ) } >
102- < h3 > First, add your item!</ h3 >
103- < Form . Label htmlFor = "item-name" > Item:</ Form . Label >
104- < Form . Control
105- id = "item-name"
106- type = "text"
107- name = "item"
108- value = { itemName }
109- onChange = { handleItemNameTextChange }
110- aria-label = "Enter the item name"
111- aria-required
108+ < Form onSubmit = { ( e ) => handleSubmit ( e , listPath ) } >
109+ < h3 > First, add your item!</ h3 >
110+ < Form . Label htmlFor = "item-name" >
111+ Item:
112+ < Form . Control
113+ id = "item-name"
114+ type = "text"
115+ name = "item"
116+ value = { itemName }
117+ onChange = { handleItemNameTextChange }
118+ aria-label = "Enter the item name"
119+ aria-required
120+ />
121+ </ Form . Label >
122+ < label htmlFor = "item-quantity" >
123+ Quantity:
124+ < Form . Control
125+ id = "item-quantity"
126+ type = "number"
127+ name = "quantity"
128+ value = { addedQuantity }
129+ onChange = { handleItemQuantityChange }
130+ aria-label = "Enter the item quantity"
131+ aria-required
132+ />
133+ </ label >
134+ < br />
135+ < h3 > Next, pick when you plan on buying this item again!</ h3 >
136+ < fieldset >
137+ < legend > When to buy:</ legend >
138+ < Form . Label htmlFor = { PurchaseTime . soon } >
139+ < Form . Check
140+ type = "radio"
141+ id = { PurchaseTime . soon }
142+ name = "when-to-buy"
143+ value = { PurchaseTime . soon }
144+ required
145+ onChange = { ( ) => handleNextPurchaseChange ( PurchaseTime . soon ) }
146+ checked = { itemNextPurchaseTimeline === PurchaseTime . soon }
147+ aria-label = { `Set buy to soon, within ${ purchaseTimelines [ PurchaseTime . soon ] } days` }
148+ />
149+ Soon -- Within { purchaseTimelines [ PurchaseTime . soon ] } days!
150+ </ Form . Label >
151+ < br />
152+ < Form . Label htmlFor = { PurchaseTime . kindOfSoon } >
153+ < Form . Check
154+ type = "radio"
155+ id = { PurchaseTime . kindOfSoon }
156+ name = "when-to-buy"
157+ value = { PurchaseTime . kindOfSoon }
158+ required
159+ onChange = { ( ) => handleNextPurchaseChange ( PurchaseTime . kindOfSoon ) }
160+ checked = { itemNextPurchaseTimeline === PurchaseTime . kindOfSoon }
161+ aria-label = { `Set buy to kind of soon, within ${ purchaseTimelines [ PurchaseTime . kindOfSoon ] } days` }
162+ />
163+ Kind of soon -- Within { purchaseTimelines [ PurchaseTime . kindOfSoon ] } { " " }
164+ days!
165+ </ Form . Label >
166+ < br />
167+ < label htmlFor = { PurchaseTime . notSoon } >
168+ < Form . Check
169+ type = "radio"
170+ id = { PurchaseTime . notSoon }
171+ name = "when-to-buy"
172+ value = { PurchaseTime . notSoon }
173+ required
174+ onChange = { ( ) => handleNextPurchaseChange ( PurchaseTime . notSoon ) }
175+ checked = { itemNextPurchaseTimeline === PurchaseTime . notSoon }
176+ aria-label = { `Set buy to not soon, within ${ purchaseTimelines [ PurchaseTime . notSoon ] } days` }
112177 />
113- < br />
114- < h3 > Next, pick when you plan on buying this item again!</ h3 >
115- < fieldset className = "border border-2 rounded-1 p-2 mb-4" >
116- < legend className = "fs-2 float-none w-auto p-2" >
117- When to buy:
118- </ legend >
119- < Form . Check
120- type = "radio"
121- className = "mb-3 ms-3"
122- id = { PurchaseTime . soon }
123- name = "when-to-buy"
124- value = { PurchaseTime . soon }
125- required
126- onChange = { ( ) => handleNextPurchaseChange ( PurchaseTime . soon ) }
127- checked = { itemNextPurchaseTimeline === PurchaseTime . soon }
128- aria-label = { `Set buy to soon, within ${ purchaseTimelines [ PurchaseTime . soon ] } days` }
129- label = { `Soon -- Within ${ purchaseTimelines [ PurchaseTime . soon ] } days!` }
130- />
131- < Form . Check
132- type = "radio"
133- className = "mb-3 ms-3"
134- id = { PurchaseTime . kindOfSoon }
135- name = "when-to-buy"
136- value = { PurchaseTime . kindOfSoon }
137- required
138- onChange = { ( ) =>
139- handleNextPurchaseChange ( PurchaseTime . kindOfSoon )
140- }
141- checked = { itemNextPurchaseTimeline === PurchaseTime . kindOfSoon }
142- aria-label = { `Set buy to kind of soon, within ${ purchaseTimelines [ PurchaseTime . kindOfSoon ] } days` }
143- label = { `Kind of soon -- Within
144- ${ purchaseTimelines [ PurchaseTime . kindOfSoon ] } days!` }
145- />
146- < Form . Check
147- type = "radio"
148- className = "mb-3 ms-3"
149- id = { PurchaseTime . notSoon }
150- name = "when-to-buy"
151- value = { PurchaseTime . notSoon }
152- required
153- onChange = { ( ) => handleNextPurchaseChange ( PurchaseTime . notSoon ) }
154- checked = { itemNextPurchaseTimeline === PurchaseTime . notSoon }
155- aria-label = { `Set buy to not soon, within ${ purchaseTimelines [ PurchaseTime . notSoon ] } days` }
156- label = { `Not soon -- Within ${ purchaseTimelines [ PurchaseTime . notSoon ] } days!` }
157- />
158- </ fieldset >
159- < Button
160- type = "submit"
161- aria-label = "Add item to shopping list"
162- className = "mb-3"
163- >
164- Submit Item
165- </ Button >
166- </ Form >
167- < h4 > Let's go look at your list!</ h4 >
168- < Button onClick = { navigateToListPage } className = "my-3" >
169- { "View List" }
170- </ Button >
171- </ >
172- ) }
178+ Not soon -- Within { purchaseTimelines [ PurchaseTime . notSoon ] } days!
179+ </ label >
180+ </ fieldset >
181+ < Button type = "submit" aria-label = "Add item to shopping list" >
182+ Submit Item
183+ </ Button >
184+ </ Form >
185+ < h4 > Let's go look at your list!</ h4 >
186+ < Button onClick = { navigateToListPage } > { "View List" } </ Button >
173187 </ section >
174188 ) ;
175189}
0 commit comments