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

Ap sm delete item #33

Merged
merged 4 commits into from
Mar 17, 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
@@ -1,5 +1,6 @@
import {
addDoc,
deleteDoc,
arrayUnion,
getDoc,
setDoc,
Expand Down Expand Up @@ -239,10 +240,12 @@ export async function updateItem(listPath, itemID, isChecked) {
}
}

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, itemID) {
// Note - firebase deleteDoc will remove the entire collection if the item being removed is the last in said collection!
andiedoescode marked this conversation as resolved.
Show resolved Hide resolved
const listRef = doc(db, listPath, 'items', itemID);
try {
await deleteDoc(listRef);
} catch (error) {
console.log(error);
}
}
12 changes: 11 additions & 1 deletion src/components/ListItem.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import './ListItem.css';
import { updateItem } from '../api/firebase.js';
import { updateItem, deleteItem } from '../api/firebase.js';
import { useState, useEffect, useMemo } from 'react';
import { ONE_DAY_IN_MILLISECONDS } from '../utils/dates.js';

Expand Down Expand Up @@ -29,6 +29,13 @@ export function ListItem({ listPath, item }) {
purchaseItem();
};

const deleteHandler = async (e) => {
// Note: Should we add more user feedback when items are successfully deleted? Some might further interrupt usability.
if (window.confirm(`Are you sure you'd like to delete ${item.name}?`)) {
andiedoescode marked this conversation as resolved.
Show resolved Hide resolved
await deleteItem(listPath, item.id);
}
};

//Calculate time remaining if purchase was less than 24 hours ago
const updateTimer = () => {
if (item.dateLastPurchased) {
Expand Down Expand Up @@ -65,6 +72,9 @@ export function ListItem({ listPath, item }) {
checked={isChecked}
/>
</label>
<button type="button" onClick={deleteHandler}>
Delete
</button>
</li>
);
}
Loading