From 489fbf923d38f95f2739927ffe545161fa04d13a Mon Sep 17 00:00:00 2001 From: Edoardo Sabadelli Date: Fri, 31 Jan 2020 14:32:20 +0100 Subject: [PATCH] fix: properly set layout on vis type change for all vis types (#586) The adapted layout should be used always when changing vis type, also for the "default" vis types. --- .../modules/__tests__/layoutAdapters.spec.js | 26 ++++++++++++++++++- packages/app/src/modules/layoutAdapters.js | 12 +++++++++ packages/app/src/modules/ui.js | 9 ++++++- 3 files changed, 45 insertions(+), 2 deletions(-) diff --git a/packages/app/src/modules/__tests__/layoutAdapters.spec.js b/packages/app/src/modules/__tests__/layoutAdapters.spec.js index cde47785ba..3379f40038 100644 --- a/packages/app/src/modules/__tests__/layoutAdapters.spec.js +++ b/packages/app/src/modules/__tests__/layoutAdapters.spec.js @@ -7,11 +7,35 @@ import { DIMENSION_ID_ORGUNIT, } from '@dhis2/analytics' -import { pieLayoutAdapter, yearOverYearLayoutAdapter } from '../layoutAdapters' +import { + defaultLayoutAdapter, + pieLayoutAdapter, + yearOverYearLayoutAdapter, +} from '../layoutAdapters' const someId = 'someId' const otherId = 'otherId' +describe('defaultLayoutAdapter', () => { + it('should move all extra dimensions in columns and rows to filters', () => { + const initialState = { + [AXIS_ID_COLUMNS]: [DIMENSION_ID_DATA, someId], + [AXIS_ID_ROWS]: [DIMENSION_ID_PERIOD, otherId], + [AXIS_ID_FILTERS]: [DIMENSION_ID_ORGUNIT], + } + + const actualState = defaultLayoutAdapter(initialState) + + const expectedState = { + [AXIS_ID_COLUMNS]: [DIMENSION_ID_DATA], + [AXIS_ID_ROWS]: [DIMENSION_ID_PERIOD], + [AXIS_ID_FILTERS]: [DIMENSION_ID_ORGUNIT, someId, otherId], + } + + expect(actualState).toEqual(expectedState) + }) +}) + describe('pieLayoutAdapter', () => { it('should move all column and row dimensions to filter except the first column dimension', () => { const initialState = { diff --git a/packages/app/src/modules/layoutAdapters.js b/packages/app/src/modules/layoutAdapters.js index e4e4959c2d..8264d49280 100644 --- a/packages/app/src/modules/layoutAdapters.js +++ b/packages/app/src/modules/layoutAdapters.js @@ -6,6 +6,18 @@ import { DIMENSION_ID_PERIOD, } from '@dhis2/analytics' +// Transform from ui.layout to default layout format +export const defaultLayoutAdapter = layout => { + const columns = layout[AXIS_ID_COLUMNS].slice() + const rows = layout[AXIS_ID_ROWS].slice() + + return { + [AXIS_ID_COLUMNS]: [columns.shift()], + [AXIS_ID_ROWS]: [rows.shift()], + [AXIS_ID_FILTERS]: [...layout[AXIS_ID_FILTERS], ...columns, ...rows], + } +} + // Transform from ui.layout to pie layout format export const pieLayoutAdapter = layout => { const columns = layout[AXIS_ID_COLUMNS].slice() diff --git a/packages/app/src/modules/ui.js b/packages/app/src/modules/ui.js index 3fe129030b..f65eaae554 100644 --- a/packages/app/src/modules/ui.js +++ b/packages/app/src/modules/ui.js @@ -16,6 +16,7 @@ import { getInverseLayout } from './layout' import { getOptionsFromVisualization } from './options' import { BASE_FIELD_YEARLY_SERIES } from './fields/baseFields' import { + defaultLayoutAdapter, pieLayoutAdapter, yearOverYearLayoutAdapter, singleValueLayoutAdapter, @@ -44,6 +45,12 @@ export const getUiFromVisualization = (vis, currentState = {}) => ({ axes: getIdAxisMap(vis.optionalAxes), }) +// Transform from store.ui to default format +export const defaultUiAdapter = ui => ({ + ...ui, + layout: defaultLayoutAdapter(ui.layout), +}) + // Transform from store.ui to pie format export const pieUiAdapter = ui => ({ ...ui, @@ -83,7 +90,7 @@ export const getAdaptedUiByType = ui => { return singleValueUiAdapter(ui) } default: - return ui + return defaultUiAdapter(ui) } }