diff --git a/components/home.js b/components/home.js index ad4dce1..e57633d 100644 --- a/components/home.js +++ b/components/home.js @@ -9,7 +9,6 @@ import { getCycleDaysSortedByDate } from '../db' import { getFertilityStatusForDay } from '../lib/sympto-adapter' import styles from '../styles' import AppText, { AppTextLight } from './app-text' -import nothingChanged from '../db/db-unchanged' import DripHomeIcon from '../assets/drip-home-icons' const HomeButton = ({ backgroundColor, children }) => { @@ -42,23 +41,6 @@ export default class Home extends Component { } this.cycleDays = getCycleDaysSortedByDate() - this.cycleDays.addListener(this.updateState) - } - - updateState = (_, changes) => { - if (nothingChanged(changes)) return - const prediction = this.getBleedingPrediction() - const fertilityStatus = getFertilityStatusForDay(this.todayDateString) - this.setState({ - cycleDayNumber: this.getCycleDayNumber(this.todayDateString), - predictionText: determinePredictionText(prediction), - bleedingPredictionRange: getBleedingPredictionRange(prediction), - ...fertilityStatus - }) - } - - componentWillUnmount() { - this.cycleDays.removeListener(this.updateState) } passTodayTo(componentName) { diff --git a/lib/sympto-adapter.js b/lib/sympto-adapter.js index 628ea2b..980cbfe 100644 --- a/lib/sympto-adapter.js +++ b/lib/sympto-adapter.js @@ -1,16 +1,17 @@ import getFertilityStatus from './sympto' import cycleModule from './cycle' -import { fertilityStatus } from '../i18n/en/labels' import { useCervixObservable } from '../local-storage' +import { fertilityStatus as labels } from '../i18n/en/labels' export function getFertilityStatusForDay(dateString) { const status = getCycleStatusForDay(dateString) if (!status) return { - status: fertilityStatus.fertile, + status: labels.fertile, phase: null } - const phaseNameForDay = Object.keys(status.phases).find(phaseName => { + const phases = Object.keys(status.phases) + const phaseNameForDay = phases.find(phaseName => { const phase = status.phases[phaseName] const dayIsAfterPhaseStart = dateString >= phase.start.date let dayIsBeforePhaseEnd @@ -22,6 +23,12 @@ export function getFertilityStatusForDay(dateString) { return dayIsAfterPhaseStart && dayIsBeforePhaseEnd }) + // if there's only cycle data for the pre phase and the target day is after its end, + // the day is in the peri phase + if (phases.length === 1 && phases[0] === 'preOvulatory' && !phaseNameForDay) { + return formatStatus('periOvulatory', dateString, {phases: {periOvulatory: {}}}) + } + return formatStatus(phaseNameForDay, dateString, status) } @@ -58,27 +65,32 @@ function formatStatus(phaseNameForDay, dateString, status) { const mapping = { preOvulatory: () => { return { - status: fertilityStatus.infertile, + status: labels.infertile, phase: 1, - statusText: fertilityStatus.preOvuText + statusText: labels.preOvuText } }, periOvulatory: (dateString, status) => { - const phaseEnd = status.phases.periOvulatory.end + //there might not actually be any data for the phase + const peri = status.phases.periOvulatory + const phaseEnd = peri && peri.end + let s if (phaseEnd && phaseEnd.date === dateString) { - return fertilityStatus.fertileUntilEvening + s = labels.fertileUntilEvening + } else { + s = labels.fertile } return { - status: fertilityStatus.fertile, + status: s, phase: 2, - statusText: fertilityStatus.periOvuText + statusText: labels.periOvuText } }, postOvulatory: (dateString, status) => { return { - status: fertilityStatus.infertile, + status: labels.infertile, phase: 3, - statusText: fertilityStatus.postOvuText(status.temperatureShift.rule) + statusText: labels.postOvuText(status.temperatureShift.rule) } } }