Skip to content

Commit

Permalink
moved managed logic to agent policy multi select
Browse files Browse the repository at this point in the history
  • Loading branch information
juliaElastic committed Jun 28, 2024
1 parent aabc753 commit d56993c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,45 @@ import type { EuiComboBoxOptionOption } from '@elastic/eui';
import { EuiComboBox } from '@elastic/eui';

import { i18n } from '@kbn/i18n';
import { uniq } from 'lodash';

import React, { useMemo } from 'react';
import React, { useCallback, useMemo } from 'react';

import type { PackageInfo } from '../../../../../../../../../common';
import type { AgentPolicy, PackageInfo } from '../../../../../../../../../common';

export interface Props {
isLoading: boolean;
agentPolicyMultiOptions: Array<EuiComboBoxOptionOption<string>>;
selectedPolicyIds: string[];
setSelectedPolicyIds: (policyIds: string[]) => void;
packageInfo?: PackageInfo;
selectedAgentPolicies: AgentPolicy[];
}

export const AgentPolicyMultiSelect: React.FunctionComponent<Props> = ({
isLoading,
agentPolicyMultiOptions,
selectedPolicyIds,
setSelectedPolicyIds,
selectedAgentPolicies,
}) => {
const selectedOptions = useMemo(() => {
return agentPolicyMultiOptions.filter((option) => selectedPolicyIds.includes(option.key!));
}, [agentPolicyMultiOptions, selectedPolicyIds]);

// managed policies cannot be removed
const updateSelectedPolicyIds = useCallback(
(ids: string[]) => {
setSelectedPolicyIds(
uniq([
...selectedAgentPolicies.filter((policy) => policy.is_managed).map((policy) => policy.id),
...ids,
])
);
},
[selectedAgentPolicies, setSelectedPolicyIds]
);

return (
<EuiComboBox
aria-label="Select Multiple Agent Policies"
Expand All @@ -44,9 +60,9 @@ export const AgentPolicyMultiSelect: React.FunctionComponent<Props> = ({
)}
options={agentPolicyMultiOptions}
selectedOptions={selectedOptions}
onChange={(newOptions) => {
setSelectedPolicyIds(newOptions.map((option: any) => option.key));
}}
onChange={(newOptions) =>
updateSelectedPolicyIds(newOptions.map((option: any) => option.key))
}
isClearable={true}
isLoading={isLoading}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ export const StepSelectAgentPolicy: React.FunctionComponent<{

const [selectedPolicyIds, setSelectedPolicyIds] = useState<string[]>([]);
const [isFirstLoad, setIsFirstLoad] = useState<boolean>(true);

const [isLoadingSelectedAgentPolicies, setIsLoadingSelectedAgentPolicies] =
useState<boolean>(false);
const [selectedAgentPolicies, setSelectedAgentPolicies] = useState<AgentPolicy[]>(agentPolicies);
Expand Down Expand Up @@ -183,13 +182,6 @@ export const StepSelectAgentPolicy: React.FunctionComponent<{
doesAgentPolicyHaveLimitedPackage(selectedAgentPolicy, packageInfo)
);

// managed policies cannot be removed
const updateSelectedPolicyIds = (ids: string[]) =>
setSelectedPolicyIds([
...agentPolicies.filter((policy) => policy.is_managed).map((policy) => policy.id),
...ids,
]);

return (
<>
<EuiFlexGroup direction="column" gutterSize="m">
Expand Down Expand Up @@ -263,8 +255,9 @@ export const StepSelectAgentPolicy: React.FunctionComponent<{
<AgentPolicyMultiSelect
isLoading={isLoading || !packageInfo || isLoadingSelectedAgentPolicies}
selectedPolicyIds={selectedPolicyIds}
setSelectedPolicyIds={updateSelectedPolicyIds}
setSelectedPolicyIds={setSelectedPolicyIds}
agentPolicyMultiOptions={agentPolicyMultiOptions}
selectedAgentPolicies={agentPolicies}
/>
) : (
<EuiSuperSelect
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
EuiFormRow,
EuiText,
} from '@elastic/eui';
import React, { useState, useMemo, useCallback } from 'react';
import React, { useState, useMemo } from 'react';
import { FormattedMessage } from '@kbn/i18n-react';

import { i18n } from '@kbn/i18n';
Expand Down Expand Up @@ -110,16 +110,6 @@ export const ManageAgentPoliciesModal: React.FunctionComponent<Props> = ({

const { agentPolicyMultiOptions, isLoading } = useAgentPoliciesOptions(packageInfo);

// managed policies cannot be removed
const updateSelectedPolicyIds = useCallback(
(ids) =>
setSelectedPolicyIds([
...selectedAgentPolicies.filter((policy) => policy.is_managed).map((policy) => policy.id),
...ids,
]),
[selectedAgentPolicies, setSelectedPolicyIds]
);

return (
<EuiConfirmModal
title={
Expand Down Expand Up @@ -182,8 +172,9 @@ export const ManageAgentPoliciesModal: React.FunctionComponent<Props> = ({
<AgentPolicyMultiSelect
isLoading={isLoading}
selectedPolicyIds={selectedPolicyIds}
setSelectedPolicyIds={updateSelectedPolicyIds}
setSelectedPolicyIds={setSelectedPolicyIds}
agentPolicyMultiOptions={agentPolicyMultiOptions}
selectedAgentPolicies={selectedAgentPolicies}
/>
</EuiFormRow>
</EuiFlexItem>
Expand Down

0 comments on commit d56993c

Please sign in to comment.