diff --git a/components/Checkbox.tsx b/components/Checkbox.tsx index 3b7cd7e..5c95bbe 100644 --- a/components/Checkbox.tsx +++ b/components/Checkbox.tsx @@ -12,23 +12,17 @@ type Props = { export default function Checkbox({ label, state, className, onClick }: Props) { return ( ); } diff --git a/components/CollectionCard.tsx b/components/CollectionCard.tsx index 955bd30..8bb0f94 100644 --- a/components/CollectionCard.tsx +++ b/components/CollectionCard.tsx @@ -12,6 +12,7 @@ import getPublicUserData from "@/lib/client/getPublicUserData"; import useAccountStore from "@/store/account"; import EditCollectionModal from "./Modals/EditCollectionModal"; import EditCollectionSharingModal from "./Modals/EditCollectionSharingModal"; +import DeleteCollectionModal from "./Modals/DeleteCollectionModal"; type Props = { collection: CollectionIncludingMembersAndLinkCount; @@ -62,6 +63,7 @@ export default function CollectionCard({ collection, className }: Props) { const [editCollectionModal, setEditCollectionModal] = useState(false); const [editCollectionSharingModal, setEditCollectionSharingModal] = useState(false); + const [deleteCollectionModal, setDeleteCollectionModal] = useState(false); return (
@@ -109,15 +111,7 @@ export default function CollectionCard({ collection, className }: Props) { tabIndex={0} onClick={() => { (document?.activeElement as HTMLElement)?.blur(); - collection && - setModal({ - modal: "COLLECTION", - state: true, - method: "UPDATE", - isOwner: permissions === true, - active: collection, - defaultIndex: permissions === true ? 2 : 1, - }); + setDeleteCollectionModal(true); }} > {permissions === true ? "Delete Collection" : "Leave Collection"} @@ -132,7 +126,7 @@ export default function CollectionCard({ collection, className }: Props) { {collectionOwner.id ? ( ) : undefined} {collection.members @@ -212,6 +206,12 @@ export default function CollectionCard({ collection, className }: Props) { modalId={"edit-collection-sharing-modal" + collection.id} activeCollection={collection} /> + setDeleteCollectionModal(false)} + modalId={"delete-collection-modal" + collection.id} + activeCollection={collection} + />
); } diff --git a/components/Modals/DeleteCollectionModal.tsx b/components/Modals/DeleteCollectionModal.tsx new file mode 100644 index 0000000..70b6d7b --- /dev/null +++ b/components/Modals/DeleteCollectionModal.tsx @@ -0,0 +1,180 @@ +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); + + useEffect(() => { + modal?.addEventListener("close", () => { + onClose(); + }); + + return () => { + modal?.addEventListener("close", () => { + onClose(); + }); + }; + }, [isOpen]); + + const [collection, setCollection] = + useState(activeCollection); + + useEffect(() => { + setCollection(activeCollection); + setInputField(""); + }, [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.

+ )} + + +
+
+
+ +
+
+ ); +} diff --git a/components/Modals/EditCollectionModal.tsx b/components/Modals/EditCollectionModal.tsx index 37f5fe9..ce1456c 100644 --- a/components/Modals/EditCollectionModal.tsx +++ b/components/Modals/EditCollectionModal.tsx @@ -4,7 +4,6 @@ import useCollectionStore from "@/store/collections"; import toast, { Toaster } from "react-hot-toast"; import { faFolder } from "@fortawesome/free-solid-svg-icons"; import { HexColorPicker } from "react-colorful"; -import { Collection } from "@prisma/client"; import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; import { CollectionIncludingMembersAndLinkCount } from "@/types/global"; diff --git a/components/Modals/EditCollectionSharingModal.tsx b/components/Modals/EditCollectionSharingModal.tsx index 88cb335..a8271a0 100644 --- a/components/Modals/EditCollectionSharingModal.tsx +++ b/components/Modals/EditCollectionSharingModal.tsx @@ -5,11 +5,8 @@ import toast, { Toaster } from "react-hot-toast"; import { faClose, faCrown, - faFolder, faUserPlus, } from "@fortawesome/free-solid-svg-icons"; -import { HexColorPicker } from "react-colorful"; -import { Collection } from "@prisma/client"; import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; import { CollectionIncludingMembersAndLinkCount, Member } from "@/types/global"; import getPublicUserData from "@/lib/client/getPublicUserData"; diff --git a/components/Navbar.tsx b/components/Navbar.tsx index 78286b7..b710b28 100644 --- a/components/Navbar.tsx +++ b/components/Navbar.tsx @@ -110,7 +110,6 @@ export default function Navbar() {