From 0c12cb25140fc2bedcead86a4120f4d933ac0f73 Mon Sep 17 00:00:00 2001 From: Nicolas Chaulet Date: Wed, 24 Jan 2024 10:27:55 -0500 Subject: [PATCH] [Fleet] Refresh policies when deleting agent policy (#175330) --- .../agent_policy_delete_provider.tsx | 5 ++-- .../agent_policy/list_page/index.test.tsx | 4 +-- .../sections/agent_policy/list_page/index.tsx | 6 ++--- .../public/hooks/use_request/agent_policy.ts | 25 +++++++++++++------ 4 files changed, 25 insertions(+), 15 deletions(-) diff --git a/x-pack/plugins/fleet/public/applications/fleet/sections/agent_policy/components/agent_policy_delete_provider.tsx b/x-pack/plugins/fleet/public/applications/fleet/sections/agent_policy/components/agent_policy_delete_provider.tsx index c80a257c93bcd..84f40ee4b692f 100644 --- a/x-pack/plugins/fleet/public/applications/fleet/sections/agent_policy/components/agent_policy_delete_provider.tsx +++ b/x-pack/plugins/fleet/public/applications/fleet/sections/agent_policy/components/agent_policy_delete_provider.tsx @@ -14,11 +14,11 @@ import { useHistory } from 'react-router-dom'; import { AGENTS_PREFIX } from '../../../constants'; import { - sendDeleteAgentPolicy, useStartServices, useConfig, sendRequest, useLink, + useDeleteAgentPolicyMutation, } from '../../../hooks'; import { API_VERSIONS } from '../../../../../../common/constants'; @@ -47,6 +47,7 @@ export const AgentPolicyDeleteProvider: React.FunctionComponent = ({ const onSuccessCallback = useRef(null); const { getPath } = useLink(); const history = useHistory(); + const deleteAgentPolicyMutation = useDeleteAgentPolicyMutation(); const deleteAgentPolicyPrompt: DeleteAgentPolicy = ( agentPolicyToDelete, @@ -72,7 +73,7 @@ export const AgentPolicyDeleteProvider: React.FunctionComponent = ({ setIsLoading(true); try { - const { data } = await sendDeleteAgentPolicy({ + const { data } = await deleteAgentPolicyMutation.mutateAsync({ agentPolicyId: agentPolicy!, }); diff --git a/x-pack/plugins/fleet/public/applications/fleet/sections/agent_policy/list_page/index.test.tsx b/x-pack/plugins/fleet/public/applications/fleet/sections/agent_policy/list_page/index.test.tsx index e2e9c11192b0e..3f50ad4ac514e 100644 --- a/x-pack/plugins/fleet/public/applications/fleet/sections/agent_policy/list_page/index.test.tsx +++ b/x-pack/plugins/fleet/public/applications/fleet/sections/agent_policy/list_page/index.test.tsx @@ -16,7 +16,7 @@ import { AgentPolicyListPage } from '.'; jest.mock('../../../hooks', () => ({ ...jest.requireActual('../../../hooks'), - useGetAgentPolicies: jest.fn().mockReturnValue({ + useGetAgentPoliciesQuery: jest.fn().mockReturnValue({ data: { items: [ { id: 'not_managed_policy', is_managed: false, updated_at: '2023-04-06T07:19:29.892Z' }, @@ -25,7 +25,7 @@ jest.mock('../../../hooks', () => ({ total: 2, } as GetAgentPoliciesResponse, isLoading: false, - resendRequest: jest.fn(), + refetch: jest.fn(), }), })); diff --git a/x-pack/plugins/fleet/public/applications/fleet/sections/agent_policy/list_page/index.tsx b/x-pack/plugins/fleet/public/applications/fleet/sections/agent_policy/list_page/index.tsx index e7c691fce5ea9..a80c21393df22 100644 --- a/x-pack/plugins/fleet/public/applications/fleet/sections/agent_policy/list_page/index.tsx +++ b/x-pack/plugins/fleet/public/applications/fleet/sections/agent_policy/list_page/index.tsx @@ -26,13 +26,13 @@ import type { AgentPolicy } from '../../../types'; import { AGENT_POLICY_SAVED_OBJECT_TYPE } from '../../../constants'; import { useAuthz, - useGetAgentPolicies, usePagination, useSorting, useLink, useConfig, useUrlParams, useBreadcrumbs, + useGetAgentPoliciesQuery, } from '../../../hooks'; import { SearchBar } from '../../../components'; import { AgentPolicySummaryLine } from '../../../../../components'; @@ -83,8 +83,8 @@ export const AgentPolicyListPage: React.FunctionComponent<{}> = () => { const { isLoading, data: agentPolicyData, - resendRequest, - } = useGetAgentPolicies({ + refetch: resendRequest, + } = useGetAgentPoliciesQuery({ page: pagination.currentPage, perPage: pagination.pageSize, sortField: sorting?.field, diff --git a/x-pack/plugins/fleet/public/hooks/use_request/agent_policy.ts b/x-pack/plugins/fleet/public/hooks/use_request/agent_policy.ts index 6ce49febdeca2..9dbc939907f38 100644 --- a/x-pack/plugins/fleet/public/hooks/use_request/agent_policy.ts +++ b/x-pack/plugins/fleet/public/hooks/use_request/agent_policy.ts @@ -4,7 +4,7 @@ * 2.0; you may not use this file except in compliance with the Elastic License * 2.0. */ -import { useQuery } from '@tanstack/react-query'; +import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query'; import { agentPolicyRouteService } from '../../services'; import { API_VERSIONS } from '../../../common/constants'; @@ -141,14 +141,23 @@ export const sendCopyAgentPolicy = ( }); }; -export const sendDeleteAgentPolicy = (body: DeleteAgentPolicyRequest['body']) => { - return sendRequest({ - path: agentPolicyRouteService.getDeletePath(), - method: 'post', - body: JSON.stringify(body), - version: API_VERSIONS.public.v1, +export function useDeleteAgentPolicyMutation() { + const queryClient = useQueryClient(); + + return useMutation({ + mutationFn: function sendDeleteAgentPolicy(body: DeleteAgentPolicyRequest['body']) { + return sendRequest({ + path: agentPolicyRouteService.getDeletePath(), + method: 'post', + body: JSON.stringify(body), + version: API_VERSIONS.public.v1, + }); + }, + onSuccess: () => { + return queryClient.invalidateQueries(['agentPolicies']); + }, }); -}; +} export const sendResetOnePreconfiguredAgentPolicy = (agentPolicyId: string) => { return sendRequest({