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

108 lines
3.1 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 { 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";
2024-06-09 08:27:16 -05:00
import { useTranslation } from "next-i18next";
2024-07-30 13:57:09 -05:00
import { useDeleteCollection } from "@/hooks/store/collections";
2024-08-14 14:22:28 -05:00
import toast from "react-hot-toast";
2023-12-01 13:00:52 -06:00
type Props = {
onClose: Function;
activeCollection: CollectionIncludingMembersAndLinkCount;
};
export default function DeleteCollectionModal({
onClose,
activeCollection,
}: Props) {
2024-06-09 08:27:16 -05:00
const { t } = useTranslation();
const [collection, setCollection] =
useState<CollectionIncludingMembersAndLinkCount>(activeCollection);
2023-12-01 13:00:52 -06:00
const [submitLoader, setSubmitLoader] = useState(false);
const router = useRouter();
const [inputField, setInputField] = useState("");
const permissions = usePermissions(collection.id as number);
2024-06-09 08:27:16 -05:00
useEffect(() => {
setCollection(activeCollection);
}, []);
2023-12-01 13:00:52 -06:00
2024-07-30 13:57:09 -05:00
const deleteCollection = useDeleteCollection();
2024-06-09 08:27:16 -05:00
const submit = async () => {
if (permissions === true && collection.name !== inputField) return;
2023-12-01 13:00:52 -06:00
if (!submitLoader) {
setSubmitLoader(true);
if (!collection) return null;
setSubmitLoader(true);
2024-08-14 14:22:28 -05:00
const load = toast.loading(t("deleting_collection"));
2024-07-30 13:57:09 -05:00
deleteCollection.mutateAsync(collection.id as number, {
2024-08-14 14:22:28 -05:00
onSettled: (data, error) => {
2024-10-26 09:58:27 -05:00
setSubmitLoader(false);
2024-08-14 14:22:28 -05:00
toast.dismiss(load);
if (error) {
toast.error(error.message);
} else {
onClose();
toast.success(t("deleted"));
2024-08-14 15:44:07 -05:00
router.push("/collections");
2024-08-14 14:22:28 -05:00
}
2024-07-30 13:57:09 -05:00
},
});
2023-12-01 13:00:52 -06:00
}
};
return (
2023-12-01 16:42:45 -06:00
<Modal toggleModal={onClose}>
<p className="text-xl font-thin text-red-500">
2024-06-09 08:27:16 -05:00
{permissions === true ? t("delete_collection") : t("leave_collection")}
2023-12-01 16:42:45 -06:00
</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 ? (
<>
2024-06-09 08:27:16 -05:00
<p>{t("confirm_deletion_prompt", { name: collection.name })}</p>
<TextInput
value={inputField}
onChange={(e) => setInputField(e.target.value)}
placeholder={t("type_name_placeholder", {
name: collection.name,
})}
className="w-3/4 mx-auto"
/>
2023-12-01 16:42:45 -06:00
<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>
2024-06-09 08:27:16 -05:00
<b>{t("warning")}: </b>
{t("deletion_warning")}
2023-12-01 16:42:45 -06:00
</span>
</div>
</>
) : (
2024-06-09 08:27:16 -05:00
<p>{t("leave_prompt")}</p>
2023-12-01 16:42:45 -06:00
)}
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>
2024-06-09 08:27:16 -05:00
{permissions === true ? t("delete") : t("leave")}
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
);
}