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 (
  • - + {getAuth().currentUser.uid === singleListPath ? ( + + + + + + + + Are you absolutely sure? + + + This will permanently delete your list. Do you really want to + delete {name}? + + + + setIsAlertOpen(false)} + > + Cancel + + handleDeleteClick(name)} + > + Continue + + + + + ) : ( + + + + + + +

    You cannot delete a list you don't own!

    +
    +
    +
    + )}
  • ); diff --git a/src/views/Home.jsx b/src/views/Home.jsx index d56c3ae..c10ad70 100644 --- a/src/views/Home.jsx +++ b/src/views/Home.jsx @@ -11,6 +11,7 @@ export function Home({ handleShareModalClick, }) { const [selectedItem, setSelectedItem] = useState(''); + return (
    @@ -49,6 +50,7 @@ export function Home({ key={index} name={item.name} path={item.path} + listPath={listPath} setListPath={setListPath} handleShareModalClick={handleShareModalClick} setSelectedItem={setSelectedItem} diff --git a/src/views/List.jsx b/src/views/List.jsx index efa6b61..6cc98d1 100644 --- a/src/views/List.jsx +++ b/src/views/List.jsx @@ -46,99 +46,104 @@ export function List({ data, listPath, listName }) { }; return ( -
    -
    -
    -

    - - {listName} - -

    - {/* Did the conditional rendering of the underline like this cause we still need to hoist the darkMode */} - Description - Description + <> + {listPath === '' ? ( +
    +
    +

    + No list selected +

    + Description +
    -
    -
    - - - - - - - - Add New Item - - Fill in the details of the item you want to add. - - - +
    +
    + - - - -
    -
      - {displayData.map((item) => ( - - ))} -
    - {displayData.length === 0 && search.length > 0 && ( -
    -

    No items found. Try searching for a different item!

    -
    - )} - {data.length === 0 && ( -
    -

    - Your list is empty. Start adding some items now! -

    - + + + + + + + Add New Item + + Fill in the details of the item you want to add. + + + + + + +
    +
      + {displayData.map((item) => ( + + ))} +
    + {displayData.length === 0 && search.length > 0 && ( +
    +

    No items found. Try searching for a different item!

    +
    + )} + {data.length === 0 && ( +
    +

    + Your list is empty. Start adding some items now! +

    + +
    + )}
    )} -
    + ); }