Skip to content

Commit

Permalink
Merge pull request #36 from the-collab-lab/an-sc-issue-12
Browse files Browse the repository at this point in the history
12. As a user, I want to be able to delete items from my shopping list so that my list isn’t cluttered with items I don’t want to buy in the future.
  • Loading branch information
eonflower authored Mar 14, 2024
2 parents f1cbcf9 + 8567d99 commit e69acd4
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
11 changes: 5 additions & 6 deletions src/api/firebase.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
arrayUnion,
getDoc,
setDoc,
deleteDoc,
collection,
doc,
onSnapshot,
Expand Down Expand Up @@ -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);
}
17 changes: 16 additions & 1 deletion src/components/ListItem.jsx
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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 (
<li className="ListItem">
<label>
Expand All @@ -55,6 +67,9 @@ export function ListItem({
checked={subtractDates(todaysDate, dateLastPurchased)}
></input>
</label>
<button onClick={handleDelete} className="delete-button">
Delete
</button>
</li>
);
}

0 comments on commit e69acd4

Please sign in to comment.