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

export function ManageList({ listPath }) {
const [itemName, setItemName] = useState('');
const [daysUntilNextPurchase, setDaysUntilNextPurchase] = useState(0);

const handleChangeItem = (e) => {
setItemName(e.target.value);
};

const handleChangeDate = (e) => {
setDaysUntilNextPurchase(parseInt(e.target.value));
};

const handleSubmit = async (e) => {
e.preventDefault();
try {
const newItem = await addItem(listPath, {
itemName,
jaguilar89 marked this conversation as resolved.
Show resolved Hide resolved
daysUntilNextPurchase,
});
alert('Item saved successfully.');
} catch (error) {
alert('Item not saved successfully.');
}
setItemName('');
document.getElementById('item-form').reset();
};
jaguilar89 marked this conversation as resolved.
Show resolved Hide resolved

return (
<p>
Hello from the <code>/manage-list</code> page!
</p>
<div>
<form id="item-form" onSubmit={handleSubmit}>
jaguilar89 marked this conversation as resolved.
Show resolved Hide resolved
<div>
Copy link
Member

Choose a reason for hiding this comment

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

I would recommend moving this form to a separate component in case we use it in the future. For example, we can create a component called RadioInput. The component should use props to receive the options, like this:

const options = [{label: "Soon (7 days)", value: 7, id: "soon"}, {label: "Kind of soon (14 days)", value: 14, id:"kind-of-soon"}];


...

<RadioInput name="daysUntilNextPurchase" options={options}/>

and inside of the component we can use those props:

function RadioInput({name, options}) {

  return (
    <ul>
      {options.map((option) => (
          <li>
             <label htmlFor={option.id}>{option.label}</label>
             <input value={option.value} name={name}... >
          </li>
        )
      )}
    <ul/>
  ) 
}

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 is a good suggestion. However, Paige and I think it's unnecessary for now simply because we don't think a radio button component will be needed in the future, based on the acceptance criteria of future issues. But, we can always keep this suggestion in mind in case it does become a necessary change in the future.

<label htmlFor="item">Item Name</label>
<input
value={itemName}
onChange={handleChangeItem}
name="itemName"
type="text"
id="item"
required
/>
Copy link
Collaborator

Choose a reason for hiding this comment

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

added required so form field can't be left blank on submit

</div>

<div>
<label htmlFor="daysUntilNextPurchase">
How soon do you think you'll purchase this item again?
</label>
<ul>
<li>
<label htmlFor="soon"> Soon (7 days)</label>
<input
value={7}
onChange={handleChangeDate}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Consider making this your default value, to enhance accessibility. This can be achieved by adding another attribute here. If you chose to do this, you'll also need to update the initial state value to be "7".

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.

We initially leave this part of the form unselected to account for accidental form submissions where an item name would be provided but the first radio button would already be selected, before the user has a chance to select it on their own. Whereas if there is no default radio button value, the user would see an error message, asking them to select an option first. See below screenshot.

image

As an alternative, we added a clear "Please select option" instruction to make it clear that the user must make a selection before submitting the form. See below screenshot.

image

Copy link
Collaborator

Choose a reason for hiding this comment

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

@trushmi What do you think?

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.

added id attribute to match forHTML attribute on <label> for accessibility

/>
Copy link
Collaborator

Choose a reason for hiding this comment

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

added required so form field can't be left blank on submit

</li>
<li>
<label htmlFor="kind-of-soon"> Kind of Soon (14 days)</label>
<input
value={14}
onChange={handleChangeDate}
name="daysUntilNextPurchase"
type="radio"
id="kind-of-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.

added id attribute to match forHTML attribute on <label> for accessibility

/>
Copy link
Collaborator

Choose a reason for hiding this comment

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

added required so form field can't be left blank on submit

</li>
<li>
<label htmlFor="not-soon"> Not Soon (30 days)</label>
<input
value={30}
onChange={handleChangeDate}
name="daysUntilNextPurchase"
type="radio"
id="not-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.

added id attribute to match forHTML attribute on <label> for accessibility

/>
</li>
</ul>
</div>
<button type="submit">Submit</button>
</form>
</div>
);
}
Loading