-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Issue 5. Add Items to list #20
Changes from 6 commits
f49ce05
0f24cac
e32236d
854f7b8
667147e
4160494
078f475
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,80 @@ | ||
export function ManageList() { | ||
import { addItem } from '../api/firebase'; | ||
import { useRef } from 'react'; | ||
|
||
export function ManageList({ listPath }) { | ||
const formRef = useRef(); | ||
|
||
const handleSubmit = async (e) => { | ||
e.preventDefault(); | ||
|
||
const formData = new FormData(e.target); | ||
const itemName = formData.get('itemName'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Cool approach here using the FormData api in conjunction with default form/input functionality to access necessary data. You don't see this often in client-side React apps, but totally legit thing to do so that's cool to see. |
||
const daysUntilNextPurchase = parseInt( | ||
formData.get('daysUntilNextPurchase'), | ||
); | ||
|
||
try { | ||
await addItem(listPath, { | ||
itemName, | ||
daysUntilNextPurchase, | ||
}); | ||
|
||
alert('Item saved successfully'); | ||
} catch (error) { | ||
alert('There was a problem'); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't necessarily have to fully display the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I modified the catch block to display a more detailed error message. try {
...
} catch (error) {
alert(`There was a problem: ${error.message}`);
}; |
||
|
||
formRef.current.reset(); | ||
}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder whether this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This makes sense, clearing a form after a successful submit is expected behavior, not clearing it would clearly indicate there was a problem. I added this change. |
||
|
||
return ( | ||
<p> | ||
Hello from the <code>/manage-list</code> page! | ||
</p> | ||
<form ref={formRef} onSubmit={handleSubmit}> | ||
<div> | ||
<label htmlFor="item">Item Name</label> | ||
<input name="itemName" type="text" id="item" required /> | ||
</div> | ||
|
||
<div> | ||
<label htmlFor="daysUntilNextPurchase"> | ||
How soon do you think you'll purchase this item again? | ||
</label> | ||
|
||
<p>Please select an option:</p> | ||
|
||
<ul> | ||
<li> | ||
<label htmlFor="soon"> Soon (7 days)</label> | ||
<input | ||
value={7} | ||
name="daysUntilNextPurchase" | ||
type="radio" | ||
id="soon" | ||
required | ||
/> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I said this to the other group as well, but the built in HTML validations actually aren't as accessible as one would hope. So I think down the road we should build our own validation + status displaying logic. |
||
</li> | ||
<li> | ||
<label htmlFor="kind-of-soon"> Kind of Soon (14 days)</label> | ||
<input | ||
value={14} | ||
name="daysUntilNextPurchase" | ||
type="radio" | ||
id="kind-of-soon" | ||
required | ||
/> | ||
</li> | ||
<li> | ||
<label htmlFor="not-soon"> Not Soon (30 days)</label> | ||
<input | ||
value={30} | ||
name="daysUntilNextPurchase" | ||
type="radio" | ||
id="not-soon" | ||
required | ||
/> | ||
</li> | ||
</ul> | ||
</div> | ||
<button type="submit">Submit</button> | ||
</form> | ||
); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the best practice recommendation by the React team is to declare this as
const formRef = useRef(null)
if you are going to assign the reference to a DOM element. I'm honestly not sure the differentnull
vsundefined
makes in the initial declaration in reality, but I would still recommend doing it so that your code can be as consistent as possible.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Noted. I will add this change. Thanks!