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 all 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
80 changes: 76 additions & 4 deletions src/views/ManageList.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,79 @@
export function ManageList() {
import { addItem } from '../api/firebase';
import { useRef } from 'react';

export function ManageList({ listPath }) {
const formRef = useRef(null);

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');
formRef.current.reset();
} catch (error) {
alert(`There was a problem: ${error.message}`);
}
};

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