From 0d745db32389ce35f9f0e4e8bd69a0b4d10bf995 Mon Sep 17 00:00:00 2001 From: Stratoula Kalafateli Date: Thu, 20 Oct 2022 11:09:19 +0300 Subject: [PATCH 1/3] [Lens] Fixes bug with removing layer with trendline --- .../lens/public/datasources/form_based/form_based.tsx | 8 ++++++++ .../lens/public/visualizations/metric/visualization.tsx | 3 ++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/x-pack/plugins/lens/public/datasources/form_based/form_based.tsx b/x-pack/plugins/lens/public/datasources/form_based/form_based.tsx index d41218f785d9b..9016fb6ae2cf3 100644 --- a/x-pack/plugins/lens/public/datasources/form_based/form_based.tsx +++ b/x-pack/plugins/lens/public/datasources/form_based/form_based.tsx @@ -219,6 +219,14 @@ export function getFormBasedDatasource({ const newLayers = { ...state.layers }; delete newLayers[layerId]; + // delete layers linked to this layer + Object.keys(newLayers).forEach((id) => { + const linkedLayers = newLayers[id]?.linkToLayers; + if (linkedLayers && linkedLayers.length && linkedLayers.includes(layerId)) { + delete newLayers[id]; + } + }); + return { ...state, layers: newLayers, diff --git a/x-pack/plugins/lens/public/visualizations/metric/visualization.tsx b/x-pack/plugins/lens/public/visualizations/metric/visualization.tsx index 95c9785b2e9fe..123821a62aa69 100644 --- a/x-pack/plugins/lens/public/visualizations/metric/visualization.tsx +++ b/x-pack/plugins/lens/public/visualizations/metric/visualization.tsx @@ -440,9 +440,10 @@ export const getMetricVisualization = ({ return { ...state, trendlineLayerId: layerId, trendlineLayerType: layerType }; }, - removeLayer(state) { + removeLayer(state, layerId) { const newState: MetricVisualizationState = { ...state, + ...(state.layerId === layerId && { metricAccessor: undefined }), trendlineLayerId: undefined, trendlineLayerType: undefined, trendlineMetricAccessor: undefined, From 9cb51db0ae85ed714febcc1300d5bcd050d70af6 Mon Sep 17 00:00:00 2001 From: Stratoula Kalafateli Date: Thu, 20 Oct 2022 11:29:50 +0300 Subject: [PATCH 2/3] Add unit tests --- .../datasources/form_based/form_based.test.ts | 23 +++++++++++++++++++ .../metric/visualization.test.ts | 11 +++++++++ 2 files changed, 34 insertions(+) diff --git a/x-pack/plugins/lens/public/datasources/form_based/form_based.test.ts b/x-pack/plugins/lens/public/datasources/form_based/form_based.test.ts index e8ac68460beb9..3f30dff6fd1a7 100644 --- a/x-pack/plugins/lens/public/datasources/form_based/form_based.test.ts +++ b/x-pack/plugins/lens/public/datasources/form_based/form_based.test.ts @@ -1675,6 +1675,29 @@ describe('IndexPattern Data Source', () => { }, }); }); + + it('should remove linked layers', () => { + const state = { + layers: { + first: { + indexPatternId: '1', + columnOrder: [], + columns: {}, + }, + second: { + indexPatternId: '2', + columnOrder: [], + columns: {}, + linkToLayers: ['first'], + }, + }, + currentIndexPatternId: '1', + }; + expect(FormBasedDatasource.removeLayer(state, 'first')).toEqual({ + ...state, + layers: {}, + }); + }); }); describe('#createEmptyLayer', () => { diff --git a/x-pack/plugins/lens/public/visualizations/metric/visualization.test.ts b/x-pack/plugins/lens/public/visualizations/metric/visualization.test.ts index c6194956c2f0b..2a89ef784492d 100644 --- a/x-pack/plugins/lens/public/visualizations/metric/visualization.test.ts +++ b/x-pack/plugins/lens/public/visualizations/metric/visualization.test.ts @@ -758,6 +758,16 @@ describe('metric visualization', () => { `); }); + it('removes all accessors from a layer', () => { + const chk = visualization.removeLayer!(fullState, 'first'); + expect(chk.metricAccessor).toBeUndefined(); + expect(chk.trendlineLayerId).toBeUndefined(); + expect(chk.trendlineLayerType).toBeUndefined(); + expect(chk.trendlineMetricAccessor).toBeUndefined(); + expect(chk.trendlineTimeAccessor).toBeUndefined(); + expect(chk.trendlineBreakdownByAccessor).toBeUndefined(); + }); + it('appends a trendline layer', () => { const newLayerId = 'new-layer-id'; const chk = visualization.appendLayer!(fullState, newLayerId, 'metricTrendline', ''); @@ -767,6 +777,7 @@ describe('metric visualization', () => { it('removes trendline layer', () => { const chk = visualization.removeLayer!(fullStateWTrend, fullStateWTrend.trendlineLayerId); + expect(chk.metricAccessor).toBe('metric-col-id'); expect(chk.trendlineLayerId).toBeUndefined(); expect(chk.trendlineLayerType).toBeUndefined(); expect(chk.trendlineMetricAccessor).toBeUndefined(); From 9257e77d03335ebbd38dd1ba4e765ab9f3eea6f7 Mon Sep 17 00:00:00 2001 From: Stratoula Kalafateli Date: Thu, 20 Oct 2022 17:03:26 +0300 Subject: [PATCH 3/3] Update x-pack/plugins/lens/public/datasources/form_based/form_based.tsx Co-authored-by: Marco Liberati --- .../plugins/lens/public/datasources/form_based/form_based.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x-pack/plugins/lens/public/datasources/form_based/form_based.tsx b/x-pack/plugins/lens/public/datasources/form_based/form_based.tsx index 9016fb6ae2cf3..62c77ec5225fe 100644 --- a/x-pack/plugins/lens/public/datasources/form_based/form_based.tsx +++ b/x-pack/plugins/lens/public/datasources/form_based/form_based.tsx @@ -222,7 +222,7 @@ export function getFormBasedDatasource({ // delete layers linked to this layer Object.keys(newLayers).forEach((id) => { const linkedLayers = newLayers[id]?.linkToLayers; - if (linkedLayers && linkedLayers.length && linkedLayers.includes(layerId)) { + if (linkedLayers && linkedLayers.includes(layerId)) { delete newLayers[id]; } });