2024-12-04 18:35:09 +00:00
|
|
|
|
import React, { useContext, useState } from "react";
|
2023-10-10 22:00:45 +00:00
|
|
|
|
|
|
|
|
|
|
import scriptAPI from "services/entities/scripts";
|
|
|
|
|
|
import { NotificationContext } from "context/notification";
|
2023-03-06 15:03:48 +00:00
|
|
|
|
|
|
|
|
|
|
import Modal from "components/Modal";
|
|
|
|
|
|
import Button from "components/buttons/Button";
|
2024-10-04 01:06:20 +00:00
|
|
|
|
import { AxiosResponse } from "axios";
|
|
|
|
|
|
import { IApiError } from "../../../../../interfaces/errors";
|
|
|
|
|
|
import { getErrorMessage } from "../ScriptUploader/helpers";
|
2023-03-06 15:03:48 +00:00
|
|
|
|
|
|
|
|
|
|
const baseClass = "delete-script-modal";
|
|
|
|
|
|
|
|
|
|
|
|
interface IDeleteScriptModalProps {
|
|
|
|
|
|
scriptName: string;
|
|
|
|
|
|
scriptId: number;
|
|
|
|
|
|
onCancel: () => void;
|
2023-10-10 22:00:45 +00:00
|
|
|
|
onDone: () => void;
|
2024-11-06 17:48:11 +00:00
|
|
|
|
isHidden?: boolean;
|
2023-03-06 15:03:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const DeleteScriptModal = ({
|
|
|
|
|
|
scriptName,
|
|
|
|
|
|
scriptId,
|
|
|
|
|
|
onCancel,
|
2023-10-10 22:00:45 +00:00
|
|
|
|
onDone,
|
2024-11-06 17:48:11 +00:00
|
|
|
|
isHidden = false,
|
2023-03-06 15:03:48 +00:00
|
|
|
|
}: IDeleteScriptModalProps) => {
|
2023-10-10 22:00:45 +00:00
|
|
|
|
const { renderFlash } = useContext(NotificationContext);
|
2024-12-04 18:35:09 +00:00
|
|
|
|
const [isDeleting, setIsDeleting] = useState(false);
|
2023-10-10 22:00:45 +00:00
|
|
|
|
|
|
|
|
|
|
const onClickDelete = async (id: number) => {
|
2024-12-04 18:35:09 +00:00
|
|
|
|
setIsDeleting(true);
|
2023-10-10 22:00:45 +00:00
|
|
|
|
try {
|
|
|
|
|
|
await scriptAPI.deleteScript(id);
|
|
|
|
|
|
renderFlash("success", "Successfully deleted!");
|
2024-10-04 01:06:20 +00:00
|
|
|
|
} catch (e) {
|
|
|
|
|
|
const error = e as AxiosResponse<IApiError>;
|
|
|
|
|
|
const apiErrMessage = getErrorMessage(error);
|
|
|
|
|
|
renderFlash(
|
|
|
|
|
|
"error",
|
|
|
|
|
|
apiErrMessage.includes("Policy automation")
|
|
|
|
|
|
? apiErrMessage
|
|
|
|
|
|
: "Couldn’t delete. Please try again."
|
|
|
|
|
|
);
|
2023-10-10 22:00:45 +00:00
|
|
|
|
}
|
2024-12-04 18:35:09 +00:00
|
|
|
|
setIsDeleting(false);
|
2023-10-10 22:00:45 +00:00
|
|
|
|
onDone();
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2023-03-06 15:03:48 +00:00
|
|
|
|
return (
|
|
|
|
|
|
<Modal
|
|
|
|
|
|
className={baseClass}
|
2024-02-23 14:57:18 +00:00
|
|
|
|
title="Delete script"
|
2023-03-06 15:03:48 +00:00
|
|
|
|
onExit={onCancel}
|
2023-10-10 22:00:45 +00:00
|
|
|
|
onEnter={() => onClickDelete(scriptId)}
|
2024-11-06 17:48:11 +00:00
|
|
|
|
isHidden={isHidden}
|
2024-12-04 18:35:09 +00:00
|
|
|
|
isContentDisabled={isDeleting}
|
2023-03-06 15:03:48 +00:00
|
|
|
|
>
|
|
|
|
|
|
<>
|
|
|
|
|
|
<p>
|
2025-09-24 17:49:50 +00:00
|
|
|
|
This action will cancel any pending script execution for{" "}
|
|
|
|
|
|
<span className={`${baseClass}__script-name`}>{scriptName}</span>
|
2023-03-06 15:03:48 +00:00
|
|
|
|
</p>
|
2025-09-24 17:49:50 +00:00
|
|
|
|
<p>
|
|
|
|
|
|
If the script is currently running on a host it will still complete,
|
|
|
|
|
|
but results won't appear in Fleet.
|
|
|
|
|
|
</p>
|
|
|
|
|
|
<p>You cannot undo this action.</p>
|
2023-03-06 15:03:48 +00:00
|
|
|
|
<div className="modal-cta-wrap">
|
|
|
|
|
|
<Button
|
|
|
|
|
|
type="button"
|
2023-10-10 22:00:45 +00:00
|
|
|
|
onClick={() => onClickDelete(scriptId)}
|
2023-03-06 15:03:48 +00:00
|
|
|
|
variant="alert"
|
|
|
|
|
|
className="delete-loading"
|
2024-12-04 18:35:09 +00:00
|
|
|
|
isLoading={isDeleting}
|
2023-03-06 15:03:48 +00:00
|
|
|
|
>
|
|
|
|
|
|
Delete
|
|
|
|
|
|
</Button>
|
|
|
|
|
|
<Button onClick={onCancel} variant="inverse-alert">
|
|
|
|
|
|
Cancel
|
|
|
|
|
|
</Button>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</>
|
|
|
|
|
|
</Modal>
|
|
|
|
|
|
);
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
export default DeleteScriptModal;
|