diff --git a/x-pack/plugins/fleet/public/applications/fleet/sections/agent_policy/components/agent_policy_create_inline.tsx b/x-pack/plugins/fleet/public/applications/fleet/sections/agent_policy/components/agent_policy_create_inline.tsx index afab3980c1f56e..b4fbd874ebf138 100644 --- a/x-pack/plugins/fleet/public/applications/fleet/sections/agent_policy/components/agent_policy_create_inline.tsx +++ b/x-pack/plugins/fleet/public/applications/fleet/sections/agent_policy/components/agent_policy_create_inline.tsx @@ -39,7 +39,7 @@ const StyledEuiAccordion = styled(EuiAccordion)` `; interface Props { - updateAgentPolicy: (u: AgentPolicy | null) => void; + updateAgentPolicy: (u: AgentPolicy | null, errorMessage?: JSX.Element) => void; isFleetServerPolicy?: boolean; agentPolicyName: string; } @@ -84,12 +84,24 @@ export const AgentPolicyCreateInlineForm: React.FunctionComponent = ({ updateAgentPolicy(resp.data.item); } } catch (e) { - updateAgentPolicy(null); + updateAgentPolicy(null, mapError(e)); } finally { setIsLoading(false); } }, [newAgentPolicy, withSysMonitoring, updateAgentPolicy]); + function mapError(e: { statusCode: number }): JSX.Element | undefined { + switch (e.statusCode) { + case 409: + return ( + + ); + } + } + return ( diff --git a/x-pack/plugins/fleet/public/applications/fleet/sections/agents/components/agent_policy_created_callout.tsx b/x-pack/plugins/fleet/public/applications/fleet/sections/agents/components/agent_policy_created_callout.tsx index ba3af7716d985c..b73bd7251f8533 100644 --- a/x-pack/plugins/fleet/public/applications/fleet/sections/agents/components/agent_policy_created_callout.tsx +++ b/x-pack/plugins/fleet/public/applications/fleet/sections/agents/components/agent_policy_created_callout.tsx @@ -8,21 +8,27 @@ import React from 'react'; import { EuiSpacer, EuiCallOut } from '@elastic/eui'; import { FormattedMessage } from '@kbn/i18n-react'; + export enum CREATE_STATUS { INITIAL = 'initial', CREATED = 'created', FAILED = 'failed', } +export interface AgentPolicyCreateState { + status: CREATE_STATUS; + errorMessage?: JSX.Element; +} + interface Props { - createStatus: CREATE_STATUS; + createState: AgentPolicyCreateState; } -export const AgentPolicyCreatedCallOut: React.FunctionComponent = ({ createStatus }) => { +export const AgentPolicyCreatedCallOut: React.FunctionComponent = ({ createState }) => { return ( <> - {createStatus === CREATE_STATUS.CREATED ? ( + {createState.status === CREATE_STATUS.CREATED ? ( = ({ crea } color="danger" iconType="cross" - /> + > + {createState.errorMessage ?? null} + )} ); diff --git a/x-pack/plugins/fleet/public/components/agent_enrollment_flyout/agent_policy_select_create.tsx b/x-pack/plugins/fleet/public/components/agent_enrollment_flyout/agent_policy_select_create.tsx index 87382ac70a9bb6..a8354237bbcb7c 100644 --- a/x-pack/plugins/fleet/public/components/agent_enrollment_flyout/agent_policy_select_create.tsx +++ b/x-pack/plugins/fleet/public/components/agent_enrollment_flyout/agent_policy_select_create.tsx @@ -7,6 +7,7 @@ import React, { useState, useCallback, useEffect } from 'react'; +import type { AgentPolicyCreateState } from '../../applications/fleet/sections/agents/components'; import { AgentPolicyCreatedCallOut, CREATE_STATUS, @@ -40,7 +41,9 @@ export const SelectCreateAgentPolicy: React.FC = ({ }) => { const [showCreatePolicy, setShowCreatePolicy] = useState(agentPolicies.length === 0); - const [createStatus, setCreateStatus] = useState(CREATE_STATUS.INITIAL); + const [createState, setCreateState] = useState({ + status: CREATE_STATUS.INITIAL, + }); const [newName, setNewName] = useState(incrementPolicyName(agentPolicies, isFleetServerPolicy)); @@ -52,13 +55,13 @@ export const SelectCreateAgentPolicy: React.FC = ({ }, [agentPolicies, isFleetServerPolicy]); const onAgentPolicyCreated = useCallback( - async (policy: AgentPolicy | null) => { + async (policy: AgentPolicy | null, errorMessage?: JSX.Element) => { if (!policy) { - setCreateStatus(CREATE_STATUS.FAILED); + setCreateState({ status: CREATE_STATUS.FAILED, errorMessage }); return; } setShowCreatePolicy(false); - setCreateStatus(CREATE_STATUS.CREATED); + setCreateState({ status: CREATE_STATUS.CREATED }); if (onAgentPolicyChange) { onAgentPolicyChange(policy.id, policy!); } @@ -88,8 +91,8 @@ export const SelectCreateAgentPolicy: React.FC = ({ isFleetServerPolicy={isFleetServerPolicy} /> )} - {createStatus !== CREATE_STATUS.INITIAL && ( - + {createState.status !== CREATE_STATUS.INITIAL && ( + )} );