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

[Backport 2.x] Adds toast ID to toast api #3868

Merged
merged 3 commits into from
Apr 18, 2023
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
- [Monaco editor] Add json worker support ([#3424](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/3424))
- [Dashboard] Indicate that IE is no longer supported ([#3641](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/3641))
- [Data] Add geo shape filter field ([#3605](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/3605))
- [Notifications] Adds id to toast api for deduplication ([#3752](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/3752))
- [UI] Add support for comma delimiters in the global filter bar ([#3686](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/3686))
- [VisBuilder] Add UI actions handler ([#3732](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/3732))

Expand Down
23 changes: 23 additions & 0 deletions src/core/public/notifications/toasts/toasts_api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,24 @@ describe('#get$()', () => {
toasts.remove('bar');
expect(onToasts).not.toHaveBeenCalled();
});

it('does not emit a new toast list when a toast with the same id is passed to add()', () => {
const toasts = new ToastsApi(toastDeps());
const onToasts = jest.fn();

toasts.get$().subscribe(onToasts);
toasts.add({
id: 'foo',
title: 'foo',
});
onToasts.mockClear();

toasts.add({
id: 'foo',
title: 'bar',
});
expect(onToasts).not.toHaveBeenCalled();
});
});

describe('#add()', () => {
Expand Down Expand Up @@ -135,6 +153,11 @@ describe('#add()', () => {
const toasts = new ToastsApi(toastDeps());
expect(toasts.add('foo')).toHaveProperty('title', 'foo');
});

it('accepts an id and does not auto increment', async () => {
const toasts = new ToastsApi(toastDeps());
expect(toasts.add({ id: 'foo', title: 'not foo' })).toHaveProperty('id', 'foo');
});
});

describe('#remove()', () => {
Expand Down
14 changes: 11 additions & 3 deletions src/core/public/notifications/toasts/toasts_api.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,10 @@ import { I18nStart } from '../../i18n';
/**
* Allowed fields for {@link ToastInput}.
*
* @remarks
* `id` cannot be specified.
*
* @public
*/
export type ToastInputFields = Pick<EuiToast, Exclude<keyof EuiToast, 'id' | 'text' | 'title'>> & {
id?: string;
title?: string | MountPoint;
text?: string | MountPoint;
};
Expand Down Expand Up @@ -143,6 +141,16 @@ export class ToastsApi implements IToasts {
* @returns a {@link Toast}
*/
public add(toastOrTitle: ToastInput) {
if (typeof toastOrTitle !== 'string') {
const toastObject = toastOrTitle;
const list = this.toasts$.getValue();
const existingToast = list.find((toast) => toast.id === toastObject.id);

if (existingToast) {
return existingToast;
}
}

const toast: Toast = {
id: String(this.idCounter++),
toastLifeTimeMs: this.uiSettings.get('notifications:lifetime:info'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,11 @@ export const WorkspaceUI = () => {

const err = schemaValidation.errorMsg || aggValidation.errorMsg;

if (err) toasts.addWarning(err);
if (err)
toasts.addWarning({
id: 'vb_expression_validation',
title: err,
});

return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ export const getPreloadedStore = async (services: VisBuilderServices) => {
};

// the store subscriber will automatically detect changes and call handleChange function
store.subscribe(handleChange);
const unsubscribe = store.subscribe(handleChange);

return store;
return { store, unsubscribe };
};

// Infer the `RootState` and `AppDispatch` types from the store itself
Expand Down
3 changes: 2 additions & 1 deletion src/plugins/vis_builder/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,14 +163,15 @@ export class VisBuilderPlugin
};

// Instantiate the store
const store = await getPreloadedStore(services);
const { store, unsubscribe: unsubscribeStore } = await getPreloadedStore(services);
const unmount = renderApp(params, services, store);

// Render the application
return () => {
unlistenParentHistory();
unmount();
appUnMounted();
unsubscribeStore();
};
},
});
Expand Down