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

Issue #12: Delete Items from Shopping List #31

Merged
merged 3 commits into from
Sep 21, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
10 changes: 4 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,
addDoc,
deleteDoc,
} from 'firebase/firestore';
import { useEffect, useState } from 'react';
import { db } from './config';
Expand Down Expand Up @@ -233,10 +234,7 @@ export async function updateItem(
});
}

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) {
const itemDocRef = doc(db, listPath, `items`, itemId);
await deleteDoc(itemDocRef);
}
23 changes: 22 additions & 1 deletion src/components/ListItem.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useEffect, useState } from 'react';
import { useToggle } from '@uidotdev/usehooks';
import { Toggle } from './Toggle.jsx';
import './ListItem.css';
import { updateItem } from '../api/firebase.js';
import { updateItem, deleteItem } from '../api/firebase.js';

export function ListItem({
name,
Expand All @@ -12,9 +12,11 @@ export function ListItem({
dateLastPurchased,
purchaseInterval,
dateCreated,
setMessage,
}) {
const [purchased, setPurchased] = useToggle(false);
const [isDisabled, setIsDisabled] = useState(false);

useEffect(() => {
if (dateLastPurchased) {
const checkExpiration = () => {
Expand Down Expand Up @@ -59,6 +61,22 @@ export function ListItem({
}
};

// handleDelete Function
const handleDelete = async () => {
const deleteConfirm = window.confirm(
`Are you sure you want to delete ${name}?`,
);

if (deleteConfirm) {
try {
await deleteItem(listPath, itemId);
setMessage(`${name} has been deleted successfully!`);
} catch (error) {
console.log(`Error:`, error);
}
}
};

return (
<>
<li className="ListItem">
Expand All @@ -72,6 +90,9 @@ export function ListItem({
/>

{dateLastPurchased ? dateLastPurchased.toDate().toLocaleString() : ''}
<button onClick={handleDelete} aria-label={`Delete ${name}`}>
Delete
</button>
</li>
</>
);
Expand Down
5 changes: 5 additions & 0 deletions src/views/List.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { NavLink } from 'react-router-dom';

export function List({ data, listPath }) {
const [searchInput, setSearchInput] = useState('');
const [message, setMessage] = useState('');

const handleInputChange = (e) => {
setSearchInput(e.target.value);
};
Expand Down Expand Up @@ -62,6 +64,7 @@ export function List({ data, listPath }) {
dateLastPurchased={item.dateLastPurchased}
purchaseInterval={item.purchaseInterval}
dateCreated={item.dateCreated}
setMessage={setMessage}
/>
);
})
Expand All @@ -71,6 +74,8 @@ export function List({ data, listPath }) {
No items found! <NavLink to="/manage-list"> Add item</NavLink>
</li>
)}
<br />
<span>{message}</span>
</ul>
</>
);
Expand Down