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

Attach and remove listeners from same db collection

parent a15f1177
No related branches found
No related tags found
No related merge requests found
...@@ -9,11 +9,11 @@ import styles from '../styles/index' ...@@ -9,11 +9,11 @@ import styles from '../styles/index'
export default class CalendarView extends Component { export default class CalendarView extends Component {
constructor(props) { constructor(props) {
const bleedingDaysSortedByDate = getBleedingDaysSortedByDate()
super(props) super(props)
this.bleedingDays = getBleedingDaysSortedByDate()
const predictedMenses = cycleModule().getPredictedMenses() const predictedMenses = cycleModule().getPredictedMenses()
this.state = { this.state = {
bleedingDaysInCalFormat: toCalFormat(bleedingDaysSortedByDate), bleedingDaysInCalFormat: toCalFormat(this.bleedingDays),
predictedBleedingDaysInCalFormat: predictionToCalFormat(predictedMenses), predictedBleedingDaysInCalFormat: predictionToCalFormat(predictedMenses),
todayInCalFormat: todayToCalFormat() todayInCalFormat: todayToCalFormat()
} }
...@@ -22,18 +22,18 @@ export default class CalendarView extends Component { ...@@ -22,18 +22,18 @@ export default class CalendarView extends Component {
return function() { return function() {
const predictedMenses = cycleModule().getPredictedMenses() const predictedMenses = cycleModule().getPredictedMenses()
CalendarComponent.setState({ CalendarComponent.setState({
bleedingDaysInCalFormat: toCalFormat(bleedingDaysSortedByDate), bleedingDaysInCalFormat: toCalFormat(this.bleedingDays),
predictedBleedingDaysInCalFormat: predictionToCalFormat(predictedMenses), predictedBleedingDaysInCalFormat: predictionToCalFormat(predictedMenses),
todayInCalFormat: todayToCalFormat() todayInCalFormat: todayToCalFormat()
}) })
} }
})(this) })(this)
bleedingDaysSortedByDate.addListener(this.setStateWithCalFormattedDays) this.bleedingDays.addListener(this.setStateWithCalFormattedDays)
} }
componentWillUnmount() { componentWillUnmount() {
getBleedingDaysSortedByDate().removeListener(this.setStateWithCalFormattedDays) this.bleedingDays.removeListener(this.setStateWithCalFormattedDays)
} }
passDateToDayView = (result) => { passDateToDayView = (result) => {
......
...@@ -14,30 +14,29 @@ import {bleedingPrediction as labels} from './labels' ...@@ -14,30 +14,29 @@ import {bleedingPrediction as labels} from './labels'
export default class Home extends Component { export default class Home extends Component {
constructor(props) { constructor(props) {
super(props) super(props)
const getCycleDayNumber = cycleModule().getCycleDayNumber this.getCycleDayNumber = cycleModule().getCycleDayNumber
this.todayDateString = LocalDate.now().toString() this.todayDateString = LocalDate.now().toString()
const cycleDayNumber = getCycleDayNumber(this.todayDateString) const cycleDayNumber = this.getCycleDayNumber(this.todayDateString)
this.state = { this.state = {
welcomeText: determineWelcomeText(cycleDayNumber), welcomeText: determineWelcomeText(cycleDayNumber),
predictionText: determinePredictionText() predictionText: determinePredictionText()
} }
this.setStateWithCurrentText = (function (HomeComponent) { this.bleedingDays = getBleedingDaysSortedByDate()
return function () { this.bleedingDays.addListener(this.setStateWithCurrentText)
const cycleDayNumber = getCycleDayNumber(HomeComponent.todayDateString) }
HomeComponent.setState({
welcomeText: determineWelcomeText(cycleDayNumber),
predictionText: determinePredictionText()
})
}
})(this)
getBleedingDaysSortedByDate().addListener(this.setStateWithCurrentText) setStateWithCurrentText = () => {
const cycleDayNumber = this.getCycleDayNumber(this.todayDateString)
this.setState({
welcomeText: determineWelcomeText(cycleDayNumber),
predictionText: determinePredictionText()
})
} }
componentWillUnmount() { componentWillUnmount() {
getBleedingDaysSortedByDate().removeListener(this.setStateWithCurrentText) this.bleedingDays.removeListener(this.setStateWithCurrentText)
} }
passTodayToDayView() { passTodayToDayView() {
......
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