fleet/frontend/pages/ManageControlsPage/Variables/components/DeleteCustomVariableModal/DeleteCustomVariableModal.tsx

91 lines
2.5 KiB
TypeScript
Raw Normal View History

import React, { useContext, useState } from "react";
import Modal from "components/Modal";
import Button from "components/buttons/Button";
import TooltipTruncatedText from "components/TooltipTruncatedText";
import { ISecret } from "interfaces/secrets";
import { NotificationContext } from "context/notification";
2025-08-22 14:22:37 +00:00
import formatErrorResponse from "utilities/format_error_response";
import secretsAPI from "services/entities/secrets";
interface DeleteCustomVariableModalProps {
secret: ISecret | undefined;
onExit: () => void;
Refactor paginated list to use UseQuery (#33669) # Details As mentioned in a previous front-end sync, I realized after having to add a `reload()` method to the `PaginatedList` imperative handle that I had strayed too far from the path. The original concept for this component was for it to be fully self-contained, so the parent didn't have to concern itself with pagination at all other than being told what page to load. But the addition of an `onChangePage` property isn't a tragedy and is totally worth the reduction in code, consistency with use of `useQuery` elsewhere and React best practice of passing data from parent to child. This refactor still retains the use of the imperative handle for querying the "dirty state" of the list, so parents don't have to manage that state themselves. ## Testing - [X] Added/updated automated tests Updated PaginatedList tests as needed. Also confirmed that tests for the upstream components (PoliciesPaginatedList, Secrets, RunBatchScriptModal and ScriptBatchProgress) all passed without any modification. - [X] QA'd all new/changed functionality manually Added enough data to paginate each of the components that uses PaginatedList, confirmed pagination still works and dirty state functionality (in PoliciesPaginatedList) still works. Also confirmed add/delete functionality works as expected in Secrets. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - New Features - Consistent, responsive pagination across Scripts, Secrets, Hosts, and Policies pages with clearer loading and empty states. - Bug Fixes - Delete Secret modal now reliably displays errors and refreshes the list after deletion. - Refactor - Unified data-driven pagination flow for improved performance and smoother navigation. - Simplified list interactions by removing manual reloads. - Tests - Updated tests to align with the new pagination behavior and data flow. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
2025-10-28 16:27:05 +00:00
onDeleteSecret: () => void;
}
const baseClass = "delete-custom-variable-modal";
const DeleteCustomVariableModal = ({
secret,
onExit,
Refactor paginated list to use UseQuery (#33669) # Details As mentioned in a previous front-end sync, I realized after having to add a `reload()` method to the `PaginatedList` imperative handle that I had strayed too far from the path. The original concept for this component was for it to be fully self-contained, so the parent didn't have to concern itself with pagination at all other than being told what page to load. But the addition of an `onChangePage` property isn't a tragedy and is totally worth the reduction in code, consistency with use of `useQuery` elsewhere and React best practice of passing data from parent to child. This refactor still retains the use of the imperative handle for querying the "dirty state" of the list, so parents don't have to manage that state themselves. ## Testing - [X] Added/updated automated tests Updated PaginatedList tests as needed. Also confirmed that tests for the upstream components (PoliciesPaginatedList, Secrets, RunBatchScriptModal and ScriptBatchProgress) all passed without any modification. - [X] QA'd all new/changed functionality manually Added enough data to paginate each of the components that uses PaginatedList, confirmed pagination still works and dirty state functionality (in PoliciesPaginatedList) still works. Also confirmed add/delete functionality works as expected in Secrets. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - New Features - Consistent, responsive pagination across Scripts, Secrets, Hosts, and Policies pages with clearer loading and empty states. - Bug Fixes - Delete Secret modal now reliably displays errors and refreshes the list after deletion. - Refactor - Unified data-driven pagination flow for improved performance and smoother navigation. - Simplified list interactions by removing manual reloads. - Tests - Updated tests to align with the new pagination behavior and data flow. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
2025-10-28 16:27:05 +00:00
onDeleteSecret,
}: DeleteCustomVariableModalProps) => {
const [isDeleting, setIsDeleting] = useState(false);
const { renderFlash } = useContext(NotificationContext);
const onClickDelete = async () => {
if (!secret) {
return;
}
setIsDeleting(true);
try {
await secretsAPI.deleteSecret(secret.id);
renderFlash("success", "Variable successfully deleted.");
Refactor paginated list to use UseQuery (#33669) # Details As mentioned in a previous front-end sync, I realized after having to add a `reload()` method to the `PaginatedList` imperative handle that I had strayed too far from the path. The original concept for this component was for it to be fully self-contained, so the parent didn't have to concern itself with pagination at all other than being told what page to load. But the addition of an `onChangePage` property isn't a tragedy and is totally worth the reduction in code, consistency with use of `useQuery` elsewhere and React best practice of passing data from parent to child. This refactor still retains the use of the imperative handle for querying the "dirty state" of the list, so parents don't have to manage that state themselves. ## Testing - [X] Added/updated automated tests Updated PaginatedList tests as needed. Also confirmed that tests for the upstream components (PoliciesPaginatedList, Secrets, RunBatchScriptModal and ScriptBatchProgress) all passed without any modification. - [X] QA'd all new/changed functionality manually Added enough data to paginate each of the components that uses PaginatedList, confirmed pagination still works and dirty state functionality (in PoliciesPaginatedList) still works. Also confirmed add/delete functionality works as expected in Secrets. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - New Features - Consistent, responsive pagination across Scripts, Secrets, Hosts, and Policies pages with clearer loading and empty states. - Bug Fixes - Delete Secret modal now reliably displays errors and refreshes the list after deletion. - Refactor - Unified data-driven pagination flow for improved performance and smoother navigation. - Simplified list interactions by removing manual reloads. - Tests - Updated tests to align with the new pagination behavior and data flow. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
2025-10-28 16:27:05 +00:00
onDeleteSecret();
} catch (error) {
2025-08-22 14:22:37 +00:00
const errorObject = formatErrorResponse(error);
const isInUseError =
errorObject.http_status === 409 &&
/used by/.test(errorObject?.base ?? "");
const message =
isInUseError && typeof errorObject?.base === "string"
? errorObject.base
: "An error occurred while deleting the custom variable. Please try again.";
renderFlash("error", message);
Refactor paginated list to use UseQuery (#33669) # Details As mentioned in a previous front-end sync, I realized after having to add a `reload()` method to the `PaginatedList` imperative handle that I had strayed too far from the path. The original concept for this component was for it to be fully self-contained, so the parent didn't have to concern itself with pagination at all other than being told what page to load. But the addition of an `onChangePage` property isn't a tragedy and is totally worth the reduction in code, consistency with use of `useQuery` elsewhere and React best practice of passing data from parent to child. This refactor still retains the use of the imperative handle for querying the "dirty state" of the list, so parents don't have to manage that state themselves. ## Testing - [X] Added/updated automated tests Updated PaginatedList tests as needed. Also confirmed that tests for the upstream components (PoliciesPaginatedList, Secrets, RunBatchScriptModal and ScriptBatchProgress) all passed without any modification. - [X] QA'd all new/changed functionality manually Added enough data to paginate each of the components that uses PaginatedList, confirmed pagination still works and dirty state functionality (in PoliciesPaginatedList) still works. Also confirmed add/delete functionality works as expected in Secrets. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - New Features - Consistent, responsive pagination across Scripts, Secrets, Hosts, and Policies pages with clearer loading and empty states. - Bug Fixes - Delete Secret modal now reliably displays errors and refreshes the list after deletion. - Refactor - Unified data-driven pagination flow for improved performance and smoother navigation. - Simplified list interactions by removing manual reloads. - Tests - Updated tests to align with the new pagination behavior and data flow. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
2025-10-28 16:27:05 +00:00
onExit();
} finally {
setIsDeleting(false);
}
};
return (
<Modal
title="Delete custom variable?"
onExit={onExit}
className={baseClass}
>
<div className={`${baseClass}__message`}>
<span>
This will delete the
<b>
<TooltipTruncatedText value={secret?.name} />
</b>
custom variable.
</span>
<br />
<br />
If this custom variable is used in any configuration profiles or
scripts, they will fail. To resolve, edit the configuration profile or
script.
</div>
<div className="modal-cta-wrap">
<Button
variant="alert"
onClick={onClickDelete}
isLoading={isDeleting}
disabled={isDeleting}
>
Delete
</Button>
<Button variant="inverse-alert" onClick={onExit}>
Cancel
</Button>
</div>
</Modal>
);
};
export default DeleteCustomVariableModal;