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

66 lines
1.8 KiB
TypeScript
Raw Normal View History

2024-02-10 00:37:48 -06:00
import React from "react";
import useLinkStore from "@/store/links";
import toast from "react-hot-toast";
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-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();
2024-02-10 18:34:25 -06:00
const { selectedLinks, setSelectedLinks, deleteLinksById } = useLinkStore();
2024-02-10 00:37:48 -06:00
2024-02-10 18:34:25 -06:00
const deleteLink = async () => {
2024-06-09 08:27:16 -05:00
const load = toast.loading(t("deleting"));
2024-02-10 00:37:48 -06:00
2024-02-10 18:34:25 -06:00
const response = await deleteLinksById(
selectedLinks.map((link) => link.id as number)
);
2024-02-10 00:37:48 -06:00
2024-02-10 18:34:25 -06:00
toast.dismiss(load);
2024-02-10 00:37:48 -06:00
2024-02-13 04:54:18 -06:00
if (response.ok) {
2024-06-09 08:27:16 -05:00
toast.success(t("deleted"));
2024-02-13 04:54:18 -06:00
setSelectedLinks([]);
onClose();
} else toast.error(response.data as string);
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
}