Skip to content

Commit

Permalink
Merge pull request #29 from the-collab-lab/hm-mm-deleteItem
Browse files Browse the repository at this point in the history
  • Loading branch information
Hudamabkhoot authored Sep 21, 2024
2 parents 8704b58 + e21a512 commit 825f492
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
15 changes: 9 additions & 6 deletions src/api/firebase.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
updateDoc,
addDoc,
Timestamp,
deleteDoc,
} from 'firebase/firestore';
import { useEffect, useState } from 'react';
import { db } from './config';
Expand Down Expand Up @@ -232,10 +233,12 @@ export async function updateItem(listPath, checked, itemData) {
}
}

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, id) {
const listCollectionRef = collection(db, listPath, 'items');
const itemRef = doc(listCollectionRef, id);
try {
await deleteDoc(itemRef);
} catch (error) {
console.error('Error deleting your item', error);
}
}
16 changes: 15 additions & 1 deletion src/components/ListItem.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import './ListItem.css';
import { updateItem } from '../api';
import { updateItem, deleteItem } from '../api';
import { useEffect } from 'react';
import { ONE_DAY_IN_MILLISECONDS } from '../utils/dates';
import toast from 'react-hot-toast';

export function ListItem({
listPath,
Expand All @@ -26,6 +27,16 @@ export function ListItem({
});
};

const handleDelete = async () => {
const confirm = window.confirm(`are you sure you want to delete ${name}?`);
if (confirm) {
await deleteItem(listPath, id);
toast.success(`${name} was deleted from the list`);
} else {
toast.error('Deletion canceled');
}
};

useEffect(() => {
const today = new Date().getTime();
const datePurchasedInMillis = dateLastPurchased?.toMillis();
Expand All @@ -51,6 +62,9 @@ export function ListItem({
disabled={isChecked}
/>
<label htmlFor={`${id}`}>{name}</label>
<button type="button" id={id} onClick={handleDelete}>
Delete
</button>
</li>
);
}

0 comments on commit 825f492

Please sign in to comment.