2023-12-02 03:42:51 -06:00
|
|
|
import React, { useEffect, useState } from "react";
|
|
|
|
import { Toaster } from "react-hot-toast";
|
|
|
|
import CollectionSelection from "@/components/InputSelect/CollectionSelection";
|
|
|
|
import TagSelection from "@/components/InputSelect/TagSelection";
|
|
|
|
import TextInput from "@/components/TextInput";
|
|
|
|
import unescapeString from "@/lib/client/unescapeString";
|
|
|
|
import useLinkStore from "@/store/links";
|
|
|
|
import { LinkIncludingShortenedCollectionAndTags } from "@/types/global";
|
|
|
|
import toast from "react-hot-toast";
|
|
|
|
import Link from "next/link";
|
|
|
|
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
|
|
|
|
import { faLink, faTrashCan } from "@fortawesome/free-solid-svg-icons";
|
|
|
|
import Modal from "../Modal";
|
2023-12-06 23:33:05 -06:00
|
|
|
import { useRouter } from "next/router";
|
2023-12-02 03:42:51 -06:00
|
|
|
|
|
|
|
type Props = {
|
|
|
|
onClose: Function;
|
|
|
|
activeLink: LinkIncludingShortenedCollectionAndTags;
|
|
|
|
};
|
|
|
|
|
|
|
|
export default function DeleteLinkModal({ onClose, activeLink }: Props) {
|
|
|
|
const [link, setLink] =
|
|
|
|
useState<LinkIncludingShortenedCollectionAndTags>(activeLink);
|
|
|
|
|
|
|
|
const { removeLink } = useLinkStore();
|
|
|
|
const [submitLoader, setSubmitLoader] = useState(false);
|
|
|
|
|
2023-12-06 23:33:05 -06:00
|
|
|
const router = useRouter();
|
|
|
|
|
2023-12-02 03:42:51 -06:00
|
|
|
useEffect(() => {
|
|
|
|
setLink(activeLink);
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
const deleteLink = async () => {
|
|
|
|
const load = toast.loading("Deleting...");
|
|
|
|
|
|
|
|
const response = await removeLink(link.id as number);
|
|
|
|
|
|
|
|
toast.dismiss(load);
|
|
|
|
|
|
|
|
response.ok && toast.success(`Link Deleted.`);
|
|
|
|
|
2023-12-06 23:33:05 -06:00
|
|
|
if (router.pathname.startsWith("/links/[id]")) {
|
|
|
|
router.push("/dashboard");
|
|
|
|
}
|
|
|
|
|
2023-12-02 03:42:51 -06:00
|
|
|
onClose();
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Modal toggleModal={onClose}>
|
2023-12-03 22:52:32 -06:00
|
|
|
<p className="text-xl font-thin text-red-500">Delete Link</p>
|
|
|
|
|
2023-12-05 14:17:36 -06:00
|
|
|
<div className="divider mb-3 mt-1"></div>
|
2023-12-03 22:52:32 -06:00
|
|
|
|
2023-12-02 03:42:51 -06:00
|
|
|
<div className="flex flex-col gap-3">
|
|
|
|
<p>Are you sure you want to delete this Link?</p>
|
|
|
|
|
|
|
|
<div role="alert" className="alert alert-warning">
|
|
|
|
<svg
|
|
|
|
xmlns="http://www.w3.org/2000/svg"
|
|
|
|
className="stroke-current shrink-0 h-6 w-6"
|
|
|
|
fill="none"
|
|
|
|
viewBox="0 0 24 24"
|
|
|
|
>
|
|
|
|
<path
|
|
|
|
strokeLinecap="round"
|
|
|
|
strokeLinejoin="round"
|
|
|
|
strokeWidth="2"
|
|
|
|
d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-3L13.732 4c-.77-1.333-2.694-1.333-3.464 0L3.34 16c-.77 1.333.192 3 1.732 3z"
|
|
|
|
/>
|
|
|
|
</svg>
|
|
|
|
<span>Warning: This action is irreversible!</span>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<p>
|
|
|
|
Hold the <kbd className="kbd kbd-sm">Shift</kbd> key while clicking
|
2023-12-08 10:01:47 -06:00
|
|
|
'Delete' to bypass this confirmation in the future.
|
2023-12-02 03:42:51 -06:00
|
|
|
</p>
|
|
|
|
|
|
|
|
<button
|
|
|
|
className={`ml-auto btn w-fit text-white flex items-center gap-2 duration-100 bg-red-500 hover:bg-red-400 hover:dark:bg-red-600 cursor-pointer`}
|
|
|
|
onClick={deleteLink}
|
|
|
|
>
|
|
|
|
<FontAwesomeIcon icon={faTrashCan} className="h-5" />
|
|
|
|
Delete
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
</Modal>
|
|
|
|
);
|
|
|
|
}
|