Skip to content

Commit

Permalink
Merge branch 'main' into dtp-nr-sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
NickRoccodev11 authored Sep 21, 2024
2 parents 7835574 + a0a270f commit 8279ed4
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 10 deletions.
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);
}
29 changes: 27 additions & 2 deletions 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 @@ -13,9 +13,11 @@ export function ListItem({
purchaseInterval,
dateCreated,
sortCriteria,
setMessage,
}) {
const [purchased, setPurchased] = useToggle(false);
const [isDisabled, setIsDisabled] = useState(false);

useEffect(() => {
if (dateLastPurchased) {
const checkExpiration = () => {
Expand Down Expand Up @@ -59,13 +61,35 @@ 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);
}
}
};

const urgencyClass = sortCriteria.tag.toLowerCase().replace(/\s/g, '');

return (
<>
<tr className="ListItem">
<td>{name}</td>
<td>{name}
<button onClick={handleDelete} aria-label={`Delete ${name}`}>
Delete
</button>
</td>

<td>
<Toggle
toggle={handleToggle}
Expand All @@ -80,6 +104,7 @@ export function ListItem({
</td>
<td className={urgencyClass}>{sortCriteria.tag}</td>
</tr>

</>
);
}
19 changes: 17 additions & 2 deletions src/views/List.jsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { useState } from 'react';
import { useEffect, useState } from 'react';
import { ListItem } from '../components';
import { NavLink } from 'react-router-dom';
import { comparePurchaseUrgency } from '../utils/dates.js';

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

const handleInputChange = (e) => {
setSearchInput(e.target.value);
setMessage('');
};

const clearSearchInput = () => {
Expand All @@ -21,6 +24,14 @@ export function List({ data, listPath }) {
: item;
});

useEffect(() => {
if (message !== '') {
setInterval(() => {
setMessage('');
}, 5000);
}
}, [message]);

return (
<>
<p>
Expand Down Expand Up @@ -73,14 +84,18 @@ export function List({ data, listPath }) {
dateLastPurchased={item.dateLastPurchased}
purchaseInterval={item.purchaseInterval}
dateCreated={item.dateCreated}
setMessage={setMessage}
sortCriteria={item.sortCriteria}
/>
))}
</tbody>
</table>
) : (
<p>No items to display</p>
)}
)}
<br />
<span>{message}</span>

</>
);
}

0 comments on commit 8279ed4

Please sign in to comment.