Skip to content

Commit

Permalink
Added delete button and implemented a function. Created a confirm win…
Browse files Browse the repository at this point in the history
…dow for the user upon deleting item. Filled out deleteItem function in firebase

Co-authored-by: EmmaBin <[email protected]>
  • Loading branch information
MarcosPerez16 and EmmaBin committed Sep 17, 2024
1 parent 2907fc7 commit 28ab926
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 28ab926

Please sign in to comment.