Skip to content

Commit

Permalink
Merge pull request #29 from the-collab-lab/mp-eb-delete-item
Browse files Browse the repository at this point in the history
Delete Item
  • Loading branch information
EmmaBin authored Sep 20, 2024
2 parents 2907fc7 + 28ab926 commit dd5ebac
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
13 changes: 7 additions & 6 deletions src/api/firebase.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
onSnapshot,
updateDoc,
increment,
deleteDoc,
} from 'firebase/firestore';
import { useEffect, useState } from 'react';
import { db } from './config';
Expand Down Expand Up @@ -228,10 +229,10 @@ export async function updateItem(listPath, itemId, item) {
return updateDoc(updateItemListDocRef, updateData);
}

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, item) {
try {
await deleteDoc(doc(db, listPath, 'items', item.name));
} catch (error) {
console.log(error);
}
}
16 changes: 14 additions & 2 deletions src/components/ListItem.jsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
import { useState, useEffect } from 'react';
import './ListItem.css';
import { updateItem } from '../api';
import { updateItem, deleteItem } from '../api';

export function ListItem({ item }) {
const { name, dateLastPurchased, dateCreated, id } = item;
const { name, dateLastPurchased, id } = item;
const [checked, setChecked] = useState(false);

const handleDelete = async () => {
try {
if (window.confirm(`Are you sure you want to delete ${name}?`)) {
const listPath = localStorage.getItem('tcl-shopping-list-path');
await deleteItem(listPath, item);
}
} catch (error) {
console.log(error);
}
};

const has24HoursPassed = (dateLastPurchased) => {
const millisecondsFromTimestamp =
dateLastPurchased.seconds * 1000 +
Expand Down Expand Up @@ -47,6 +58,7 @@ export function ListItem({ item }) {
disabled={checked}
/>
{name}
<button onClick={handleDelete}>Delete</button>
</li>
);
}

0 comments on commit dd5ebac

Please sign in to comment.