Skip to content
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

Merged
merged 7 commits into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ export function App() {
element={<Home data={lists} setListPath={setListPath} />}
/>
<Route path="/list" element={<List data={data} />} />
<Route path="/manage-list" element={<ManageList />} />
<Route
path="/manage-list"
element={<ManageList listPath={listPath} />}
/>
</Route>
</Routes>
</Router>
Expand Down
5 changes: 2 additions & 3 deletions src/api/firebase.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
doc,
onSnapshot,
updateDoc,
addDoc,
} from 'firebase/firestore';
import { useEffect, useState } from 'react';
import { db } from './config';
Expand Down Expand Up @@ -163,9 +164,7 @@ export async function shareList(listPath, currentUserId, recipientEmail) {
*/
export async function addItem(listPath, { itemName, daysUntilNextPurchase }) {
const listCollectionRef = collection(db, listPath, 'items');
// TODO: Replace this call to console.log with the appropriate
// Firebase function, so this information is sent to your database!
return console.log(listCollectionRef, {
await addDoc(listCollectionRef, {
dateCreated: new Date(),
// NOTE: This is null because the item has just been created.
// We'll use updateItem to put a Date here when the item is purchased!
Expand Down
81 changes: 77 additions & 4 deletions src/views/ManageList.jsx
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();

Copy link
Collaborator

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 different null vs undefined makes in the initial declaration in reality, but I would still recommend doing it so that your code can be as consistent as possible.

Copy link
Collaborator

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!

const handleSubmit = async (e) => {
e.preventDefault();

const formData = new FormData(e.target);
const itemName = formData.get('itemName');
Copy link
Collaborator

Choose a reason for hiding this comment

The 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');
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't necessarily have to fully display the error in this catch statement, but I do think we could make this error status more specific.

Copy link
Collaborator

Choose a reason for hiding this comment

The 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();
};
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder whether this reset() should be inside of the try branch of the try ... catch .... If there is an error that means that the item wasn't saved, so we probably don't want to clear the form on people (in case they want to try again?)

Copy link
Collaborator

@jaguilar89 jaguilar89 Feb 16, 2024

Choose a reason for hiding this comment

The 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
/>
Copy link
Collaborator

Choose a reason for hiding this comment

The 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>
);
}
Loading