generated from the-collab-lab/smart-shopping-list
-
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.
Merge pull request #21 from the-collab-lab/ar-eb-add-new-item-issue-5
manage shopping list by adding new item
- Loading branch information
Showing
2 changed files
with
90 additions
and
8 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,7 +1,91 @@ | ||
import { useState } from 'react'; | ||
import { addItem } from '../api'; | ||
|
||
export function ManageList() { | ||
const [formData, setFormData] = useState({ | ||
name: '', | ||
frequency: '', | ||
}); | ||
|
||
function handleChange(e) { | ||
e.preventDefault(); | ||
setFormData((prev) => ({ | ||
...prev, | ||
[e.target.name]: e.target.value, | ||
})); | ||
} | ||
|
||
function handleSubmit(e) { | ||
e.preventDefault(); | ||
if (formData.name.trim() === '') { | ||
window.alert('Item name cannot be empty.'); | ||
return; | ||
} | ||
|
||
const listPath = localStorage.getItem('tcl-shopping-list-path'); | ||
|
||
if (!listPath) { | ||
window.alert('List is not existed.'); | ||
return; | ||
} | ||
|
||
addItem(listPath, { | ||
itemName: formData.name, | ||
daysUntilNextPurchase: parseInt(formData.frequency), | ||
}) | ||
.then(() => { | ||
window.alert(`${formData.name} has been added to your list.`); | ||
setFormData({ | ||
name: '', | ||
frequency: '', | ||
}); | ||
}) | ||
.catch((error) => { | ||
window.alert(`${formData.name} failed to add to your list.`); | ||
console.error('Error:', error); | ||
}); | ||
} | ||
|
||
return ( | ||
<p> | ||
Hello from the <code>/manage-list</code> page! | ||
</p> | ||
<> | ||
<p> | ||
Hello from the <code>/manage-list</code> page! | ||
</p> | ||
|
||
<div className="manage-list-form"> | ||
<form onSubmit={handleSubmit}> | ||
<label htmlFor="name">Name of item:</label> | ||
<input | ||
type="text" | ||
name="name" | ||
id="name-of-item" | ||
value={formData.name} | ||
onChange={handleChange} | ||
required | ||
></input> | ||
|
||
<label htmlFor="frequency"> | ||
When will you need this item again?: | ||
</label> | ||
|
||
<select | ||
id="frequency" | ||
value={formData.frequency} | ||
name="frequency" | ||
onChange={handleChange} | ||
required | ||
> | ||
<option value="" disabled> | ||
Select your option | ||
</option> | ||
<option value="7">Soon (7 days)</option> | ||
<option value="14">Kind of Soon (14 Days)</option> | ||
<option value="30">Not Soon (30 Days)</option> | ||
</select> | ||
|
||
<button type="submit">Submit</button> | ||
</form> | ||
</div> | ||
</> | ||
); | ||
} |