2024-01-24 14:48:40 -06:00
|
|
|
import React, { useEffect, useState } from "react";
|
2024-06-09 08:27:16 -05:00
|
|
|
import useTokenStore from "@/store/tokens";
|
2024-01-24 14:48:40 -06:00
|
|
|
import toast from "react-hot-toast";
|
|
|
|
import Modal from "../Modal";
|
2024-05-24 18:15:33 -05:00
|
|
|
import Button from "../ui/Button";
|
2024-06-09 08:27:16 -05:00
|
|
|
import { useTranslation } from "next-i18next";
|
|
|
|
import { AccessToken } from "@prisma/client";
|
2024-01-24 14:48:40 -06:00
|
|
|
|
|
|
|
type Props = {
|
|
|
|
onClose: Function;
|
|
|
|
activeToken: AccessToken;
|
|
|
|
};
|
|
|
|
|
|
|
|
export default function DeleteTokenModal({ onClose, activeToken }: Props) {
|
2024-06-09 08:27:16 -05:00
|
|
|
const { t } = useTranslation();
|
2024-01-24 14:48:40 -06:00
|
|
|
const [token, setToken] = useState<AccessToken>(activeToken);
|
|
|
|
|
|
|
|
const { revokeToken } = useTokenStore();
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
setToken(activeToken);
|
2024-06-09 08:27:16 -05:00
|
|
|
}, [activeToken]);
|
2024-01-24 14:48:40 -06:00
|
|
|
|
|
|
|
const deleteLink = async () => {
|
2024-06-09 08:27:16 -05:00
|
|
|
const load = toast.loading(t("deleting"));
|
2024-01-24 14:48:40 -06:00
|
|
|
|
|
|
|
const response = await revokeToken(token.id as number);
|
|
|
|
|
|
|
|
toast.dismiss(load);
|
|
|
|
|
2024-06-09 08:27:16 -05:00
|
|
|
if (response.ok) {
|
|
|
|
toast.success(t("token_revoked"));
|
2024-07-16 19:33:33 -05:00
|
|
|
} else {
|
|
|
|
toast.error(response.data as string);
|
2024-06-09 08:27:16 -05:00
|
|
|
}
|
2024-01-24 14:48:40 -06:00
|
|
|
|
|
|
|
onClose();
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Modal toggleModal={onClose}>
|
2024-06-09 08:27:16 -05:00
|
|
|
<p className="text-xl font-thin text-red-500">{t("revoke_token")}</p>
|
2024-01-24 14:48:40 -06:00
|
|
|
|
|
|
|
<div className="divider mb-3 mt-1"></div>
|
|
|
|
|
|
|
|
<div className="flex flex-col gap-3">
|
2024-06-09 08:27:16 -05:00
|
|
|
<p>{t("revoke_confirmation")}</p>
|
2024-01-24 14:48:40 -06:00
|
|
|
|
2024-05-24 18:15:33 -05:00
|
|
|
<Button className="ml-auto" intent="destructive" onClick={deleteLink}>
|
2024-01-24 14:48:40 -06:00
|
|
|
<i className="bi-trash text-xl" />
|
2024-06-09 08:27:16 -05:00
|
|
|
{t("revoke")}
|
2024-05-24 18:15:33 -05:00
|
|
|
</Button>
|
2024-01-24 14:48:40 -06:00
|
|
|
</div>
|
|
|
|
</Modal>
|
|
|
|
);
|
|
|
|
}
|