Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: properly set layout on vis type change for all vis types #586

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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