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

manage shopping list by adding new item #21

Merged
merged 8 commits into from
Aug 25, 2024
4 changes: 3 additions & 1 deletion src/api/firebase.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,9 @@ 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!
EmmaBin marked this conversation as resolved.
Show resolved Hide resolved
return console.log(listCollectionRef, {

const itemDocRef = doc(listCollectionRef, itemName);
return setDoc(itemDocRef, {
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
1 change: 1 addition & 0 deletions src/views/Home.jsx
EmmaBin marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export function Home({ data, setListPath }) {
<ul>
{data &&
data.map((list) => {
console.log('here is list path', list.path);
EmmaBin marked this conversation as resolved.
Show resolved Hide resolved
return (
<SingleList
key={crypto.randomUUID()}
Expand Down
90 changes: 87 additions & 3 deletions src/views/ManageList.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,91 @@
import { useState } from 'react';
import { addItem } from '../api';

EmmaBin marked this conversation as resolved.
Show resolved Hide resolved
export function ManageList() {
const [formData, setFormData] = useState({
name: '',
frequency: '',
});

// const [formSubmitted, setFormSubmitted] = useState(false);
EmmaBin marked this conversation as resolved.
Show resolved Hide resolved

function handleChange(e) {
e.preventDefault();
setFormData((prev) => ({
...prev,
[e.target.name]: e.target.value,
}));
}

function handleSubmit(e) {
e.preventDefault();

console.log('here is form submission', formData);
EmmaBin marked this conversation as resolved.
Show resolved Hide resolved

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);
});
}
Comment on lines +32 to +47
Copy link
Member

Choose a reason for hiding this comment

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

note (non-blocking): This does read very cleanly! I like that you considered that in your usage here. And it does work well here, but it may be worthwhile to know that it's not an exact swap for using async/await see here for a document on it!


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

@mentalcaries mentalcaries Aug 22, 2024

Choose a reason for hiding this comment

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

bug the success message works when an item is added, however the message doesn't clear. If I submit an item, and then start submitting a second item, the success message just gets updated with the new item name

image

Copy link
Collaborator

Choose a reason for hiding this comment

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

praise (non-blocking): I like how an alert is used for showing the message here! And I see it was used to solve this bug, great idea.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@mentalcaries Thank you for catching this bug and the issue we overlooked with implementing addItem.

</>
);
}
Loading