Skip to content
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

[ENDPOINT] Added close button to toast notifications by migrating to different API that is more widely used in Kibana and Security solution in particular. #73662

Merged
merged 4 commits into from
Jul 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
import { useHistory } from 'react-router-dom';
import { FormattedMessage } from '@kbn/i18n/react';
import { i18n } from '@kbn/i18n';
import { useKibana } from '../../../../../../../../../src/plugins/kibana_react/public';
import { useToasts } from '../../../../../common/lib/kibana';
import { useHostSelector } from '../hooks';
import { urlFromQueryParams } from '../url_from_query_params';
import {
Expand All @@ -44,7 +44,7 @@ import { useFormatUrl } from '../../../../../common/components/link_to';

export const HostDetailsFlyout = memo(() => {
const history = useHistory();
const { notifications } = useKibana();
const toasts = useToasts();
const queryParams = useHostSelector(uiQueryParams);
const { selected_host: selectedHost, ...queryParamsWithoutSelectedHost } = queryParams;
const details = useHostSelector(detailsData);
Expand All @@ -58,23 +58,16 @@ export const HostDetailsFlyout = memo(() => {

useEffect(() => {
if (error !== undefined) {
notifications.toasts.danger({
title: (
<FormattedMessage
id="xpack.securitySolution.endpoint.host.details.errorTitle"
defaultMessage="Could not find host"
/>
),
body: (
<FormattedMessage
id="xpack.securitySolution.endpoint.host.details.errorBody"
defaultMessage="Please exit the flyout and select an available host."
/>
),
toastLifeTimeMs: 10000,
efreeti marked this conversation as resolved.
Show resolved Hide resolved
toasts.addDanger({
title: i18n.translate('xpack.securitySolution.endpoint.host.details.errorTitle', {
defaultMessage: 'Could not find host',
}),
text: i18n.translate('xpack.securitySolution.endpoint.host.details.errorBody', {
defaultMessage: 'Please exit the flyout and select an available host.',
}),
});
}
}, [error, notifications.toasts]);
}, [error, toasts]);

return (
<EuiFlyout onClose={handleFlyoutClose} data-test-subj="hostDetailsFlyout" size="s">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,11 +255,11 @@ describe('Policy Details', () => {
policyView.update();

// Toast notification should be shown
const toastAddMock = coreStart.notifications.toasts.add.mock;
const toastAddMock = coreStart.notifications.toasts.addSuccess.mock;
expect(toastAddMock.calls).toHaveLength(1);
expect(toastAddMock.calls[0][0]).toMatchObject({
color: 'success',
iconType: 'check',
title: 'Success!',
text: expect.any(Function),
});
});
it('should show an error notification toast if update fails', async () => {
Expand All @@ -270,11 +270,11 @@ describe('Policy Details', () => {
policyView.update();

// Toast notification should be shown
const toastAddMock = coreStart.notifications.toasts.add.mock;
const toastAddMock = coreStart.notifications.toasts.addDanger.mock;
expect(toastAddMock.calls).toHaveLength(1);
expect(toastAddMock.calls[0][0]).toMatchObject({
color: 'danger',
iconType: 'alert',
title: 'Failed!',
text: expect.any(String),
});
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,12 @@ import {
isLoading,
apiError,
} from '../store/policy_details/selectors';
import { useKibana } from '../../../../../../../../src/plugins/kibana_react/public';
import { useKibana, toMountPoint } from '../../../../../../../../src/plugins/kibana_react/public';
import { AgentsSummary } from './agents_summary';
import { VerticalDivider } from './vertical_divider';
import { WindowsEvents, MacEvents, LinuxEvents } from './policy_forms/events';
import { MalwareProtections } from './policy_forms/protections/malware';
import { useToasts } from '../../../../common/lib/kibana';
import { AppAction } from '../../../../common/store/actions';
import { useNavigateByRouterEventHandler } from '../../../../common/hooks/endpoint/use_navigate_by_router_event_handler';
import { PageViewHeaderTitle } from '../../../../common/components/endpoint/page_view';
Expand All @@ -51,11 +52,11 @@ import { PolicyDetailsRouteState } from '../../../../../common/endpoint/types';
export const PolicyDetails = React.memo(() => {
const dispatch = useDispatch<(action: AppAction) => void>();
const {
notifications,
services: {
application: { navigateToApp },
},
} = useKibana();
const toasts = useToasts();
const { formatUrl } = useFormatUrl(SecurityPageName.administration);
const { state: locationRouteState } = useLocation<PolicyDetailsRouteState>();

Expand All @@ -76,15 +77,14 @@ export const PolicyDetails = React.memo(() => {
useEffect(() => {
if (policyUpdateStatus) {
if (policyUpdateStatus.success) {
notifications.toasts.success({
toastLifeTimeMs: 10000,
toasts.addSuccess({
title: i18n.translate(
'xpack.securitySolution.endpoint.policy.details.updateSuccessTitle',
{
defaultMessage: 'Success!',
}
),
body: (
text: toMountPoint(
<span data-test-subj="policyDetailsSuccessMessage">
<FormattedMessage
id="xpack.securitySolution.endpoint.policy.details.updateSuccessMessage"
Expand All @@ -99,16 +99,15 @@ export const PolicyDetails = React.memo(() => {
navigateToApp(...routeState.onSaveNavigateTo);
}
} else {
notifications.toasts.danger({
toastLifeTimeMs: 10000,
toasts.addDanger({
title: i18n.translate('xpack.securitySolution.endpoint.policy.details.updateErrorTitle', {
defaultMessage: 'Failed!',
}),
body: <>{policyUpdateStatus.error!.message}</>,
text: policyUpdateStatus.error!.message,
});
}
}
}, [navigateToApp, notifications.toasts, policyName, policyUpdateStatus, routeState]);
}, [navigateToApp, toasts, policyName, policyUpdateStatus, routeState]);

const handleBackToListOnClick = useNavigateByRouterEventHandler(hostListRouterPath);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import { CreateStructuredSelector } from '../../../../common/store';
import * as selectors from '../store/policy_list/selectors';
import { usePolicyListSelector } from './policy_hooks';
import { PolicyListAction } from '../store/policy_list';
import { useToasts } from '../../../../common/lib/kibana';
import { useKibana } from '../../../../../../../../src/plugins/kibana_react/public';
import { Immutable, PolicyData } from '../../../../../common/endpoint/types';
import { useNavigateByRouterEventHandler } from '../../../../common/hooks/endpoint/use_navigate_by_router_event_handler';
Expand Down Expand Up @@ -124,7 +125,8 @@ const PolicyLink: React.FC<{ name: string; route: string; href: string }> = ({

const selector = (createStructuredSelector as CreateStructuredSelector)(selectors);
export const PolicyList = React.memo(() => {
const { services, notifications } = useKibana();
const { services } = useKibana();
const toasts = useToasts();
const history = useHistory();
const location = useLocation();
const { formatUrl, search } = useFormatUrl(SecurityPageName.administration);
Expand Down Expand Up @@ -167,45 +169,42 @@ export const PolicyList = React.memo(() => {

useEffect(() => {
if (apiError) {
notifications.toasts.danger({
toasts.addDanger({
title: apiError.error,
body: apiError.message,
toastLifeTimeMs: 10000,
text: apiError.message,
});
}
}, [apiError, dispatch, notifications.toasts]);
}, [apiError, dispatch, toasts]);

// Handle showing update statuses
useEffect(() => {
if (deleteStatus !== undefined) {
if (deleteStatus === true) {
setPolicyIdToDelete('');
setShowDelete(false);
notifications.toasts.success({
toastLifeTimeMs: 10000,
toasts.addSuccess({
title: i18n.translate('xpack.securitySolution.endpoint.policyList.deleteSuccessToast', {
defaultMessage: 'Success!',
}),
body: (
<FormattedMessage
id="xpack.securitySolution.endpoint.policyList.deleteSuccessToastDetails"
defaultMessage="Policy has been deleted."
/>
text: i18n.translate(
'xpack.securitySolution.endpoint.policyList.deleteSuccessToastDetails',
{
defaultMessage: 'Policy has been deleted.',
}
),
});
} else {
notifications.toasts.danger({
toastLifeTimeMs: 10000,
toasts.addDanger({
title: i18n.translate('xpack.securitySolution.endpoint.policyList.deleteFailedToast', {
defaultMessage: 'Failed!',
}),
body: i18n.translate('xpack.securitySolution.endpoint.policyList.deleteFailedToastBody', {
text: i18n.translate('xpack.securitySolution.endpoint.policyList.deleteFailedToastBody', {
defaultMessage: 'Failed to delete policy',
}),
});
}
}
}, [notifications.toasts, deleteStatus]);
}, [toasts, deleteStatus]);

const paginationSetup = useMemo(() => {
return {
Expand Down