From d87a8567546a5d487203e098b9b1b1dbab2001b3 Mon Sep 17 00:00:00 2001 From: Julia Friesel <julia.friesel@gmail.com> Date: Mon, 17 Sep 2018 15:37:56 +0200 Subject: [PATCH] Attach and remove listeners from same db collection --- components/calendar.js | 10 +++++----- components/home.js | 25 ++++++++++++------------- 2 files changed, 17 insertions(+), 18 deletions(-) diff --git a/components/calendar.js b/components/calendar.js index 74b754bd..d9a0fd9a 100644 --- a/components/calendar.js +++ b/components/calendar.js @@ -9,11 +9,11 @@ import styles from '../styles/index' export default class CalendarView extends Component { constructor(props) { - const bleedingDaysSortedByDate = getBleedingDaysSortedByDate() super(props) + this.bleedingDays = getBleedingDaysSortedByDate() const predictedMenses = cycleModule().getPredictedMenses() this.state = { - bleedingDaysInCalFormat: toCalFormat(bleedingDaysSortedByDate), + bleedingDaysInCalFormat: toCalFormat(this.bleedingDays), predictedBleedingDaysInCalFormat: predictionToCalFormat(predictedMenses), todayInCalFormat: todayToCalFormat() } @@ -22,18 +22,18 @@ export default class CalendarView extends Component { return function() { const predictedMenses = cycleModule().getPredictedMenses() CalendarComponent.setState({ - bleedingDaysInCalFormat: toCalFormat(bleedingDaysSortedByDate), + bleedingDaysInCalFormat: toCalFormat(this.bleedingDays), predictedBleedingDaysInCalFormat: predictionToCalFormat(predictedMenses), todayInCalFormat: todayToCalFormat() }) } })(this) - bleedingDaysSortedByDate.addListener(this.setStateWithCalFormattedDays) + this.bleedingDays.addListener(this.setStateWithCalFormattedDays) } componentWillUnmount() { - getBleedingDaysSortedByDate().removeListener(this.setStateWithCalFormattedDays) + this.bleedingDays.removeListener(this.setStateWithCalFormattedDays) } passDateToDayView = (result) => { diff --git a/components/home.js b/components/home.js index 025a77b6..fffe6939 100644 --- a/components/home.js +++ b/components/home.js @@ -14,30 +14,29 @@ import {bleedingPrediction as labels} from './labels' export default class Home extends Component { constructor(props) { super(props) - const getCycleDayNumber = cycleModule().getCycleDayNumber + this.getCycleDayNumber = cycleModule().getCycleDayNumber this.todayDateString = LocalDate.now().toString() - const cycleDayNumber = getCycleDayNumber(this.todayDateString) + const cycleDayNumber = this.getCycleDayNumber(this.todayDateString) this.state = { welcomeText: determineWelcomeText(cycleDayNumber), predictionText: determinePredictionText() } - this.setStateWithCurrentText = (function (HomeComponent) { - return function () { - const cycleDayNumber = getCycleDayNumber(HomeComponent.todayDateString) - HomeComponent.setState({ - welcomeText: determineWelcomeText(cycleDayNumber), - predictionText: determinePredictionText() - }) - } - })(this) + this.bleedingDays = getBleedingDaysSortedByDate() + this.bleedingDays.addListener(this.setStateWithCurrentText) + } - getBleedingDaysSortedByDate().addListener(this.setStateWithCurrentText) + setStateWithCurrentText = () => { + const cycleDayNumber = this.getCycleDayNumber(this.todayDateString) + this.setState({ + welcomeText: determineWelcomeText(cycleDayNumber), + predictionText: determinePredictionText() + }) } componentWillUnmount() { - getBleedingDaysSortedByDate().removeListener(this.setStateWithCurrentText) + this.bleedingDays.removeListener(this.setStateWithCurrentText) } passTodayToDayView() { -- GitLab