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

Deleting item off the list #29

Merged
merged 4 commits into from
Sep 21, 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
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>
);
}
Loading