Skip to content

Commit

Permalink
fix: avoid visualization plugin double render (#1665)
Browse files Browse the repository at this point in the history
* refactor: remove unused action

* fix: pass the same userSettings object to VisualizationPlugin props

This avoids a double render issue which causes the Highcharts config
generation to run twice.

* refactor: list all used props in the deconstructor

* fix: init id as null to avoid rerender when not passed

This happened in dashboards where id is not passed to ChartPlugin
because the re-render on resize with no animation is only needed in DV.

Co-authored-by: Martin Ohlson <martin@moid.se>
  • Loading branch information
2 people authored and janhenrikoverland committed Mar 31, 2021
1 parent 43c4842 commit 88bd9db
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 39 deletions.
7 changes: 1 addition & 6 deletions packages/app/src/actions/settings.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import { SET_SETTINGS, ADD_SETTINGS } from '../reducers/settings'
import { ADD_SETTINGS } from '../reducers/settings'
import { apiFetchSystemSettings } from '../api/settings'
import { apiFetchOrganisationUnitRoots } from '@dhis2/analytics'

export const acSetSettings = value => ({
type: SET_SETTINGS,
value,
})

export const acAddSettings = value => ({
type: ADD_SETTINGS,
value,
Expand Down
28 changes: 20 additions & 8 deletions packages/app/src/components/Visualization/Visualization.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,14 +155,21 @@ export class Visualization extends Component {
}

render() {
const { visualization, visFilters, error } = this.props
const {
visualization,
visFilters,
userSettings,
error,
isLoading,
onLoadingComplete,
} = this.props
const { renderId } = this.state

return !visualization || error ? (
<StartScreen />
) : (
<Fragment>
{this.props.isLoading ? (
{isLoading ? (
<div style={styles.loadingCover}>
<LoadingMask />
</div>
Expand All @@ -172,14 +179,12 @@ export class Visualization extends Component {
visualization={visualization}
filters={visFilters}
onChartGenerated={this.onChartGenerated}
onLoadingComplete={this.props.onLoadingComplete}
onLoadingComplete={onLoadingComplete}
onResponsesReceived={this.onResponsesReceived}
onError={this.onError}
onDrill={this.onDrill}
style={styles.chartCanvas}
userSettings={{
displayProperty: this.props.displayProperty,
}}
userSettings={userSettings}
/>
</Fragment>
)
Expand All @@ -189,14 +194,14 @@ export class Visualization extends Component {
Visualization.propTypes = {
addMetadata: PropTypes.func,
addParentGraphMap: PropTypes.func,
displayProperty: PropTypes.string,
error: PropTypes.object,
isLoading: PropTypes.bool,
rightSidebarOpen: PropTypes.bool,
setChart: PropTypes.func,
setCurrent: PropTypes.func,
setLoadError: PropTypes.func,
setUiItems: PropTypes.func,
userSettings: PropTypes.object,
visFilters: PropTypes.object,
visualization: PropTypes.object,
onLoadingComplete: PropTypes.func,
Expand All @@ -216,13 +221,20 @@ export const visFiltersSelector = createSelector(
: {}
)

export const userSettingsSelector = createSelector(
[sGetSettingsDisplayNameProperty],
displayProperty => ({
displayProperty,
})
)

const mapStateToProps = state => ({
visualization: visualizationSelector(state),
visFilters: visFiltersSelector(state),
rightSidebarOpen: sGetUiRightSidebarOpen(state),
error: sGetLoadError(state),
isLoading: sGetIsPluginLoading(state),
displayProperty: sGetSettingsDisplayNameProperty(state),
userSettings: userSettingsSelector(state),
})

const mapDispatchToProps = dispatch => ({
Expand Down
22 changes: 1 addition & 21 deletions packages/app/src/reducers/__tests__/settings.spec.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import reducer, {
DEFAULT_SETTINGS,
SET_SETTINGS,
ADD_SETTINGS,
} from '../settings'
import reducer, { DEFAULT_SETTINGS, ADD_SETTINGS } from '../settings'

describe('reducer: settings', () => {
const currentState = {
Expand All @@ -16,22 +12,6 @@ describe('reducer: settings', () => {
expect(actualState).toEqual(DEFAULT_SETTINGS)
})

it(`${SET_SETTINGS}: should set settings`, () => {
const stateToSet = {
keyAnalysisRelativePeriod: 'LAST_4_QUARTERS',
keyAnalysisDisplayProperty: 'shortName',
}

const actualState = reducer(currentState, {
type: SET_SETTINGS,
value: stateToSet,
})

const expectedState = stateToSet

expect(actualState).toEqual(expectedState)
})

it(`${ADD_SETTINGS}: should add settings`, () => {
const stateToAdd = {
keyAnalysisDisplayProperty: 'shortName',
Expand Down
4 changes: 0 additions & 4 deletions packages/app/src/reducers/settings.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export const SET_SETTINGS = 'SET_SETTINGS'
export const ADD_SETTINGS = 'ADD_SETTINGS'

export const DEFAULT_SETTINGS = {
Expand All @@ -13,9 +12,6 @@ export const DEFAULT_SETTINGS = {

export default (state = DEFAULT_SETTINGS, action) => {
switch (action.type) {
case SET_SETTINGS: {
return Object.assign({}, action.value)
}
case ADD_SETTINGS: {
return {
...state,
Expand Down
1 change: 1 addition & 0 deletions packages/plugin/src/ChartPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ ChartPlugin.defaultProps = {
filters: {},
style: {},
animation: 200,
id: null,
onChartGenerated: Function.prototype,
}

Expand Down

0 comments on commit 88bd9db

Please sign in to comment.