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

Remove GlobalToastList wrapper directive. Add onChange callback. #16375

Merged
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
6 changes: 1 addition & 5 deletions src/ui/public/chrome/directives/kbn_chrome.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,7 @@
</div>
</div>

<global-toast-list
toasts="toastNotifications"
dismiss-toast="dismissToast"
toast-life-time-ms="TOAST_LIFE_TIME_MS"
></global-toast-list>
<div id="globalToastList"></div>

<kbn-loading-indicator></kbn-loading-indicator>

Expand Down
23 changes: 19 additions & 4 deletions src/ui/public/chrome/directives/kbn_chrome.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import React from 'react';
import ReactDOM from 'react-dom';
import $ from 'jquery';
import { remove } from 'lodash';

Expand All @@ -8,7 +10,7 @@ import {
getUnhashableStatesProvider,
unhashUrl,
} from 'ui/state_management/state_hashing';
import { notify, toastNotifications, createFirstIndexPatternPrompt } from 'ui/notify';
import { notify, GlobalToastList, toastNotifications, createFirstIndexPatternPrompt } from 'ui/notify';
import { SubUrlRouteFilterProvider } from './sub_url_route_filter';

export function kbnChromeProvider(chrome, internals) {
Expand Down Expand Up @@ -69,9 +71,22 @@ export function kbnChromeProvider(chrome, internals) {

// Notifications
$scope.notifList = notify._notifs;
$scope.toastNotifications = toastNotifications.list;
$scope.dismissToast = toastNotifications.remove;
$scope.TOAST_LIFE_TIME_MS = 6000;

const globalToastList = instance => {
toastNotifications.onChange(() => {
instance.forceUpdate();
});
};

ReactDOM.render(
<GlobalToastList
ref={globalToastList}
toasts={toastNotifications.list}
dismissToast={toastNotifications.remove}
toastLifeTimeMs={6000}
/>,
document.getElementById('globalToastList')
);

$scope.createFirstIndexPatternPrompt = createFirstIndexPatternPrompt;

Expand Down
2 changes: 1 addition & 1 deletion src/ui/public/notify/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export { notify } from './notify';
export { Notifier } from './notifier';
export { fatalError, fatalErrorInternals, addFatalErrorCallback } from './fatal_error';
export { toastNotifications } from './toasts';
export { GlobalToastList, toastNotifications } from './toasts';
export { createFirstIndexPatternPrompt } from './create_first_index_pattern_prompt';
12 changes: 0 additions & 12 deletions src/ui/public/notify/toasts/global_toast_list.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import React, {
Component,
} from 'react';
import PropTypes from 'prop-types';
import 'ngreact';
import { uiModules } from 'ui/modules';

import {
EuiGlobalToastList,
Expand Down Expand Up @@ -125,13 +123,3 @@ export class GlobalToastList extends Component {
);
}
}

const app = uiModules.get('app/kibana', ['react']);

app.directive('globalToastList', function (reactDirective) {
return reactDirective(GlobalToastList, [
'toasts',
'toastLifeTimeMs',
['dismissToast', { watchDepth: 'reference' }],
]);
});
2 changes: 1 addition & 1 deletion src/ui/public/notify/toasts/index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
import './global_toast_list';
export { GlobalToastList } from './global_toast_list';
export { toastNotifications } from './toast_notifications';
16 changes: 16 additions & 0 deletions src/ui/public/notify/toasts/toast_notifications.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,40 @@ const normalizeToast = toastOrTitle => {
return toastOrTitle;
};

let onChangeCallback;

export class ToastNotifications {
constructor() {
this.list = [];
this.idCounter = 0;
}

onChange = callback => {
onChangeCallback = callback;
};

add = toastOrTitle => {
const toast = {
id: this.idCounter++,
...normalizeToast(toastOrTitle),
};

this.list.push(toast);

if (onChangeCallback) {
onChangeCallback();
}

return toast;
};

remove = toast => {
const index = this.list.indexOf(toast);
this.list.splice(index, 1);

if (onChangeCallback) {
onChangeCallback();
}
};

addSuccess = toastOrTitle => {
Expand Down
19 changes: 19 additions & 0 deletions src/ui/public/notify/toasts/toast_notifications.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import sinon from 'sinon';

import {
ToastNotifications,
} from './toast_notifications';
Expand Down Expand Up @@ -41,6 +43,23 @@ describe('ToastNotifications', () => {
});
});

describe('onChange method', () => {
test('callback is called when a toast is added', () => {
const onChangeSpy = sinon.spy();
toastNotifications.onChange(onChangeSpy);
toastNotifications.add({});
expect(onChangeSpy.callCount).toBe(1);
});

test('callback is called when a toast is removed', () => {
const onChangeSpy = sinon.spy();
toastNotifications.onChange(onChangeSpy);
const toast = toastNotifications.add({});
toastNotifications.remove(toast);
expect(onChangeSpy.callCount).toBe(2);
});
});

describe('addSuccess method', () => {
test('adds a success toast', () => {
toastNotifications.addSuccess({});
Expand Down
2 changes: 1 addition & 1 deletion test/functional/apps/visualize/_tag_cloud.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export default function ({ getService, getPageObjects }) {
expect(spyToggleExists).to.be(true);
});

it('should show correct tag cloud data', async function () {
it.skip('should show correct tag cloud data', async function () {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be part of this PR?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was flaking out on me so I had to skip it. I recorded this in an issue here: #16383. The alternative would be to do this in master so we get a specific commit for this change, but I don't think that's necessary. What do you think?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm OK with piggybacking it. Small change.

const data = await PageObjects.visualize.getTextTag();
log.debug(data);
expect(data).to.eql([ '32,212,254,720', '21,474,836,480', '20,401,094,656', '19,327,352,832', '18,253,611,008' ]);
Expand Down