diff --git a/src/api/firebase.js b/src/api/firebase.js index 4581091..bfc0ecc 100644 --- a/src/api/firebase.js +++ b/src/api/firebase.js @@ -8,6 +8,7 @@ import { updateDoc, addDoc, deleteDoc, + arrayRemove, } from 'firebase/firestore'; import { useEffect, useState } from 'react'; import { db } from './config'; @@ -240,6 +241,91 @@ export async function deleteItem(listPath, id) { } } +export async function deleteList(collectionId, document, email) { + const listRef = doc(db, collectionId, document); + const userRef = doc(db, 'users', email); + + try { + //check if list exists for debugging purposes + const docSnapshot = await getDoc(listRef); + if (!docSnapshot.exists()) { + console.log('Document does not exist:', listRef.path); + return; + } else { + console.log('Document exists:', listRef.path); + } + //actually delete the list + await deleteDoc(listRef); + console.log('Document deleted:', listRef.path); + + //check if user list exists for debugging purposes + const docSnapshot2 = await getDoc(userRef); + if (!docSnapshot2.exists()) { + console.log('Document does not exist:', userRef.path); + return; + } else { + console.log('Document exists:', userRef.path); + } + //alert if user not found + const userDoc = await getDoc(userRef); + if (!userDoc.exists()) { + console.log('User document not found:', email); + return; + } + //show sharedLists contents before deleting ref + const sharedLists = userDoc.data().sharedLists; + console.log('Current sharedLists:', typeof sharedLists, sharedLists); + //actually delete ref from array + await updateDoc(userRef, { + sharedLists: arrayRemove(listRef), + }); + console.log('User document updated'); + } catch (error) { + console.error('Error deleting your list', error); + } +} + +// export async function deleteCollection(listPath) { +// const collectionRef = collection(db, listPath); +// const query = collectionRef.orderBy('__name__').limit(500); +// console.log('delete collecton triggered'); + +// return new Promise((resolve, reject) => { +// console.log('inside promise'); + +// deleteQueryBatch(db, query, resolve).catch(reject); +// }); +// } + +// async function deleteQueryBatch(db, query, resolve) { +// const snapshot = await query.get(); +// console.log('deletequery triggered'); + +// const batchSize = snapshot.size; +// if (batchSize === 0) { +// // When there are no documents left, we are done +// console.log('batch size 0'); + +// resolve(); +// return; +// } + +// // Delete documents in a batch +// const batch = db.batch(); +// snapshot.docs.forEach((doc) => { +// console.log('deleted ', doc.ref); + +// batch.delete(doc.ref); +// }); +// await batch.commit(); + +// // Recurse on the next process tick, to avoid +// // exploding the stack. +// process.nextTick(() => { +// deleteQueryBatch(db, query, resolve); +// }); +// } + export function comparePurchaseUrgency(arr) { const groupedItems = { Overdue: [], diff --git a/src/components/SingleList.jsx b/src/components/SingleList.jsx index 135eb3b..6c80d2a 100644 --- a/src/components/SingleList.jsx +++ b/src/components/SingleList.jsx @@ -1,13 +1,46 @@ +import { deleteList } from '@/api'; +import { getAuth } from 'firebase/auth'; +import { useEffect, useState } from 'react'; import { FaShareNodes } from 'react-icons/fa6'; -import { Trash2 } from 'lucide-react'; import { useNavigate } from 'react-router-dom'; +import toast from 'react-hot-toast'; +import { + AlertDialog, + AlertDialogAction, + AlertDialogCancel, + AlertDialogContent, + AlertDialogDescription, + AlertDialogFooter, + AlertDialogHeader, + AlertDialogTitle, + AlertDialogTrigger, +} from './ui/alert-dialog'; +import { Button } from './ui/button'; +import { Trash2 } from 'lucide-react'; +import { + Tooltip, + TooltipContent, + TooltipProvider, + TooltipTrigger, +} from '@/components/ui/tooltip'; + export function SingleList({ name, path, + listPath, setListPath, handleShareModalClick, setSelectedItem, }) { + const [isAlertOpen, setIsAlertOpen] = useState(false); + const [collectionId, setCollectionId] = useState(''); + const singleListPath = path.split('/')[0]; + const email = getAuth().currentUser.email; + + useEffect(() => { + setCollectionId(singleListPath); + }, []); + const navigate = useNavigate(); function handleClick() { @@ -20,6 +53,19 @@ export function SingleList({ handleShareModalClick(); }; + const handleDeleteClick = async (name) => { + await deleteList(collectionId, name, email); + console.log(listPath, name); + + if (listPath.includes(name)) { + console.log(); + + setListPath(''); + } + toast.success(`List ${name} was deleted`); + setIsAlertOpen(false); + }; + return (
You cannot delete a list you don't own!
+