2024-05-07 07:14:36 +00:00
|
|
|
import { useState, FC } from "react";
|
2023-11-15 10:26:57 +00:00
|
|
|
import { useRouter } from "next/router";
|
2023-11-27 06:44:06 +00:00
|
|
|
import { mutate } from "swr";
|
2024-05-07 07:14:36 +00:00
|
|
|
// types
|
2024-03-19 14:38:35 +00:00
|
|
|
import { IApiToken } from "@plane/types";
|
2023-11-27 06:44:06 +00:00
|
|
|
// ui
|
2024-05-07 07:14:36 +00:00
|
|
|
import { TOAST_TYPE, setToast } from "@plane/ui";
|
|
|
|
// components
|
|
|
|
import { AlertModalCore } from "@/components/core";
|
2023-11-27 06:44:06 +00:00
|
|
|
// fetch-keys
|
2024-05-07 07:14:36 +00:00
|
|
|
import { API_TOKENS_LIST } from "@/constants/fetch-keys";
|
|
|
|
// services
|
|
|
|
import { APITokenService } from "@/services/api_token.service";
|
2023-11-15 10:26:57 +00:00
|
|
|
|
|
|
|
type Props = {
|
|
|
|
isOpen: boolean;
|
2023-11-27 06:44:06 +00:00
|
|
|
onClose: () => void;
|
|
|
|
tokenId: string;
|
2023-11-15 10:26:57 +00:00
|
|
|
};
|
|
|
|
|
2023-11-23 09:39:46 +00:00
|
|
|
const apiTokenService = new APITokenService();
|
|
|
|
|
2023-11-27 06:44:06 +00:00
|
|
|
export const DeleteApiTokenModal: FC<Props> = (props) => {
|
|
|
|
const { isOpen, onClose, tokenId } = props;
|
2023-11-23 09:39:46 +00:00
|
|
|
// states
|
2023-11-15 10:26:57 +00:00
|
|
|
const [deleteLoading, setDeleteLoading] = useState<boolean>(false);
|
2023-11-23 09:39:46 +00:00
|
|
|
// router
|
2023-11-15 10:26:57 +00:00
|
|
|
const router = useRouter();
|
2023-11-27 06:44:06 +00:00
|
|
|
const { workspaceSlug } = router.query;
|
2023-11-15 10:26:57 +00:00
|
|
|
|
2023-11-27 06:44:06 +00:00
|
|
|
const handleClose = () => {
|
|
|
|
onClose();
|
|
|
|
setDeleteLoading(false);
|
|
|
|
};
|
2023-11-15 10:26:57 +00:00
|
|
|
|
2024-05-07 07:14:36 +00:00
|
|
|
const handleDeletion = async () => {
|
2023-11-27 06:44:06 +00:00
|
|
|
if (!workspaceSlug) return;
|
2023-11-15 10:26:57 +00:00
|
|
|
|
|
|
|
setDeleteLoading(true);
|
2023-11-27 06:44:06 +00:00
|
|
|
|
2024-05-07 07:14:36 +00:00
|
|
|
await apiTokenService
|
2023-11-27 06:44:06 +00:00
|
|
|
.deleteApiToken(workspaceSlug.toString(), tokenId)
|
2023-11-15 10:26:57 +00:00
|
|
|
.then(() => {
|
2024-03-06 08:48:41 +00:00
|
|
|
setToast({
|
|
|
|
type: TOAST_TYPE.SUCCESS,
|
2023-11-27 06:44:06 +00:00
|
|
|
title: "Success!",
|
|
|
|
message: "Token deleted successfully.",
|
2023-11-15 10:26:57 +00:00
|
|
|
});
|
2023-11-27 06:44:06 +00:00
|
|
|
|
|
|
|
mutate<IApiToken[]>(
|
|
|
|
API_TOKENS_LIST(workspaceSlug.toString()),
|
|
|
|
(prevData) => (prevData ?? []).filter((token) => token.id !== tokenId),
|
|
|
|
false
|
|
|
|
);
|
|
|
|
|
|
|
|
handleClose();
|
2023-11-15 10:26:57 +00:00
|
|
|
})
|
2023-11-27 06:44:06 +00:00
|
|
|
.catch((err) =>
|
2024-03-06 08:48:41 +00:00
|
|
|
setToast({
|
|
|
|
type: TOAST_TYPE.ERROR,
|
2024-05-07 09:33:24 +00:00
|
|
|
title: "Error!",
|
2023-11-27 06:44:06 +00:00
|
|
|
message: err?.message ?? "Something went wrong. Please try again.",
|
|
|
|
})
|
|
|
|
)
|
|
|
|
.finally(() => setDeleteLoading(false));
|
2023-11-15 10:26:57 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
2024-05-07 07:14:36 +00:00
|
|
|
<AlertModalCore
|
|
|
|
handleClose={handleClose}
|
|
|
|
handleSubmit={handleDeletion}
|
2024-05-31 12:10:21 +00:00
|
|
|
isSubmitting={deleteLoading}
|
2024-05-07 07:14:36 +00:00
|
|
|
isOpen={isOpen}
|
|
|
|
title="Delete API token"
|
|
|
|
content={
|
|
|
|
<>
|
|
|
|
Any application using this token will no longer have the access to Plane data. This action cannot be undone.
|
|
|
|
</>
|
|
|
|
}
|
|
|
|
/>
|
2023-11-15 10:26:57 +00:00
|
|
|
);
|
|
|
|
};
|