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

Dc el delete items #28

Merged
merged 4 commits into from
Sep 20, 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
11 changes: 10 additions & 1 deletion src/api/firebase.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
doc,
onSnapshot,
updateDoc,
deleteDoc,
} from 'firebase/firestore';
import { useEffect, useState } from 'react';
import { db } from './config';
Expand Down Expand Up @@ -219,10 +220,18 @@ export async function updateItem(listPath, itemId, { totalPurchases }) {
}
}

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!
*/
try {
const itemRef = doc(db, listPath, 'items', itemId);
await deleteDoc(itemRef);
return 'Item has been deleted successfully';
} catch (error) {
console.log(error);
throw new Error('Error deleting the item');
}
}
14 changes: 8 additions & 6 deletions src/components/ListItem.jsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import { useState, useEffect } from 'react';
import './ListItem.css';

export function ListItem({ name, dateLastPurchased, onCheck }) {
export function ListItem({ name, dateLastPurchased, onCheck, onDelete }) {
const [isChecked, setIsChecked] = useState(false);

// Update `isChecked` based on the `dateLastPurchased` value
// Update `isChecked` based on the `dateLastPurchased` value
useEffect(() => {
const checkStatus = () => {
if (dateLastPurchased) {
const purchaseDate = dateLastPurchased.toDate();
const timeSinceLastPurchase = new Date() - purchaseDate;
const hasBeenPurchasedRecently =

timeSinceLastPurchase < 24 * 60 * 60 * 1000; // 24 hours
timeSinceLastPurchase < 24 * 60 * 60 * 1000; // 24 hours
setIsChecked(hasBeenPurchasedRecently);
} else {
setIsChecked(false);
Expand All @@ -22,8 +21,10 @@ export function ListItem({ name, dateLastPurchased, onCheck }) {
checkStatus();
}, [dateLastPurchased]);

const handleChecked = () => {
onCheck(id);
const handleDeleteButton = () => {
if (window.confirm(`Do you really want to delete ${name}?`)) {
onDelete();
}
};

return (
Expand All @@ -37,6 +38,7 @@ export function ListItem({ name, dateLastPurchased, onCheck }) {
/>
{name}
</label>
<button onClick={handleDeleteButton}>delete</button>
</li>
);
}
17 changes: 16 additions & 1 deletion src/views/List.jsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { ListItem } from '../components';
import { useState } from 'react';
import { updateItem } from '../api/firebase';
import { updateItem, deleteItem } from '../api/firebase';
import { Link } from 'react-router-dom';

export function List({ data, listPath, lists }) {
const [searchItem, setSearchItem] = useState('');
const [errorMsg, setErrorMsg] = useState('');

const handleSearch = (e) => {
e.preventDefault();
Expand All @@ -28,6 +29,16 @@ export function List({ data, listPath, lists }) {
});
};

const handleDelete = async (itemId) => {
try {
await deleteItem(listPath, itemId);
setErrorMsg('');
} catch (error) {
console.error(error.message, error);
setErrorMsg('Failed to delete the item. Please try again!');
Comment on lines +36 to +38
Copy link
Collaborator

@Amaka202 Amaka202 Sep 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great! well done team!!😊💥

}
};

return (
<>
<p>
Expand Down Expand Up @@ -76,11 +87,14 @@ export function List({ data, listPath, lists }) {
name={item.name}
dateLastPurchased={item.dateLastPurchased}
onCheck={() => handleCheck(item)}
onDelete={() => handleDelete(item.id)}
/>
))}
</ul>
)}

{errorMsg && <p>{errorMsg}</p>}

<ul>
{data.map((item) => (
<ListItem
Expand All @@ -89,6 +103,7 @@ export function List({ data, listPath, lists }) {
id={item.id}
dateLastPurchased={item.dateLastPurchased}
onCheck={() => handleCheck(item)}
onDelete={() => handleDelete(item.id)}
/>
))}
</ul>
Expand Down
Loading