-
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
Changes from 9 commits
ec8f878
70f3628
7ef8d7f
186c735
63e3396
94d0353
a8ae94c
eeb5e90
ccd7c2e
9316bb0
c24d5ee
fb49a38
5810e7b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import { collection, onSnapshot } from 'firebase/firestore'; | ||
import { addDoc, collection, onSnapshot } from 'firebase/firestore'; | ||
import { db } from './config'; | ||
import { getFutureDate } from '../utils'; | ||
|
||
|
@@ -53,17 +53,18 @@ export function getItemData(snapshot) { | |
*/ | ||
export async function addItem(listId, { itemName, daysUntilNextPurchase }) { | ||
const listCollectionRef = collection(db, listId); | ||
// TODO: Replace this call to console.log with the appropriate | ||
// Firebase function, so this information is sent to your database! | ||
return console.log(listCollectionRef, { | ||
const data = 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! | ||
dateLastPurchased: null, | ||
dateNextPurchased: getFutureDate(daysUntilNextPurchase), | ||
name: itemName, | ||
totalPurchases: 0, | ||
}).catch((err) => { | ||
console.error(err); | ||
}); | ||
return data; | ||
|
||
} | ||
|
||
export async function updateItem() { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,114 @@ | ||
export function AddItem() { | ||
return <p>Hello from the <code>/add-item</code> page!</p> | ||
import { useEffect, useState } from 'react'; | ||
import { addItem } from '../api/firebase'; | ||
|
||
const timeframeToDays = { | ||
Soon: 7, | ||
'Kind of Soon': 14, | ||
'Not Soon': 30, | ||
|
||
}; | ||
|
||
export function AddItem({ listId }) { | ||
const [timeframe, setTimeframe] = useState('Soon'); | ||
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 = (e) => { | ||
e.preventDefault(); | ||
// Make sure the user has entered an item name | ||
if (!itemName) { | ||
setMessage('Please enter an item name'); | ||
return; | ||
} | ||
|
||
const result = addItem(listId, { | ||
itemName, | ||
daysUntilNextPurchase: timeframeToDays[timeframe], | ||
}); | ||
if (result) { | ||
setMessage(`Added ${itemName} to your list.`); | ||
setItemName(''); | ||
setTimeframe('Soon'); | ||
} else { | ||
setMessage('Error adding item'); | ||
} | ||
}; | ||
|
||
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" | ||
katherineyuneman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
value={itemName} | ||
onChange={onItemChange} | ||
></input> | ||
</div> | ||
<fieldset | ||
style={{ | ||
border: 'none', | ||
padding: 0, | ||
display: 'grid', | ||
gridTemplateColumns: '1fr 1fr', | ||
}} | ||
> | ||
<label htmlFor="timeframe" style={{ gridColumn: 'span 2' }}> | ||
How soon will you buy this again? | ||
</label> | ||
|
||
|
||
<input | ||
type="radio" | ||
name="timeframe" | ||
value="Soon" | ||
id="soon" | ||
checked={timeframe === 'Soon'} | ||
onChange={onTimeChange} | ||
/> | ||
|
||
<label htmlFor="soon">Soon</label> | ||
|
||
<input | ||
type="radio" | ||
name="timeframe" | ||
value="Kind of Soon" | ||
id="kindOfSoon" | ||
checked={timeframe === 'Kind of Soon'} | ||
onChange={onTimeChange} | ||
/> | ||
|
||
<label htmlFor="kindOfSoon">Kind of Soon</label> | ||
|
||
<input | ||
type="radio" | ||
name="timeframe" | ||
value="Not Soon" | ||
id="notSoon" | ||
checked={timeframe === 'Not Soon'} | ||
onChange={onTimeChange} | ||
/> | ||
<label htmlFor="notSoon">Not Soon</label> | ||
</fieldset> | ||
<button type="submit">Add Item</button> | ||
{message && <p>{message}</p>} | ||
</form> | ||
); | ||
} |
Uh oh!
There was an error while loading. Please reload this page.