Skip to content
Snippets Groups Projects
Commit 4f6a705c authored by Julia Friesel's avatar Julia Friesel
Browse files

Remove listeners on layout before re-adding them

parent e465b2a8
No related branches found
No related tags found
No related merge requests found
...@@ -48,9 +48,7 @@ export default class CycleChart extends Component { ...@@ -48,9 +48,7 @@ export default class CycleChart extends Component {
onLayout = ({ nativeEvent }) => { onLayout = ({ nativeEvent }) => {
if (this.state.chartHeight) return if (this.state.chartHeight) return
const height = nativeEvent.layout.height const height = nativeEvent.layout.height
this.setState({ chartHeight: height }) const reCalculateChartInfo = () => {
this.reCalculateChartInfo = (_, changes) => {
if (nothingChanged(changes)) return
// how many symptoms need to be displayed on the chart's upper symptom row? // how many symptoms need to be displayed on the chart's upper symptom row?
this.symptomRowSymptoms = [ this.symptomRowSymptoms = [
'bleeding', 'bleeding',
...@@ -66,8 +64,8 @@ export default class CycleChart extends Component { ...@@ -66,8 +64,8 @@ export default class CycleChart extends Component {
}) })
}) })
this.xAxisHeight = this.state.chartHeight * config.xAxisHeightPercentage this.xAxisHeight = height * config.xAxisHeightPercentage
const remainingHeight = this.state.chartHeight - this.xAxisHeight const remainingHeight = height - this.xAxisHeight
this.symptomHeight = config.symptomHeightPercentage * remainingHeight this.symptomHeight = config.symptomHeightPercentage * remainingHeight
this.symptomRowHeight = this.symptomRowSymptoms.length * this.symptomRowHeight = this.symptomRowSymptoms.length *
this.symptomHeight this.symptomHeight
...@@ -78,16 +76,35 @@ export default class CycleChart extends Component { ...@@ -78,16 +76,35 @@ export default class CycleChart extends Component {
this.chartSymptoms.push('temperature') this.chartSymptoms.push('temperature')
} }
const columnData = this.makeColumnInfo() const columnData = this.makeColumnInfo(nfpLines(), this.chartSymptoms)
this.setState({ columns: columnData }) this.setState({
columns: columnData,
chartHeight: height
})
}
reCalculateChartInfo()
this.updateListeners(reCalculateChartInfo)
}
updateListeners(dataUpdateHandler) {
// remove existing listeners
if(this.handleDbChange) {
this.cycleDaysSortedByDate.removeListener(this.handleDbChange)
}
if (this.removeObvListener) this.removeObvListener()
this.handleDbChange = (_, changes) => {
if (nothingChanged(changes)) return
dataUpdateHandler()
} }
this.cycleDaysSortedByDate.addListener(this.reCalculateChartInfo) this.cycleDaysSortedByDate.addListener(this.handleDbChange)
this.removeObvListener = scaleObservable(this.reCalculateChartInfo, false) this.removeObvListener = scaleObservable(dataUpdateHandler, false)
} }
componentWillUnmount() { componentWillUnmount() {
this.cycleDaysSortedByDate.removeListener(this.reCalculateChartInfo) this.cycleDaysSortedByDate.removeListener(this.handleDbChange)
this.removeObvListener() this.removeObvListener()
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment