Fix diff mutation bug, simplify resetZoom trigger

pull/10616/head
Jared Scheib 2017-05-26 21:00:04 -07:00
parent a2884a175e
commit 59027f157d
2 changed files with 7 additions and 26 deletions

View File

@ -1,8 +1,8 @@
/* eslint-disable no-magic-numbers */
import React, {PropTypes} from 'react'
import shallowCompare from 'react-addons-shallow-compare'
import _ from 'lodash'
import {deepDiffByKeys} from 'utils/deepDiff.js'
import Dygraph from '../../external/dygraph'
import getRange from 'src/shared/parsing/getRangeForDygraph'
@ -160,18 +160,17 @@ export default React.createClass({
// If only timeRange has changed, only reset the zoom.
// TODO: possibly try to use dateWindow and a zoomCallback to updateOptions
// in comonentDidUpdate so that zoom and redraw happen at the same time
shouldComponentUpdate(nextProps) {
const changedProps = deepDiffByKeys(this.props, nextProps)
const testProps = ['lower', 'upper']
const timeRangeChanged = !!_.intersection(changedProps, testProps).length
const shouldUpdate = !!_.difference(changedProps, testProps).length
shouldComponentUpdate(nextProps, nextState) {
const timeRangeChanged = !_.isEqual(
nextProps.timeRange,
this.props.timeRange
)
if (this.dygraph.isZoomed() && timeRangeChanged) {
this.dygraph.resetZoom()
}
return shouldUpdate
return shallowCompare(this, nextProps, nextState)
},
componentDidUpdate() {

View File

@ -1,18 +0,0 @@
import _ from 'lodash'
export const deepDiffByKeys = (sourceObj, comparatorObj) => {
const keysWithDiffValues = []
// perform a deep diff on props objects to see what keys have changed
// from https://stackoverflow.com/questions/8572826/generic-deep-diff-between-two-objects
_.mergeWith(sourceObj, comparatorObj, (objectValue, sourceValue, key) => {
if (
!_.isEqual(objectValue, sourceValue) &&
Object(objectValue) !== objectValue
) {
keysWithDiffValues.push(key)
}
})
return keysWithDiffValues
}