el.xwx.moe/components/Modal/Collection/DeleteCollection.tsx

125 lines
4.3 KiB
TypeScript
Raw Normal View History

import React, { useState } from "react";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import {
faRightFromBracket,
faTrashCan,
} from "@fortawesome/free-solid-svg-icons";
2023-06-16 07:55:21 -05:00
import { CollectionIncludingMembersAndLinkCount } from "@/types/global";
import useCollectionStore from "@/store/collections";
import { useRouter } from "next/router";
import usePermissions from "@/hooks/usePermissions";
import { toast } from "react-hot-toast";
2023-08-17 15:05:44 -05:00
import TextInput from "@/components/TextInput";
type Props = {
2023-05-18 13:02:17 -05:00
toggleDeleteCollectionModal: Function;
2023-06-16 07:55:21 -05:00
collection: CollectionIncludingMembersAndLinkCount;
};
2023-05-26 23:29:45 -05:00
export default function DeleteCollection({
2023-05-18 13:02:17 -05:00
toggleDeleteCollectionModal,
collection,
}: Props) {
const [inputField, setInputField] = useState("");
const { removeCollection } = useCollectionStore();
const router = useRouter();
const submit = async () => {
if (permissions === true) if (collection.name !== inputField) return null;
2023-05-26 23:11:29 -05:00
const load = toast.loading("Deleting...");
const response = await removeCollection(collection.id as number);
toast.dismiss(load);
if (response.ok) {
toast.success("Collection Deleted.");
2023-05-18 13:02:17 -05:00
toggleDeleteCollectionModal();
router.push("/collections");
}
};
const permissions = usePermissions(collection.id as number);
return (
<div className="flex flex-col gap-3 justify-between sm:w-[35rem] w-80">
{permissions === true ? (
<>
<p className="text-red-500 font-bold text-center">Warning!</p>
<div className="max-h-[20rem] overflow-y-auto">
2023-08-22 17:34:46 -05:00
<div className="text-black dark:text-white">
<p>
Please note that deleting the collection will permanently remove
all its contents, including the following:
</p>
<div className="p-3">
<li className="list-inside">
Links: All links within the collection will be permanently
deleted.
</li>
<li className="list-inside">
Tags: All tags associated with the collection will be removed.
</li>
<li className="list-inside">
Screenshots/PDFs: Any screenshots or PDFs attached to links
within this collection will be permanently deleted.
</li>
<li className="list-inside">
Members: Any members who have been granted access to the
collection will lose their permissions and no longer be able
to view or interact with the content.
</li>
</div>
<p>
Please double-check that you have backed up any essential data
and have informed the relevant members about this action.
</p>
</div>
</div>
<div className="flex flex-col gap-3">
<p className="text-black dark:text-white text-center">
To confirm, type &quot;
2023-08-14 22:25:25 -05:00
<span className="font-bold">{collection.name}</span>
&quot; in the box below:
</p>
2023-08-17 15:05:44 -05:00
<TextInput
autoFocus={true}
value={inputField}
onChange={(e) => setInputField(e.target.value)}
placeholder={`Type "${collection.name}" Here.`}
2023-08-17 15:05:44 -05:00
className="w-3/4 mx-auto"
/>
</div>
</>
) : (
2023-08-22 17:34:46 -05:00
<p className="text-black dark:text-white">
2023-08-30 23:17:27 -05:00
Click the button below to leave the current collection.
</p>
)}
<div
2023-06-24 16:54:35 -05:00
className={`mx-auto mt-2 text-white flex items-center gap-2 py-2 px-5 rounded-md select-none font-bold duration-100 ${
permissions === true
? inputField === collection.name
2023-08-14 22:25:25 -05:00
? "bg-red-500 hover:bg-red-400 hover:dark:bg-red-600 cursor-pointer"
: "cursor-not-allowed bg-red-300 dark:bg-red-900"
: "bg-red-500 hover:bg-red-400 hover:dark:bg-red-600 cursor-pointer"
}`}
onClick={submit}
>
<FontAwesomeIcon
icon={permissions === true ? faTrashCan : faRightFromBracket}
className="h-5"
/>
{permissions === true ? "Delete" : "Leave"} Collection
</div>
</div>
);
}