Skip to content

Commit

Permalink
fix: properly set layout on vis type change for all vis types (#586)
Browse files Browse the repository at this point in the history
The adapted layout should be used always when changing vis type, also
for the "default" vis types.
  • Loading branch information
edoardo authored Jan 31, 2020
1 parent 8a40296 commit 489fbf9
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
26 changes: 25 additions & 1 deletion packages/app/src/modules/__tests__/layoutAdapters.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
12 changes: 12 additions & 0 deletions packages/app/src/modules/layoutAdapters.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
9 changes: 8 additions & 1 deletion packages/app/src/modules/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -83,7 +90,7 @@ export const getAdaptedUiByType = ui => {
return singleValueUiAdapter(ui)
}
default:
return ui
return defaultUiAdapter(ui)
}
}

Expand Down

0 comments on commit 489fbf9

Please sign in to comment.