el.xwx.moe/components/ModalContent/DeleteCollectionModal.tsx

113 lines
3.3 KiB
TypeScript
Raw Normal View History

2023-12-01 13:00:52 -06:00
import React, { useEffect, useState } from "react";
import TextInput from "@/components/TextInput";
import useCollectionStore from "@/store/collections";
2023-12-01 16:42:45 -06:00
import toast from "react-hot-toast";
2023-12-01 13:00:52 -06:00
import { CollectionIncludingMembersAndLinkCount } from "@/types/global";
import { useRouter } from "next/router";
import usePermissions from "@/hooks/usePermissions";
2023-12-01 16:42:45 -06:00
import Modal from "../Modal";
2024-05-24 18:13:04 -05:00
import Button from "../ui/Button";
2023-12-01 13:00:52 -06:00
type Props = {
onClose: Function;
activeCollection: CollectionIncludingMembersAndLinkCount;
};
export default function DeleteCollectionModal({
onClose,
activeCollection,
}: Props) {
const [collection, setCollection] =
useState<CollectionIncludingMembersAndLinkCount>(activeCollection);
2023-12-01 13:00:52 -06:00
useEffect(() => {
setCollection(activeCollection);
2023-12-01 16:42:45 -06:00
}, []);
2023-12-01 13:00:52 -06:00
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.`);
2023-12-01 16:42:45 -06:00
onClose();
2023-12-01 13:00:52 -06:00
router.push("/collections");
} else toast.error(response.data as string);
setSubmitLoader(false);
}
};
return (
2023-12-01 16:42:45 -06:00
<Modal toggleModal={onClose}>
<p className="text-xl font-thin text-red-500">
2023-12-01 16:42:45 -06:00
{permissions === true ? "Delete" : "Leave"} Collection
</p>
2023-12-05 14:17:36 -06:00
<div className="divider mb-3 mt-1"></div>
2023-12-01 16:42:45 -06:00
<div className="flex flex-col gap-3">
{permissions === true ? (
<>
<div className="flex flex-col gap-3">
<p>
To confirm, type &quot;
<span className="font-bold">{collection.name}</span>
&quot; in the box below:
</p>
<TextInput
value={inputField}
onChange={(e) => setInputField(e.target.value)}
placeholder={`Type "${collection.name}" Here.`}
className="w-3/4 mx-auto"
/>
</div>
<div role="alert" className="alert alert-warning">
2023-12-17 02:46:21 -06:00
<i className="bi-exclamation-triangle text-xl"></i>
2023-12-01 16:42:45 -06:00
<span>
2023-12-13 05:59:36 -06:00
<b>Warning:</b> Deleting this collection will permanently erase
all its contents, and it will become inaccessible to everyone,
including members with previous access.
2023-12-01 16:42:45 -06:00
</span>
</div>
</>
) : (
<p>Click the button below to leave the current collection.</p>
)}
2024-05-24 18:13:04 -05:00
<Button
2023-12-01 16:42:45 -06:00
disabled={permissions === true && inputField !== collection.name}
onClick={submit}
2024-05-24 18:13:04 -05:00
intent="destructive"
className="ml-auto"
2023-12-01 16:42:45 -06:00
>
2023-12-17 02:46:21 -06:00
<i className="bi-trash text-xl"></i>
2023-12-01 16:42:45 -06:00
{permissions === true ? "Delete" : "Leave"} Collection
2024-05-24 18:13:04 -05:00
</Button>
2023-12-01 13:00:52 -06:00
</div>
2023-12-01 16:42:45 -06:00
</Modal>
2023-12-01 13:00:52 -06:00
);
}