Skip to content

Commit

Permalink
fill deleteItem function in api/firebase.js and add "Delete item" but…
Browse files Browse the repository at this point in the history
…ton in ListItem.jsx
  • Loading branch information
vivitt committed Mar 11, 2024
1 parent 3e1607b commit 9200fa5
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 17 deletions.
24 changes: 10 additions & 14 deletions src/api/firebase.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
import {
addDoc,
arrayUnion,
getDoc,
setDoc,
collection,
deleteDoc,
doc,
getDoc,
onSnapshot,
setDoc,
updateDoc,
addDoc,
} from 'firebase/firestore';
import { useEffect, useState } from 'react';
import { db } from './config';
import {
getFutureDate,
getDaysBetweenDates,
isMoreThanADayAgo,
} from '../utils';
import { getDaysBetweenDates, getFutureDate } from '../utils';
import { calculateEstimate } from '@the-collab-lab/shopping-list-utils';

/**
Expand Down Expand Up @@ -215,10 +212,9 @@ export async function updateItem(listPath, itemId) {
return itemDocumentRef;
}

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 listCollectionRef = collection(db, listPath, 'items');
const itemDocumentRef = doc(listCollectionRef, itemId);

await deleteDoc(itemDocumentRef);
}
2 changes: 2 additions & 0 deletions src/components/ListItem.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import './ListItem.css';
import { deleteItem } from '../api/firebase';

export function ListItem({
isRecentlyPurchased,
Expand Down Expand Up @@ -27,6 +28,7 @@ export function ListItem({
className="ListItem__label"
>{`Mark ${name} as purchased`}</label>
</div>
<button onClick={() => deleteItem(listPath, itemId)}>Delete item</button>
</li>
);
}
2 changes: 1 addition & 1 deletion src/utils/inputValidation.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export const inputHasOnlyNUmbers = (string) => {
return !!string.match(/^\d+$/);
};

export const inputHasRepeatedValue = (string, value) => {
export const stringsHaveSameValue = (string, value) => {
const regex = /[^a-z]/g;
const stringOne = string.toLowerCase().trim().replace(regex, '');
const stringTwo = value.toLowerCase().trim().replace(regex, '');
Expand Down
4 changes: 2 additions & 2 deletions src/views/ManageList.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import ErrorMessage from '../components/ErrorMessage';
import {
inputHasValue,
inputHasOnlyNUmbers,
inputHasRepeatedValue,
stringsHaveSameValue,
} from '../utils/inputValidation';

export function ManageList({ data, listPath, userId, userEmail }) {
Expand All @@ -28,7 +28,7 @@ export function ManageList({ data, listPath, userId, userEmail }) {
return;
}

if (data.some((item) => inputHasRepeatedValue(item.name, itemName))) {
if (data.some((item) => stringsHaveSameValue(item.name, itemName))) {
setAddItemErrMessage('This item is already in your list');
form.reset();
return;
Expand Down

0 comments on commit 9200fa5

Please sign in to comment.