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

74 lines
2.0 KiB
TypeScript
Raw Normal View History

2024-02-10 00:37:48 -06:00
import React from "react";
import useLinkStore from "@/store/links";
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";
import { useBulkDeleteLinks } from "@/hooks/store/links";
2024-08-14 14:22:28 -05:00
import toast from "react-hot-toast";
2024-02-10 00:37:48 -06:00
type Props = {
2024-02-10 18:34:25 -06:00
onClose: Function;
2024-02-10 00:37:48 -06:00
};
export default function BulkDeleteLinksModal({ onClose }: Props) {
2024-06-09 08:27:16 -05:00
const { t } = useTranslation();
const { selectedLinks, setSelectedLinks } = useLinkStore();
2024-02-10 00:37:48 -06:00
const deleteLinksById = useBulkDeleteLinks();
2024-02-10 00:37:48 -06:00
const deleteLink = async () => {
2024-08-14 14:22:28 -05:00
const load = toast.loading(t("deleting"));
await deleteLinksById.mutateAsync(
selectedLinks.map((link) => link.id as number),
{
2024-08-14 14:22:28 -05:00
onSettled: (data, error) => {
toast.dismiss(load);
if (error) {
toast.error(error.message);
} else {
setSelectedLinks([]);
onClose();
toast.success(t("deleted"));
}
},
}
2024-02-10 18:34:25 -06:00
);
};
2024-02-10 00:37:48 -06:00
2024-02-10 18:34:25 -06:00
return (
<Modal toggleModal={onClose}>
<p className="text-xl font-thin text-red-500">
2024-06-09 08:27:16 -05:00
{selectedLinks.length === 1
? t("delete_link")
: t("delete_links", { count: selectedLinks.length })}
2024-02-10 18:34:25 -06:00
</p>
2024-02-10 00:37:48 -06:00
2024-02-10 18:34:25 -06:00
<div className="divider mb-3 mt-1"></div>
2024-02-10 00:37:48 -06:00
2024-02-10 18:34:25 -06:00
<div className="flex flex-col gap-3">
2024-06-09 08:27:16 -05:00
<p>
{selectedLinks.length === 1
? t("link_deletion_confirmation_message")
: t("links_deletion_confirmation_message", {
count: selectedLinks.length,
})}
</p>
2024-02-10 00:37:48 -06:00
2024-02-10 18:34:25 -06:00
<div role="alert" className="alert alert-warning">
<i className="bi-exclamation-triangle text-xl" />
2024-06-09 08:27:16 -05:00
<span>{t("warning_irreversible")}</span>
2024-02-10 18:34:25 -06:00
</div>
2024-02-10 00:37:48 -06:00
2024-06-09 08:27:16 -05:00
<p>{t("shift_key_tip")}</p>
2024-02-10 00:37:48 -06:00
2024-05-24 18:13:04 -05:00
<Button className="ml-auto" intent="destructive" onClick={deleteLink}>
2024-02-10 18:34:25 -06:00
<i className="bi-trash text-xl" />
2024-06-09 08:27:16 -05:00
{t("delete")}
2024-05-24 18:13:04 -05:00
</Button>
2024-02-10 18:34:25 -06:00
</div>
</Modal>
);
2024-02-10 00:37:48 -06:00
}