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

Delete button deletes item from list and firebase after user confirma… #49

Merged
merged 4 commits into from
Sep 19, 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
16 changes: 10 additions & 6 deletions 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 @@ -210,10 +211,13 @@ 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) {
// reference the item path
const itemDocRef = doc(db, listPath, 'items', itemId);
try {
// delete the item from the database
await deleteDoc(itemDocRef);
} catch (error) {
throw new Error(`Failed updating item: ${error.message}`);
}
}
11 changes: 4 additions & 7 deletions src/components/AddItems.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,10 @@ export function AddItems({ items }) {
type="text"
id="item-name"
placeholder="Enter item name"
pattern="^[^\s].+[^\s]$"
>
Item Name:
</TextInputElement>
<label htmlFor="item-name" required={true}>
Item Name:
</label>
label="Item Name:"
required={true}
/>

{Object.entries(daysUntilPurchaseOptions).map(([key, value]) => (
<RadioInputElement
key={key}
Expand Down
14 changes: 13 additions & 1 deletion src/components/ListItem.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useState } from 'react';
import './ListItem.css';
import { updateItem } from '../api';
import { updateItem, deleteItem } from '../api';
import { calculateDateNextPurchased, ONE_DAY_IN_MILLISECONDS } from '../utils';

const currentDate = new Date();
Expand Down Expand Up @@ -44,6 +44,17 @@ export function ListItem({ item, listPath }) {
}
};

const handleDeleteItem = async () => {
if (confirm(`Are you sure you want to delete this item?`)) {
try {
await deleteItem(listPath, id);
} catch (error) {
alert('Item was not deleted');
}
}
return;
};

return (
<li className="ListItem">
<input
Expand All @@ -53,6 +64,7 @@ export function ListItem({ item, listPath }) {
onChange={handleChange}
/>
<label htmlFor={`checkbox-${id}`}>{name}</label>
<button onClick={handleDeleteItem}>Delete Item</button>
</li>
);
}
1 change: 1 addition & 0 deletions src/components/TextInputElement.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const TextInputElement = ({
placeholder={placeholder}
onChange={onChange}
required={required}
pattern="^[^\s].+[^\s]$"
/>
<br />
</>
Expand Down
8 changes: 3 additions & 5 deletions src/views/List.jsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { ListItem } from '../components';
import { useState } from 'react';
import { useStateWithStorage } from '../utils';
import { AddItems } from '../components/AddItems';
import TextInputElement from '../components/TextInputElement';

export function List({ data }) {
export function List({ data, listPath }) {
const [searchItem, setSearchItem] = useState('');
const [listPath] = useStateWithStorage('tcl-shopping-list-path', null);

const handleTextChange = (event) => {
setSearchItem(event.target.value);
Expand All @@ -20,12 +18,12 @@ export function List({ data }) {

return (
<>
{!data.length ? (
{!data?.length ? (
<>
<p>Welcome to {listName}!</p>
<p>Ready to add your first item? Start adding below!</p>

<AddItems />
<AddItems items={data} />
</>
) : (
<>
Expand Down
4 changes: 2 additions & 2 deletions tests/List.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ beforeEach(() => {

describe('List Component', () => {
test('renders the shopping list name, search field, and all list items from the data prop', () => {
render(<List data={mockShoppingListData} />);
render(<List data={mockShoppingListData} listPath={'/groceries'} />);

expect(screen.getByText('groceries')).toBeInTheDocument();
expect(screen.getByLabelText('Search Item:')).toBeInTheDocument();
Expand All @@ -25,7 +25,7 @@ describe('List Component', () => {
});

test('shows welcome message and AddItems component when no items are present', () => {
render(<List data={[]} />);
render(<List data={[]} listPath={'/groceries'} />);

expect(screen.getByText('Welcome to groceries!')).toBeInTheDocument();
expect(screen.getByLabelText('Item Name:')).toBeInTheDocument();
Expand Down