From dab972df6f5f01dab5669ed5b53231c076a92823 Mon Sep 17 00:00:00 2001 From: Nicolas Chaulet Date: Thu, 27 Feb 2020 18:00:29 -0500 Subject: [PATCH] [Fleet] Fix after review --- .../agent_enrollment_flyout/index.tsx | 33 ++++- .../agent_enrollment_flyout/instructions.tsx | 1 - .../agent_enrollment_flyout/key_selection.tsx | 125 +++++++++++++++--- 3 files changed, 135 insertions(+), 24 deletions(-) diff --git a/x-pack/plugins/ingest_manager/public/applications/ingest_manager/sections/fleet/agent_list_page/components/agent_enrollment_flyout/index.tsx b/x-pack/plugins/ingest_manager/public/applications/ingest_manager/sections/fleet/agent_list_page/components/agent_enrollment_flyout/index.tsx index 2e87f75ef0edb..60256599c6680 100644 --- a/x-pack/plugins/ingest_manager/public/applications/ingest_manager/sections/fleet/agent_list_page/components/agent_enrollment_flyout/index.tsx +++ b/x-pack/plugins/ingest_manager/public/applications/ingest_manager/sections/fleet/agent_list_page/components/agent_enrollment_flyout/index.tsx @@ -4,7 +4,18 @@ * you may not use this file except in compliance with the Elastic License. */ import React, { useState } from 'react'; -import { EuiFlyout, EuiFlyoutBody, EuiFlyoutHeader, EuiSpacer, EuiTitle } from '@elastic/eui'; +import { + EuiFlyout, + EuiFlyoutBody, + EuiFlyoutHeader, + EuiSpacer, + EuiTitle, + EuiFlexGroup, + EuiFlexItem, + EuiButtonEmpty, + EuiButton, + EuiFlyoutFooter, +} from '@elastic/eui'; import { FormattedMessage } from '@kbn/i18n/react'; import { AgentConfig } from '../../../../../types'; import { APIKeySelection } from './key_selection'; @@ -41,6 +52,26 @@ export const AgentEnrollmentFlyout: React.FunctionComponent = ({ + + + + + + + + + + + + + + ); }; diff --git a/x-pack/plugins/ingest_manager/public/applications/ingest_manager/sections/fleet/agent_list_page/components/agent_enrollment_flyout/instructions.tsx b/x-pack/plugins/ingest_manager/public/applications/ingest_manager/sections/fleet/agent_list_page/components/agent_enrollment_flyout/instructions.tsx index 1d561e36d768e..97434d2178852 100644 --- a/x-pack/plugins/ingest_manager/public/applications/ingest_manager/sections/fleet/agent_list_page/components/agent_enrollment_flyout/instructions.tsx +++ b/x-pack/plugins/ingest_manager/public/applications/ingest_manager/sections/fleet/agent_list_page/components/agent_enrollment_flyout/instructions.tsx @@ -108,7 +108,6 @@ export const EnrollmentInstructions: React.FunctionComponent = ({ selecte ) : ( <> - void; agentConfigs: AgentConfig[]; } +function useCreateApiKeyForm(configId: string | null, onSuccess: (keyId: string) => void) { + const { notifications } = useCore(); + const [isLoading, setIsLoading] = useState(false); + const apiKeyNameInput = useInput(''); + + const onSubmit = async (event: React.FormEvent) => { + event.preventDefault(); + setIsLoading(true); + try { + const res = await sendRequest({ + method: 'post', + path: enrollmentAPIKeyRouteService.getCreatePath(), + body: JSON.stringify({ + name: apiKeyNameInput.value, + config_id: configId, + }), + }); + apiKeyNameInput.clear(); + setIsLoading(false); + onSuccess(res.data.item.id); + } catch (err) { + notifications.toasts.addError(err as Error, { + title: 'Error', + }); + setIsLoading(false); + } + }; + + return { + isLoading, + onSubmit, + apiKeyNameInput, + }; +} + export const APIKeySelection: React.FunctionComponent = ({ onKeyChange, agentConfigs }) => { const enrollmentAPIKeysRequest = useEnrollmentApiKeys({ currentPage: 1, @@ -33,7 +80,7 @@ export const APIKeySelection: React.FunctionComponent = ({ onKeyChange, a } return enrollmentAPIKeysRequest.data.list.filter( - key => key.policy_id === selectedState.agentConfigId + key => key.config_id === selectedState.agentConfigId ); }, [enrollmentAPIKeysRequest.data, selectedState.agentConfigId]); @@ -50,6 +97,16 @@ export const APIKeySelection: React.FunctionComponent = ({ onKeyChange, a // eslint-disable-next-line react-hooks/exhaustive-deps }, [filteredEnrollmentAPIKeys, selectedState.enrollmentAPIKeyId, selectedState.agentConfigId]); + const [showAPIKeyForm, setShowAPIKeyForm] = useState(false); + const apiKeyForm = useCreateApiKeyForm(selectedState.agentConfigId, async (keyId: string) => { + const res = await enrollmentAPIKeysRequest.refresh(); + setSelectedState({ + ...selectedState, + enrollmentAPIKeyId: res.data?.list.find(key => key.id === keyId)?.id ?? null, + }); + setShowAPIKeyForm(false); + }); + return ( <> @@ -94,28 +151,52 @@ export const APIKeySelection: React.FunctionComponent = ({ onKeyChange, a } labelAppend={ - { - await enrollmentAPIKeysRequest.refresh(); - }} - /> + setShowAPIKeyForm(!showAPIKeyForm)} color="primary"> + {showAPIKeyForm ? ( + + ) : ( + + )} + } > - ({ - value: key.id, - text: key.name, - }))} - value={selectedState.enrollmentAPIKeyId || undefined} - onChange={e => { - setSelectedState({ - ...selectedState, - enrollmentAPIKeyId: e.target.value, - }); - onKeyChange(selectedState.enrollmentAPIKeyId); - }} - /> + {showAPIKeyForm ? ( +
+ + + ) : ( + ({ + value: key.id, + text: key.name, + }))} + value={selectedState.enrollmentAPIKeyId || undefined} + onChange={e => { + setSelectedState({ + ...selectedState, + enrollmentAPIKeyId: e.target.value, + }); + onKeyChange(selectedState.enrollmentAPIKeyId); + }} + /> + )}