From 8a9377ed69fa0ca212d8a79b138497e2e640f655 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20Obermu=CC=88ller?= Date: Fri, 13 Oct 2023 15:21:58 +0200 Subject: [PATCH] simplify update actions --- .../insights/insightVizDataLogic.test.ts | 3 +- .../scenes/insights/insightVizDataLogic.ts | 53 ++++++------------- 2 files changed, 17 insertions(+), 39 deletions(-) diff --git a/frontend/src/scenes/insights/insightVizDataLogic.test.ts b/frontend/src/scenes/insights/insightVizDataLogic.test.ts index dace8acbd5fba..55e98f7dec9cb 100644 --- a/frontend/src/scenes/insights/insightVizDataLogic.test.ts +++ b/frontend/src/scenes/insights/insightVizDataLogic.test.ts @@ -222,7 +222,7 @@ describe('insightVizDataLogic', () => { }) }) - it('updates insight filter for other insight query kinds', () => { + it('updates the insight filter for other insight query kinds', () => { builtInsightVizDataLogic.actions.updateQuerySource(funnelsQueryDefault) expectLogic(builtInsightDataLogic, () => { @@ -238,6 +238,7 @@ describe('insightVizDataLogic', () => { ...funnelsQueryDefault.funnelsFilter, layout: FunnelLayout.horizontal, }, + trendsFilter: {}, // we currently don't remove insight filters of previous query kinds }, }, }) diff --git a/frontend/src/scenes/insights/insightVizDataLogic.ts b/frontend/src/scenes/insights/insightVizDataLogic.ts index aa014269d97c5..df725a1896e78 100644 --- a/frontend/src/scenes/insights/insightVizDataLogic.ts +++ b/frontend/src/scenes/insights/insightVizDataLogic.ts @@ -95,6 +95,11 @@ export const insightVizDataLogic = kea([ (s) => [s.query], (query) => (isNodeWithSource(query) && isInsightQueryNode(query.source) ? query.source : null), ], + localQuerySource: [ + (s) => [s.querySource, s.filterTestAccountsDefault], + (querySource, filterTestAccountsDefault) => + querySource ? querySource : queryFromKind(NodeKind.TrendsQuery, filterTestAccountsDefault).source, + ], isTrends: [(s) => [s.querySource], (q) => isTrendsQuery(q)], isFunnels: [(s) => [s.querySource], (q) => isFunnelsQuery(q)], @@ -191,53 +196,25 @@ export const insightVizDataLogic = kea([ listeners(({ actions, values, props }) => ({ updateDateRange: ({ dateRange }) => { - const localQuerySource = values.querySource - ? values.querySource - : queryFromKind(NodeKind.TrendsQuery, values.filterTestAccountsDefault).source - if (isInsightQueryNode(localQuerySource)) { - const newQuerySource = { ...localQuerySource, dateRange } - actions.updateQuerySource(newQuerySource) - } + actions.updateQuerySource({ dateRange: { ...values.dateRange, ...dateRange } }) }, updateBreakdown: ({ breakdown }) => { - const localQuerySource = values.querySource - ? values.querySource - : queryFromKind(NodeKind.TrendsQuery, values.filterTestAccountsDefault).source - if (isInsightQueryNode(localQuerySource)) { - const newQuerySource = { - ...localQuerySource, - breakdown: { ...(localQuerySource as TrendsQuery).breakdown, ...breakdown }, - } - actions.updateQuerySource(newQuerySource) - } + actions.updateQuerySource({ breakdown: { ...values.breakdown, ...breakdown } } as Partial) }, updateInsightFilter: ({ insightFilter }) => { - const localQuerySource = values.querySource - ? values.querySource - : queryFromKind(NodeKind.TrendsQuery, values.filterTestAccountsDefault).source - if (isInsightQueryNode(localQuerySource)) { - const filterProperty = filterPropertyForQuery(localQuerySource) - const newQuerySource = { ...localQuerySource } - newQuerySource[filterProperty] = { - ...localQuerySource[filterProperty], - ...insightFilter, - } - actions.updateQuerySource(newQuerySource) - } + const filterProperty = filterPropertyForQuery(values.localQuerySource) + actions.updateQuerySource({ + [filterProperty]: { ...values.localQuerySource[filterProperty], ...insightFilter }, + }) }, updateDisplay: ({ display }) => { actions.updateInsightFilter({ display }) }, updateQuerySource: ({ querySource }) => { - const localQuery = values.query - ? values.query - : queryFromKind(NodeKind.TrendsQuery, values.filterTestAccountsDefault) - if (localQuery && isInsightVizNode(localQuery)) { - actions.setQuery({ - ...localQuery, - source: { ...(localQuery as InsightVizNode).source, ...querySource }, - } as Node) - } + actions.setQuery({ + ...values.query, + source: { ...values.querySource, ...querySource }, + } as Node) }, setQuery: ({ query }) => { if (isInsightVizNode(query)) {