Skip to content

Commit

Permalink
make switchVisualization and selectSuggestion one reducer as it's ver…
Browse files Browse the repository at this point in the history
…y similar
  • Loading branch information
mbondyra committed Oct 1, 2021
1 parent 7e24804 commit 9cb6f34
Show file tree
Hide file tree
Showing 11 changed files with 113 additions and 120 deletions.
2 changes: 1 addition & 1 deletion x-pack/plugins/lens/public/app_plugin/app.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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' }));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import { LayerType, layerTypes } from '../../../common';
import { getLayerType } from './config_panel/add_layer';
import {
LensDispatch,
selectSuggestion,
switchVisualization,
DatasourceStates,
VisualizationState,
Expand Down Expand Up @@ -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,
})
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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',
},
},
})
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
});
});
Expand Down Expand Up @@ -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({
Expand Down Expand Up @@ -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,
},
});
});
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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,
},
});
});
Expand Down Expand Up @@ -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,
},
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ export const ChartSwitch = memo(function ChartSwitch(props: Props) {
...selection,
visualizationState: selection.getVisualizationState(),
},
'SWITCH_VISUALIZATION'
true
);

if (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]);

Expand Down
1 change: 0 additions & 1 deletion x-pack/plugins/lens/public/state_management/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ export const {
updateVisualizationState,
updateLayer,
switchVisualization,
selectSuggestion,
rollbackSuggestion,
submitSuggestion,
switchDatasource,
Expand Down
18 changes: 12 additions & 6 deletions x-pack/plugins/lens/public/state_management/lens_slice.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,11 @@ describe('lensSlice', () => {
const newVisState = {};
store.dispatch(
switchVisualization({
newVisualizationId: 'testVis2',
initialState: newVisState,
suggestion: {
newVisualizationId: 'testVis2',
visualizationState: newVisState,
},
clearStagedPreview: true,
})
);

Expand All @@ -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,
})
);

Expand Down
98 changes: 33 additions & 65 deletions x-pack/plugins/lens/public/state_management/lens_slice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void>('lens/rollbackSuggestion');
export const setToggleFullscreen = createAction<void>('lens/setToggleFullscreen');
export const submitSuggestion = createAction<void>('lens/submitSuggestion');
Expand Down Expand Up @@ -149,7 +145,6 @@ export const lensActions = {
rollbackSuggestion,
setToggleFullscreen,
submitSuggestion,
selectSuggestion,
switchDatasource,
navigateAway,
loadInitial,
Expand Down Expand Up @@ -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) => {
Expand Down

0 comments on commit 9cb6f34

Please sign in to comment.