-
Notifications
You must be signed in to change notification settings - Fork 8.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Multiselect to unenroll agents #48852
Merged
jen-huang
merged 7 commits into
elastic:feature-make-it-ingest
from
jen-huang:feature/fleet/agent-list-multi
Oct 23, 2019
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
403c28a
Initial pass at multiselect to unenroll agents
jen-huang 0305b13
Adjust loading state button text
jen-huang a2d964d
Fix types; use unenroll provider in agent details
jen-huang b7184b5
Update select all agents kuery
jen-huang 8615d27
Merge remote-tracking branch 'upstream/feature-make-it-ingest' into f…
jen-huang 6df78e7
Merge remote-tracking branch 'upstream/feature-make-it-ingest' into f…
jen-huang 1efdac8
Prevent inactive agents from being selected
jen-huang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
172 changes: 172 additions & 0 deletions
172
x-pack/legacy/plugins/fleet/public/components/agent_unenroll_provider.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,172 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import React, { Fragment, useRef, useState } from 'react'; | ||
import { EuiConfirmModal, EuiOverlayMask } from '@elastic/eui'; | ||
import { i18n } from '@kbn/i18n'; | ||
import { FormattedMessage } from '@kbn/i18n/react'; | ||
import { useLibs } from '../hooks/use_libs'; | ||
|
||
interface Props { | ||
children: (unenrollAgents: UnenrollAgents) => React.ReactElement; | ||
} | ||
|
||
export type UnenrollAgents = ( | ||
agents: string[] | string, | ||
agentsCount: number, | ||
onSuccess?: OnSuccessCallback | ||
) => void; | ||
|
||
type OnSuccessCallback = (agentsUnenrolled: string[]) => void; | ||
|
||
export const AgentUnenrollProvider: React.FunctionComponent<Props> = ({ children }) => { | ||
const libs = useLibs(); | ||
const [agents, setAgents] = useState<string[] | string>([]); | ||
const [agentsCount, setAgentsCount] = useState<number>(0); | ||
const [isModalOpen, setIsModalOpen] = useState<boolean>(false); | ||
const [isLoading, setIsLoading] = useState<boolean>(false); | ||
const onSuccessCallback = useRef<OnSuccessCallback | null>(null); | ||
|
||
const unenrollAgentsPrompt: UnenrollAgents = ( | ||
agentsToUnenroll, | ||
agentsToUnenrollCount, | ||
onSuccess = () => undefined | ||
) => { | ||
if ( | ||
agentsToUnenroll === undefined || | ||
(Array.isArray(agentsToUnenroll) && agentsToUnenroll.length === 0) | ||
) { | ||
throw new Error('No agents specified for unenrollment'); | ||
} | ||
setIsModalOpen(true); | ||
setAgents(agentsToUnenroll); | ||
setAgentsCount(agentsToUnenrollCount); | ||
onSuccessCallback.current = onSuccess; | ||
}; | ||
|
||
const closeModal = () => { | ||
setAgents([]); | ||
setAgentsCount(0); | ||
setIsLoading(false); | ||
setIsModalOpen(false); | ||
}; | ||
|
||
const unenrollAgents = async () => { | ||
setIsLoading(true); | ||
|
||
try { | ||
const unenrollByKuery = typeof agents === 'string'; | ||
const agentsToUnenroll = | ||
unenrollByKuery && !(agents as string).trim() ? 'agents.active:true' : agents; | ||
const unenrollMethod = unenrollByKuery ? libs.agents.unenrollByKuery : libs.agents.unenroll; | ||
const { results } = await unenrollMethod(agentsToUnenroll as string & string[]); | ||
|
||
const successfulResults = results.filter(result => result.success); | ||
const failedResults = results.filter(result => !result.success); | ||
|
||
if (successfulResults.length) { | ||
const hasMultipleSuccesses = successfulResults.length > 1; | ||
const successMessage = hasMultipleSuccesses | ||
? i18n.translate('xpack.fleet.unenrollAgents.successMultipleNotificationTitle', { | ||
defaultMessage: 'Unenrolled {count} agents', | ||
values: { count: successfulResults.length }, | ||
}) | ||
: i18n.translate('xpack.fleet.unenrollAgents.successSingleNotificationTitle', { | ||
defaultMessage: "Unenrolled agent '{id}'", | ||
values: { id: successfulResults[0].id }, | ||
}); | ||
libs.framework.notifications.addSuccess(successMessage); | ||
} | ||
|
||
if (failedResults.length) { | ||
const hasMultipleFailures = failedResults.length > 1; | ||
const failureMessage = hasMultipleFailures | ||
? i18n.translate('xpack.fleet.unenrollAgents.failureMultipleNotificationTitle', { | ||
defaultMessage: 'Error unenrolling {count} agents', | ||
values: { count: failedResults.length }, | ||
}) | ||
: i18n.translate('xpack.fleet.unenrollAgents.failureSingleNotificationTitle', { | ||
defaultMessage: "Error unenrolling agent '{id}'", | ||
values: { id: failedResults[0].id }, | ||
}); | ||
libs.framework.notifications.addDanger(failureMessage); | ||
} | ||
|
||
if (onSuccessCallback.current) { | ||
onSuccessCallback.current(successfulResults.map(result => result.id)); | ||
} | ||
} catch (e) { | ||
libs.framework.notifications.addDanger( | ||
i18n.translate('xpack.fleet.unenrollAgents.fatalErrorNotificationTitle', { | ||
defaultMessage: 'Fatal error unenrolling agents', | ||
}) | ||
); | ||
} | ||
|
||
closeModal(); | ||
}; | ||
|
||
const renderModal = () => { | ||
if (!isModalOpen) { | ||
return null; | ||
} | ||
|
||
const unenrollByKuery = typeof agents === 'string'; | ||
const isSingle = agentsCount === 1; | ||
|
||
return ( | ||
<EuiOverlayMask> | ||
<EuiConfirmModal | ||
title={ | ||
isSingle && !unenrollByKuery ? ( | ||
<FormattedMessage | ||
id="xpack.fleet.unenrollAgents.confirmModal.deleteSingleTitle" | ||
defaultMessage="Unenroll agent '{id}'?" | ||
values={{ id: agents[0] }} | ||
/> | ||
) : ( | ||
<FormattedMessage | ||
id="xpack.fleet.unenrollAgents.confirmModal.deleteMultipleTitle" | ||
defaultMessage="Unenroll {count, plural, one {# agent} other {# agents}}?" | ||
values={{ count: agentsCount }} | ||
/> | ||
) | ||
} | ||
onCancel={closeModal} | ||
onConfirm={unenrollAgents} | ||
cancelButtonText={ | ||
<FormattedMessage | ||
id="xpack.fleet.unenrollAgents.confirmModal.cancelButtonLabel" | ||
defaultMessage="Cancel" | ||
/> | ||
} | ||
confirmButtonText={ | ||
isLoading ? ( | ||
<FormattedMessage | ||
id="xpack.fleet.unenrollAgents.confirmModal.loadingButtonLabel" | ||
defaultMessage="Loading…" | ||
/> | ||
) : ( | ||
<FormattedMessage | ||
id="xpack.fleet.unenrollAgents.confirmModal.confirmButtonLabel" | ||
defaultMessage="Unenroll" | ||
/> | ||
) | ||
} | ||
buttonColor="danger" | ||
confirmButtonDisabled={isLoading} | ||
/> | ||
</EuiOverlayMask> | ||
); | ||
}; | ||
|
||
return ( | ||
<Fragment> | ||
{children(unenrollAgentsPrompt)} | ||
{renderModal()} | ||
</Fragment> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
@import 'pages/agent_list/index.scss'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 0 additions & 35 deletions
35
x-pack/legacy/plugins/fleet/public/pages/agent_details/components/modal_confirm_unenroll.tsx
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
happy to discuss that, but I have a strong preference to use only one state
{ agents, agentCount, isLoading ...}
:{isLoading: true, isModalOpen: false}
setState
and component re renderAlso personal preference I like to move the state logic in a hook outside the component, I think it make thing more readable
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤔 I norm write my code like @jen-huang did in this file, but you make REALLY good points here. I am a fan. Moving state logic out would make it much easier to test the logic in unit tests so I am especially fond of that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the typescript variants and unit testing are great benefits; I'm not worried about re-rendering since 1) React batches multiple
setState
calls and 2) re-rendering first happens in virtual DOM before being pushed up to real DOMI'll try moving state logic out the next chance I have and see if I like it 😄