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

[VizBuilder] Create a new wizard directly on a dashboard #2384

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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)

* Add updated_at column to objects' tables ([#1218](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/1218))
* [Viz Builder] State validation before dispatching and loading ([#2351](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/2351))
* [Viz Builder] Create a new wizard directly on a dashboard ([#2384](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/2384))

### 🐛 Bug Fixes

Expand Down
36 changes: 21 additions & 15 deletions src/plugins/wizard/public/application/components/top_nav.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

import React, { useMemo } from 'react';
import React, { useEffect, useState } from 'react';
import { useParams } from 'react-router-dom';
import { useUnmount } from 'react-use';
import { PLUGIN_ID } from '../../../common';
Expand All @@ -17,6 +17,7 @@ import { useTypedSelector, useTypedDispatch } from '../utils/state_management';
import { setEditorState } from '../utils/state_management/metadata_slice';
import { useCanSave } from '../utils/use/use_can_save';
import { saveStateToSavedObject } from '../../saved_visualizations/transforms';
import { TopNavMenuData } from '../../../../navigation/public';

export const TopNav = () => {
// id will only be set for the edit route
Expand All @@ -34,27 +35,32 @@ export const TopNav = () => {
const saveDisabledReason = useCanSave();
const savedWizardVis = useSavedWizardVis(visualizationIdFromUrl);
const { selected: indexPattern } = useIndexPatterns();
const [config, setConfig] = useState<TopNavMenuData[] | undefined>();

const config = useMemo(() => {
if (!savedWizardVis || !indexPattern) return;
useEffect(() => {
abbyhu2000 marked this conversation as resolved.
Show resolved Hide resolved
const getConfig = () => {
if (!savedWizardVis || !indexPattern) return;

return getTopNavConfig(
{
visualizationIdFromUrl,
savedWizardVis: saveStateToSavedObject(savedWizardVis, rootState, indexPattern),
saveDisabledReason,
dispatch,
},
services
);
return getTopNavConfig(
{
visualizationIdFromUrl,
savedWizardVis: saveStateToSavedObject(savedWizardVis, rootState, indexPattern),
saveDisabledReason,
dispatch,
},
services
);
};

setConfig(getConfig());
}, [
rootState,
savedWizardVis,
services,
visualizationIdFromUrl,
rootState,
indexPattern,
saveDisabledReason,
dispatch,
services,
indexPattern,
]);

// reset validity before component destroyed
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

import { WizardServices } from '../../types';
import { getOnSave } from './get_top_nav_config';
import { createWizardServicesMock } from './mocks';

describe('getOnSave', () => {
abbyhu2000 marked this conversation as resolved.
Show resolved Hide resolved
let savedWizardVis: any;
let originatingApp: string | undefined;
let visualizationIdFromUrl: string;
let dispatch: any;
let mockServices: jest.Mocked<WizardServices>;
let onSaveProps: {
newTitle: string;
newCopyOnSave: boolean;
isTitleDuplicateConfirmed: boolean;
onTitleDuplicate: any;
newDescription: string;
returnToOrigin: boolean;
};

beforeEach(() => {
savedWizardVis = {
id: '1',
title: 'save wizard wiz title',
description: '',
visualizationState: '',
styleState: '',
version: 0,
copyOnSave: true,
searchSourceFields: {},
save: jest.fn().mockReturnValue('1'),
};
Comment on lines +26 to +36
Copy link
Member

Choose a reason for hiding this comment

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

I wonder if there's some existing way to automock saved objects from their definitions... this definitely works, but seems like something you shouldn't really have to do.

originatingApp = '';
visualizationIdFromUrl = '';
dispatch = jest.fn();
mockServices = createWizardServicesMock();

onSaveProps = {
newTitle: 'new title',
newCopyOnSave: false,
isTitleDuplicateConfirmed: false,
onTitleDuplicate: jest.fn(),
newDescription: 'new description',
returnToOrigin: true,
};
});

test('return undefined when savedWizardVis is null', async () => {
savedWizardVis = null;
const onSave = getOnSave(
savedWizardVis,
originatingApp,
visualizationIdFromUrl,
dispatch,
mockServices
);
const onSaveResult = await onSave(onSaveProps);

expect(onSaveResult).toBeUndefined();
});

test('savedWizardVis get saved correctly', async () => {
const onSave = getOnSave(
savedWizardVis,
originatingApp,
visualizationIdFromUrl,
dispatch,
mockServices
);
const onSaveReturn = await onSave(onSaveProps);
expect(savedWizardVis).toMatchInlineSnapshot(`
Object {
"copyOnSave": false,
"description": "new description",
"id": "1",
"save": [MockFunction] {
"calls": Array [
Array [
Object {
"confirmOverwrite": false,
"isTitleDuplicateConfirmed": false,
"onTitleDuplicate": [MockFunction],
"returnToOrigin": true,
},
],
],
"results": Array [
Object {
"type": "return",
"value": "1",
},
],
},
"searchSourceFields": Object {},
"styleState": "",
"title": "new title",
"version": 0,
"visualizationState": "",
}
`);
expect(onSaveReturn?.id).toBe('1');
});

test('savedWizardVis does not change title with a null id', async () => {
savedWizardVis.save = jest.fn().mockReturnValue(null);
const onSave = getOnSave(
savedWizardVis,
originatingApp,
visualizationIdFromUrl,
dispatch,
mockServices
);
const onSaveResult = await onSave(onSaveProps);
expect(savedWizardVis.title).toBe('save wizard wiz title');
expect(onSaveResult?.id).toBeNull();
});

test('create a new wizard from dashboard', async () => {
savedWizardVis.id = undefined;
savedWizardVis.save = jest.fn().mockReturnValue('2');
originatingApp = 'dashboard';
onSaveProps.returnToOrigin = true;

const onSave = getOnSave(
savedWizardVis,
originatingApp,
visualizationIdFromUrl,
dispatch,
mockServices
);
const onSaveResult = await onSave(onSaveProps);
expect(onSaveResult?.id).toBe('2');
expect(dispatch).toBeCalledTimes(0);
});

test('edit an exising wizard from dashboard', async () => {
savedWizardVis.copyOnSave = false;
onSaveProps.newDescription = 'new description after editing';
originatingApp = 'dashboard';
onSaveProps.returnToOrigin = true;
const onSave = getOnSave(
savedWizardVis,
originatingApp,
visualizationIdFromUrl,
dispatch,
mockServices
);
const onSaveResult = await onSave(onSaveProps);
expect(onSaveResult?.id).toBe('1');
expect(mockServices.application.navigateToApp).toBeCalledTimes(1);
abbyhu2000 marked this conversation as resolved.
Show resolved Hide resolved
expect(savedWizardVis.description).toBe('new description after editing');
});
});
Loading