import React, { useEffect, useState } from "react"; import TextInput from "@/components/TextInput"; import useCollectionStore from "@/store/collections"; import toast, { Toaster } from "react-hot-toast"; import { faFolder, faRightFromBracket, faTrashCan, } from "@fortawesome/free-solid-svg-icons"; import { HexColorPicker } from "react-colorful"; import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; import { CollectionIncludingMembersAndLinkCount } from "@/types/global"; import { useRouter } from "next/router"; import usePermissions from "@/hooks/usePermissions"; type Props = { modalId: string; isOpen: boolean; onClose: Function; activeCollection: CollectionIncludingMembersAndLinkCount; }; export default function DeleteCollectionModal({ modalId, isOpen, onClose, activeCollection, }: Props) { const modal = document.getElementById(modalId); const [collection, setCollection] = useState(activeCollection); useEffect(() => { modal?.scrollTo(0, 0); setCollection(activeCollection); setInputField(""); modal?.addEventListener("close", () => { onClose(); }); return () => { modal?.addEventListener("close", () => { onClose(); }); }; }, [isOpen]); const [submitLoader, setSubmitLoader] = useState(false); const { removeCollection } = useCollectionStore(); const router = useRouter(); const [inputField, setInputField] = useState(""); const permissions = usePermissions(collection.id as number); const submit = async () => { if (permissions === true) if (collection.name !== inputField) return null; if (!submitLoader) { setSubmitLoader(true); if (!collection) return null; setSubmitLoader(true); const load = toast.loading("Deleting..."); let response; response = await removeCollection(collection.id as any); toast.dismiss(load); if (response.ok) { toast.success(`Deleted.`); (document.getElementById(modalId) as any).close(); router.push("/collections"); } else toast.error(response.data as string); setSubmitLoader(false); } }; return (

{permissions === true ? "Delete" : "Leave"} Collection

{permissions === true ? ( <>

To confirm, type " {collection.name} " in the box below:

setInputField(e.target.value)} placeholder={`Type "${collection.name}" Here.`} className="w-3/4 mx-auto" />
Warning: Deleting this collection will permanently erase all its contents , and it will become inaccessible to everyone, including members with previous access.
) : (

Click the button below to leave the current collection.

)}
); }