Skip to content

Commit

Permalink
[Fleet] Refresh policies when deleting agent policy (#175330)
Browse files Browse the repository at this point in the history
  • Loading branch information
nchaulet authored Jan 24, 2024
1 parent 1d4b7df commit 5bd32e6
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -47,6 +47,7 @@ export const AgentPolicyDeleteProvider: React.FunctionComponent<Props> = ({
const onSuccessCallback = useRef<OnSuccessCallback | null>(null);
const { getPath } = useLink();
const history = useHistory();
const deleteAgentPolicyMutation = useDeleteAgentPolicyMutation();

const deleteAgentPolicyPrompt: DeleteAgentPolicy = (
agentPolicyToDelete,
Expand All @@ -72,7 +73,7 @@ export const AgentPolicyDeleteProvider: React.FunctionComponent<Props> = ({
setIsLoading(true);

try {
const { data } = await sendDeleteAgentPolicy({
const { data } = await deleteAgentPolicyMutation.mutateAsync({
agentPolicyId: agentPolicy!,
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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' },
Expand All @@ -25,7 +25,7 @@ jest.mock('../../../hooks', () => ({
total: 2,
} as GetAgentPoliciesResponse,
isLoading: false,
resendRequest: jest.fn(),
refetch: jest.fn(),
}),
}));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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,
Expand Down
25 changes: 17 additions & 8 deletions x-pack/plugins/fleet/public/hooks/use_request/agent_policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -141,14 +141,23 @@ export const sendCopyAgentPolicy = (
});
};

export const sendDeleteAgentPolicy = (body: DeleteAgentPolicyRequest['body']) => {
return sendRequest<DeleteAgentPolicyResponse>({
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<DeleteAgentPolicyResponse>({
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({
Expand Down

0 comments on commit 5bd32e6

Please sign in to comment.