Skip to content

Commit

Permalink
Merge branch 'main' into ec-hm-issue-13
Browse files Browse the repository at this point in the history
  • Loading branch information
3campos authored Mar 14, 2024
2 parents d026db8 + e69acd4 commit dde5e5a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 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,12 +203,10 @@ 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);
}

/**
Expand Down
19 changes: 17 additions & 2 deletions src/components/ListItem.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { updateItem } from '../api';
import { getDifferenceBetweenDates, todaysDate } from '../utils';
import { getDifferenceBetweenDates, todaysDate, subtractDates } from '../utils';
import { colorPicker, calculateUrgency } from '../utils/helpers';
import { updateItem, deleteItem } from '../api';
import { Timestamp } from 'firebase/firestore';
import { calculateEstimate } from '@the-collab-lab/shopping-list-utils';
import './ListItem.css';
Expand Down Expand Up @@ -64,6 +64,18 @@ export function ListItem({
let urgency = calculateUrgency(daysTillNextPurchase, daysSinceLastPurchase);
let textColor = colorPicker(urgency);

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 @@ -76,6 +88,9 @@ export function ListItem({
checked={isChecked()}
></input>
</label>
<button onClick={handleDelete} className="delete-button">
Delete
</button>
</li>
);
}

0 comments on commit dde5e5a

Please sign in to comment.