diff --git a/src/api/firebase.js b/src/api/firebase.js index 66bc91a..c1b54d9 100644 --- a/src/api/firebase.js +++ b/src/api/firebase.js @@ -163,12 +163,10 @@ 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, { + + 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! dateLastPurchased: null, dateNextPurchased: getFutureDate(daysUntilNextPurchase), name: itemName, diff --git a/src/views/ManageList.jsx b/src/views/ManageList.jsx index e1c5fde..a2f3963 100644 --- a/src/views/ManageList.jsx +++ b/src/views/ManageList.jsx @@ -1,7 +1,91 @@ +import { useState } from 'react'; +import { addItem } from '../api'; + export function ManageList() { + const [formData, setFormData] = useState({ + name: '', + frequency: '', + }); + + function handleChange(e) { + e.preventDefault(); + setFormData((prev) => ({ + ...prev, + [e.target.name]: e.target.value, + })); + } + + function handleSubmit(e) { + e.preventDefault(); + if (formData.name.trim() === '') { + window.alert('Item name cannot be empty.'); + return; + } + + 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); + }); + } + return ( -
- Hello from the /manage-list
page!
-
+ Hello from the /manage-list
page!
+