generated from the-collab-lab/smart-shopping-list
-
Notifications
You must be signed in to change notification settings - Fork 0
Add addItem function import #19
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
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
ec8f878
Add addItem function import
ticiadev 70f3628
Add UI form elements
ticiadev 7ef8d7f
Add backend functionality to add item to Firebase
ticiadev 186c735
Remove TODO from firebase
jeremiahfallin 63e3396
ensure user enters item name
jeremiahfallin 94d0353
fix typo in timeframe initial state
jeremiahfallin a8ae94c
add id to item name input
jeremiahfallin eeb5e90
Refactor form onSubmit into a separate function
ticiadev ccd7c2e
Add catch handler to addDoc
ticiadev 9316bb0
add error handling on addItem
jeremiahfallin c24d5ee
change label to legend for accessibility and refactor our unneccessar…
jeremiahfallin fb49a38
remove unnecessary catch
jeremiahfallin 5810e7b
fix difference in types for timeframe
jeremiahfallin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,111 @@ | ||
export function AddItem() { | ||
return <p>Hello from the <code>/add-item</code> page!</p> | ||
import { useEffect, useState } from 'react'; | ||
import { addItem } from '../api/firebase'; | ||
|
||
export function AddItem({ listId }) { | ||
const [timeframe, setTimeframe] = useState('7'); | ||
const [itemName, setItemName] = useState(''); | ||
const [message, setMessage] = useState(null); | ||
|
||
useEffect(() => { | ||
const timer = setTimeout(() => { | ||
setMessage(null); | ||
}, 3000); | ||
|
||
return () => clearTimeout(timer); | ||
}, [message]); | ||
|
||
const onTimeChange = (e) => setTimeframe(e.target.value); | ||
const onItemChange = (e) => setItemName(e.target.value); | ||
const onFormSubmit = async (e) => { | ||
e.preventDefault(); | ||
// Make sure the user has entered an item name | ||
if (!itemName) { | ||
setMessage('Please enter an item name'); | ||
return; | ||
} | ||
|
||
const result = await addItem(listId, { | ||
itemName, | ||
daysUntilNextPurchase: timeframe, | ||
}); | ||
if (result) { | ||
setMessage(`Added ${itemName} to your list.`); | ||
setItemName(''); | ||
setTimeframe('7'); | ||
} else { | ||
setMessage('Error adding item, please try again.'); | ||
} | ||
}; | ||
|
||
return ( | ||
<form onSubmit={onFormSubmit}> | ||
<div | ||
style={{ | ||
border: 'none', | ||
padding: 0, | ||
paddingBottom: '1.5rem', | ||
display: 'flex', | ||
flexDirection: 'column', | ||
}} | ||
> | ||
<label htmlFor="itemName">Item name:</label> | ||
<input | ||
type="text" | ||
id="itemName" | ||
name="itemName" | ||
value={itemName} | ||
onChange={onItemChange} | ||
></input> | ||
</div> | ||
<fieldset | ||
style={{ | ||
border: 'none', | ||
padding: 0, | ||
display: 'grid', | ||
gridTemplateColumns: '1fr 1fr', | ||
}} | ||
> | ||
<legend | ||
htmlFor="timeframe" | ||
style={{ padding: 0, gridColumn: 'span 2' }} | ||
> | ||
How soon will you buy this again? | ||
</legend> | ||
|
||
<input | ||
type="radio" | ||
name="timeframe" | ||
value={7} | ||
id="soon" | ||
checked={timeframe === '7'} | ||
onChange={onTimeChange} | ||
/> | ||
|
||
<label htmlFor="soon">Soon</label> | ||
|
||
<input | ||
type="radio" | ||
name="timeframe" | ||
value={14} | ||
id="kindOfSoon" | ||
checked={timeframe === '14'} | ||
onChange={onTimeChange} | ||
/> | ||
|
||
<label htmlFor="kindOfSoon">Kind of Soon</label> | ||
|
||
<input | ||
type="radio" | ||
name="timeframe" | ||
value={30} | ||
id="notSoon" | ||
checked={timeframe === '30'} | ||
onChange={onTimeChange} | ||
/> | ||
<label htmlFor="notSoon">Not Soon</label> | ||
</fieldset> | ||
<button type="submit">Add Item</button> | ||
{message && <p>{message}</p>} | ||
</form> | ||
); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.