Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delete Item #29

Merged
merged 1 commit into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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}?`)) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the check here :) and the use of async/await

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>
);
}
Loading