Attach and remove listeners from same db collection

This commit is contained in:
Julia Friesel
2018-09-17 15:37:56 +02:00
parent a15f1177be
commit d87a856754
2 changed files with 17 additions and 18 deletions
+5 -5
View File
@@ -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 {
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) => {
+10 -11
View File
@@ -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({
setStateWithCurrentText = () => {
const cycleDayNumber = this.getCycleDayNumber(this.todayDateString)
this.setState({
welcomeText: determineWelcomeText(cycleDayNumber), welcomeText: determineWelcomeText(cycleDayNumber),
predictionText: determinePredictionText() predictionText: determinePredictionText()
}) })
} }
})(this)
getBleedingDaysSortedByDate().addListener(this.setStateWithCurrentText)
}
componentWillUnmount() { componentWillUnmount() {
getBleedingDaysSortedByDate().removeListener(this.setStateWithCurrentText) this.bleedingDays.removeListener(this.setStateWithCurrentText)
} }
passTodayToDayView() { passTodayToDayView() {