diff --git a/src/api/firebase.js b/src/api/firebase.js index 7b1b65f..a16311a 100644 --- a/src/api/firebase.js +++ b/src/api/firebase.js @@ -2,6 +2,7 @@ import { arrayUnion, getDoc, setDoc, + deleteDoc, collection, doc, onSnapshot, @@ -202,10 +203,8 @@ export async function updateItem( }); } -export async function deleteItem() { - /** - * TODO: Fill this out so that it uses the correct Firestore function - * to delete an existing item. You'll need to figure out what arguments - * this function must accept! - */ +export async function deleteItem(listPath, itemId) { + const listCollectionRef = collection(db, listPath, 'items'); + const itemDocRef = doc(listCollectionRef, itemId); + return deleteDoc(itemDocRef); } diff --git a/src/components/ListItem.jsx b/src/components/ListItem.jsx index 8bebaf9..bab17b3 100644 --- a/src/components/ListItem.jsx +++ b/src/components/ListItem.jsx @@ -1,4 +1,4 @@ -import { updateItem } from '../api'; +import { updateItem, deleteItem } from '../api'; import { subtractDates } from '../utils'; import { Timestamp } from 'firebase/firestore'; import { calculateEstimate } from '@the-collab-lab/shopping-list-utils'; @@ -43,6 +43,18 @@ export function ListItem({ } }; + const handleDelete = async () => { + try { + if (window.confirm('Are you sure you want to delete this item?')) { + await deleteItem(listPath, id); + } else { + return; + } + } catch (err) { + console.error(err.message); + } + }; + return (
  • +
  • ); }