Skip to content

Commit

Permalink
feat(#12): Added a delete button with functionality, as well as handl…
Browse files Browse the repository at this point in the history
…e JS confirm dialog and shows user a message accordingly.
  • Loading branch information
shuveksha-tuladhar committed Sep 17, 2024
1 parent f21161b commit a008115
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
5 changes: 4 additions & 1 deletion 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,12 @@ export async function updateItem(
});
}

export async function deleteItem() {
export async function deleteItem(listPath, itemId) {
/**
* 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!
*/
const itemDocRef = doc(db, listPath, `items`, itemId);
await deleteDoc(itemDocRef);
}
22 changes: 21 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,21 @@ export function ListItem({
}
};

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 +89,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

0 comments on commit a008115

Please sign in to comment.