From 9cb6f34c626b979f3099cc140bade00408500b9f Mon Sep 17 00:00:00 2001 From: Marta Bondyra Date: Fri, 1 Oct 2021 10:07:54 +0200 Subject: [PATCH] make switchVisualization and selectSuggestion one reducer as it's very similar --- .../lens/public/app_plugin/app.test.tsx | 2 +- .../editor_frame/editor_frame.tsx | 2 +- .../editor_frame/suggestion_helpers.ts | 22 ++--- .../editor_frame/suggestion_panel.test.tsx | 13 +-- .../workspace_panel/chart_switch.test.tsx | 62 +++++++----- .../workspace_panel/chart_switch.tsx | 2 +- .../workspace_panel/workspace_panel.test.tsx | 11 ++- .../workspace_panel/workspace_panel.tsx | 2 +- .../lens/public/state_management/index.ts | 1 - .../state_management/lens_slice.test.ts | 18 ++-- .../public/state_management/lens_slice.ts | 98 +++++++------------ 11 files changed, 113 insertions(+), 120 deletions(-) diff --git a/x-pack/plugins/lens/public/app_plugin/app.test.tsx b/x-pack/plugins/lens/public/app_plugin/app.test.tsx index 222fca77081e1..01244f963e0a6 100644 --- a/x-pack/plugins/lens/public/app_plugin/app.test.tsx +++ b/x-pack/plugins/lens/public/app_plugin/app.test.tsx @@ -343,7 +343,7 @@ describe('Lens App', () => { ); }); it('handles rejected index pattern', async () => { - const customServices = makeDefaultServices(sessionIdSubject); + const customServices = makeDefaultServices(sessionIdSubject, 'sessionId-1'); customServices.data.indexPatterns.get = jest .fn() .mockImplementation((id) => Promise.reject({ reason: 'Could not locate that data view' })); diff --git a/x-pack/plugins/lens/public/editor_frame_service/editor_frame/editor_frame.tsx b/x-pack/plugins/lens/public/editor_frame_service/editor_frame/editor_frame.tsx index 3b55c4923f967..c68c04b4b3e21 100644 --- a/x-pack/plugins/lens/public/editor_frame_service/editor_frame/editor_frame.tsx +++ b/x-pack/plugins/lens/public/editor_frame_service/editor_frame/editor_frame.tsx @@ -76,7 +76,7 @@ export function EditorFrame(props: EditorFrameProps) { const suggestion = getSuggestionForField.current!(field); if (suggestion) { trackUiEvent('drop_onto_workspace'); - switchToSuggestion(dispatchLens, suggestion, 'SWITCH_VISUALIZATION'); + switchToSuggestion(dispatchLens, suggestion, true); } }, [getSuggestionForField, dispatchLens] diff --git a/x-pack/plugins/lens/public/editor_frame_service/editor_frame/suggestion_helpers.ts b/x-pack/plugins/lens/public/editor_frame_service/editor_frame/suggestion_helpers.ts index eb2dac40b3d9a..7329e0e037c51 100644 --- a/x-pack/plugins/lens/public/editor_frame_service/editor_frame/suggestion_helpers.ts +++ b/x-pack/plugins/lens/public/editor_frame_service/editor_frame/suggestion_helpers.ts @@ -25,7 +25,6 @@ import { LayerType, layerTypes } from '../../../common'; import { getLayerType } from './config_panel/add_layer'; import { LensDispatch, - selectSuggestion, switchVisualization, DatasourceStates, VisualizationState, @@ -227,19 +226,18 @@ export function switchToSuggestion( Suggestion, 'visualizationId' | 'visualizationState' | 'datasourceState' | 'datasourceId' >, - type: 'SWITCH_VISUALIZATION' | 'SELECT_SUGGESTION' = 'SELECT_SUGGESTION' + clearStagedPreview?: boolean ) { - const pickedSuggestion = { - newVisualizationId: suggestion.visualizationId, - initialState: suggestion.visualizationState, - datasourceState: suggestion.datasourceState, - datasourceId: suggestion.datasourceId!, - }; - dispatchLens( - type === 'SELECT_SUGGESTION' - ? selectSuggestion(pickedSuggestion) - : switchVisualization(pickedSuggestion) + switchVisualization({ + suggestion: { + newVisualizationId: suggestion.visualizationId, + visualizationState: suggestion.visualizationState, + datasourceState: suggestion.datasourceState, + datasourceId: suggestion.datasourceId!, + }, + clearStagedPreview, + }) ); } diff --git a/x-pack/plugins/lens/public/editor_frame_service/editor_frame/suggestion_panel.test.tsx b/x-pack/plugins/lens/public/editor_frame_service/editor_frame/suggestion_panel.test.tsx index b63d2956cfe6b..26e0be3555714 100644 --- a/x-pack/plugins/lens/public/editor_frame_service/editor_frame/suggestion_panel.test.tsx +++ b/x-pack/plugins/lens/public/editor_frame_service/editor_frame/suggestion_panel.test.tsx @@ -214,16 +214,17 @@ describe('suggestion_panel', () => { act(() => { instance.find('button[data-test-subj="lnsSuggestion"]').at(1).simulate('click'); }); - // instance.update(); expect(lensStore.dispatch).toHaveBeenCalledWith( expect.objectContaining({ - type: 'lens/selectSuggestion', + type: 'lens/switchVisualization', payload: { - datasourceId: undefined, - datasourceState: {}, - initialState: { suggestion1: true }, - newVisualizationId: 'testVis', + suggestion: { + datasourceId: undefined, + datasourceState: {}, + visualizationState: { suggestion1: true }, + newVisualizationId: 'testVis', + }, }, }) ); diff --git a/x-pack/plugins/lens/public/editor_frame_service/editor_frame/workspace_panel/chart_switch.test.tsx b/x-pack/plugins/lens/public/editor_frame_service/editor_frame/workspace_panel/chart_switch.test.tsx index e7abf291b6eba..7cb97882a5e03 100644 --- a/x-pack/plugins/lens/public/editor_frame_service/editor_frame/workspace_panel/chart_switch.test.tsx +++ b/x-pack/plugins/lens/public/editor_frame_service/editor_frame/workspace_panel/chart_switch.test.tsx @@ -200,10 +200,13 @@ describe('chart_switch', () => { expect(lensStore.dispatch).toHaveBeenCalledWith({ type: 'lens/switchVisualization', payload: { - initialState: 'suggestion visB', - newVisualizationId: 'visB', - datasourceId: 'testDatasource', - datasourceState: {}, + suggestion: { + visualizationState: 'suggestion visB', + newVisualizationId: 'visB', + datasourceId: 'testDatasource', + datasourceState: {}, + }, + clearStagedPreview: true, }, }); }); @@ -238,8 +241,11 @@ describe('chart_switch', () => { expect(lensStore.dispatch).toHaveBeenCalledWith({ type: 'lens/switchVisualization', payload: { - initialState: 'visB initial state', - newVisualizationId: 'visB', + suggestion: { + visualizationState: 'visB initial state', + newVisualizationId: 'visB', + }, + clearStagedPreview: true, }, }); expect(lensStore.dispatch).toHaveBeenCalledWith({ @@ -522,10 +528,13 @@ describe('chart_switch', () => { expect(lensStore.dispatch).toHaveBeenCalledWith({ type: 'lens/switchVisualization', payload: { - datasourceId: undefined, - datasourceState: undefined, - initialState: 'visB initial state', - newVisualizationId: 'visB', + suggestion: { + datasourceId: undefined, + datasourceState: undefined, + visualizationState: 'visB initial state', + newVisualizationId: 'visB', + }, + clearStagedPreview: true, }, }); }); @@ -598,10 +607,13 @@ describe('chart_switch', () => { expect(lensStore.dispatch).toHaveBeenCalledWith({ type: 'lens/switchVisualization', payload: { - datasourceId: 'testDatasource', - datasourceState: {}, - initialState: 'switched', - newVisualizationId: 'visC', + suggestion: { + datasourceId: 'testDatasource', + datasourceState: {}, + visualizationState: 'switched', + newVisualizationId: 'visC', + }, + clearStagedPreview: true, }, }); expect(datasourceMap.testDatasource.removeLayer).not.toHaveBeenCalled(); @@ -694,10 +706,13 @@ describe('chart_switch', () => { expect(lensStore.dispatch).toHaveBeenCalledWith({ type: 'lens/switchVisualization', payload: { - newVisualizationId: 'visB', - datasourceId: 'testDatasource', - datasourceState: 'testDatasource suggestion', - initialState: 'suggestion visB', + suggestion: { + newVisualizationId: 'visB', + datasourceId: 'testDatasource', + datasourceState: 'testDatasource suggestion', + visualizationState: 'suggestion visB', + }, + clearStagedPreview: true, }, }); }); @@ -731,10 +746,13 @@ describe('chart_switch', () => { expect(lensStore.dispatch).toHaveBeenCalledWith({ type: 'lens/switchVisualization', payload: { - initialState: 'suggestion visB visB', - newVisualizationId: 'visB', - datasourceId: 'testDatasource', - datasourceState: {}, + suggestion: { + visualizationState: 'suggestion visB visB', + newVisualizationId: 'visB', + datasourceId: 'testDatasource', + datasourceState: {}, + }, + clearStagedPreview: true, }, }); }); diff --git a/x-pack/plugins/lens/public/editor_frame_service/editor_frame/workspace_panel/chart_switch.tsx b/x-pack/plugins/lens/public/editor_frame_service/editor_frame/workspace_panel/chart_switch.tsx index 51d4f2955a52b..a5ba12941cf7f 100644 --- a/x-pack/plugins/lens/public/editor_frame_service/editor_frame/workspace_panel/chart_switch.tsx +++ b/x-pack/plugins/lens/public/editor_frame_service/editor_frame/workspace_panel/chart_switch.tsx @@ -166,7 +166,7 @@ export const ChartSwitch = memo(function ChartSwitch(props: Props) { ...selection, visualizationState: selection.getVisualizationState(), }, - 'SWITCH_VISUALIZATION' + true ); if ( diff --git a/x-pack/plugins/lens/public/editor_frame_service/editor_frame/workspace_panel/workspace_panel.test.tsx b/x-pack/plugins/lens/public/editor_frame_service/editor_frame/workspace_panel/workspace_panel.test.tsx index 4c866a81e0d63..2ed65d3b0f146 100644 --- a/x-pack/plugins/lens/public/editor_frame_service/editor_frame/workspace_panel/workspace_panel.test.tsx +++ b/x-pack/plugins/lens/public/editor_frame_service/editor_frame/workspace_panel/workspace_panel.test.tsx @@ -806,10 +806,13 @@ describe('workspace_panel', () => { expect(lensStore.dispatch).toHaveBeenCalledWith({ type: 'lens/switchVisualization', payload: { - newVisualizationId: 'testVis', - initialState: {}, - datasourceState: {}, - datasourceId: 'testDatasource', + suggestion: { + newVisualizationId: 'testVis', + visualizationState: {}, + datasourceState: {}, + datasourceId: 'testDatasource', + }, + clearStagedPreview: true, }, }); }); diff --git a/x-pack/plugins/lens/public/editor_frame_service/editor_frame/workspace_panel/workspace_panel.tsx b/x-pack/plugins/lens/public/editor_frame_service/editor_frame/workspace_panel/workspace_panel.tsx index c34e3c4137368..e4816870b4380 100644 --- a/x-pack/plugins/lens/public/editor_frame_service/editor_frame/workspace_panel/workspace_panel.tsx +++ b/x-pack/plugins/lens/public/editor_frame_service/editor_frame/workspace_panel/workspace_panel.tsx @@ -275,7 +275,7 @@ export const InnerWorkspacePanel = React.memo(function InnerWorkspacePanel({ if (suggestionForDraggedField) { trackUiEvent('drop_onto_workspace'); trackUiEvent(expressionExists ? 'drop_non_empty' : 'drop_empty'); - switchToSuggestion(dispatchLens, suggestionForDraggedField, 'SWITCH_VISUALIZATION'); + switchToSuggestion(dispatchLens, suggestionForDraggedField, true); } }, [suggestionForDraggedField, expressionExists, dispatchLens]); diff --git a/x-pack/plugins/lens/public/state_management/index.ts b/x-pack/plugins/lens/public/state_management/index.ts index 938df65ce924b..cc83cc612f32d 100644 --- a/x-pack/plugins/lens/public/state_management/index.ts +++ b/x-pack/plugins/lens/public/state_management/index.ts @@ -27,7 +27,6 @@ export const { updateVisualizationState, updateLayer, switchVisualization, - selectSuggestion, rollbackSuggestion, submitSuggestion, switchDatasource, diff --git a/x-pack/plugins/lens/public/state_management/lens_slice.test.ts b/x-pack/plugins/lens/public/state_management/lens_slice.test.ts index b667833187601..7d88e6ceb616c 100644 --- a/x-pack/plugins/lens/public/state_management/lens_slice.test.ts +++ b/x-pack/plugins/lens/public/state_management/lens_slice.test.ts @@ -75,8 +75,11 @@ describe('lensSlice', () => { const newVisState = {}; store.dispatch( switchVisualization({ - newVisualizationId: 'testVis2', - initialState: newVisState, + suggestion: { + newVisualizationId: 'testVis2', + visualizationState: newVisState, + }, + clearStagedPreview: true, }) ); @@ -89,10 +92,13 @@ describe('lensSlice', () => { store.dispatch( switchVisualization({ - newVisualizationId: 'testVis2', - initialState: newVisState, - datasourceState: newDatasourceState, - datasourceId: 'testDatasource', + suggestion: { + newVisualizationId: 'testVis2', + visualizationState: newVisState, + datasourceState: newDatasourceState, + datasourceId: 'testDatasource', + }, + clearStagedPreview: true, }) ); diff --git a/x-pack/plugins/lens/public/state_management/lens_slice.ts b/x-pack/plugins/lens/public/state_management/lens_slice.ts index cda6a999640cc..0461070020055 100644 --- a/x-pack/plugins/lens/public/state_management/lens_slice.ts +++ b/x-pack/plugins/lens/public/state_management/lens_slice.ts @@ -99,18 +99,14 @@ export const updateLayer = createAction<{ }>('lens/updateLayer'); export const switchVisualization = createAction<{ - newVisualizationId: string; - initialState: unknown; - datasourceState?: unknown; - datasourceId?: string; + suggestion: { + newVisualizationId: string; + visualizationState: unknown; + datasourceState?: unknown; + datasourceId?: string; + }; + clearStagedPreview?: boolean; }>('lens/switchVisualization'); - -export const selectSuggestion = createAction<{ - newVisualizationId: string; - initialState: unknown; - datasourceState?: unknown; - datasourceId?: string; -}>('lens/selectSuggestion'); export const rollbackSuggestion = createAction('lens/rollbackSuggestion'); export const setToggleFullscreen = createAction('lens/setToggleFullscreen'); export const submitSuggestion = createAction('lens/submitSuggestion'); @@ -149,7 +145,6 @@ export const lensActions = { rollbackSuggestion, setToggleFullscreen, submitSuggestion, - selectSuggestion, switchDatasource, navigateAway, loadInitial, @@ -281,67 +276,40 @@ export const makeLensReducer = (storeDeps: LensStoreDeps) => { payload, }: { payload: { - newVisualizationId: string; - initialState: unknown; - datasourceState?: unknown; - datasourceId?: string; - }; - } - ) => { - return { - ...state, - datasourceStates: - 'datasourceId' in payload && payload.datasourceId - ? { - ...state.datasourceStates, - [payload.datasourceId]: { - ...state.datasourceStates[payload.datasourceId], - state: payload.datasourceState, - }, - } - : state.datasourceStates, - visualization: { - ...state.visualization, - activeId: payload.newVisualizationId, - state: payload.initialState, - }, - stagedPreview: undefined, - }; - }, - [selectSuggestion.type]: ( - state, - { - payload, - }: { - payload: { - newVisualizationId: string; - initialState: unknown; - datasourceState: unknown; - datasourceId: string; + suggestion: { + newVisualizationId: string; + visualizationState: unknown; + datasourceState?: unknown; + datasourceId?: string; + }; + clearStagedPreview?: boolean; }; } ) => { + const { newVisualizationId, visualizationState, datasourceState, datasourceId } = + payload.suggestion; return { ...state, - datasourceStates: - 'datasourceId' in payload && payload.datasourceId - ? { - ...state.datasourceStates, - [payload.datasourceId]: { - ...state.datasourceStates[payload.datasourceId], - state: payload.datasourceState, - }, - } - : state.datasourceStates, + datasourceStates: datasourceId + ? { + ...state.datasourceStates, + [datasourceId]: { + ...state.datasourceStates[datasourceId], + state: datasourceState, + }, + } + : state.datasourceStates, visualization: { ...state.visualization, - activeId: payload.newVisualizationId, - state: payload.initialState, - }, - stagedPreview: state.stagedPreview || { - datasourceStates: state.datasourceStates, - visualization: state.visualization, + activeId: newVisualizationId, + state: visualizationState, }, + stagedPreview: payload.clearStagedPreview + ? undefined + : state.stagedPreview || { + datasourceStates: state.datasourceStates, + visualization: state.visualization, + }, }; }, [rollbackSuggestion.type]: (state) => {