From ebf18dac22dff00652c30d20e04f017752ae8f12 Mon Sep 17 00:00:00 2001 From: Julia Friesel Date: Wed, 18 Jul 2018 14:14:43 +0200 Subject: [PATCH] Refactor getStatusAsString --- components/cycle-day/index.js | 4 +- components/cycle-day/symptoms/mucus.js | 2 +- lib/sympto-adapter.js | 60 +++++++++++++++++++------- 3 files changed, 47 insertions(+), 19 deletions(-) diff --git a/components/cycle-day/index.js b/components/cycle-day/index.js index 3eeb983..78c3293 100644 --- a/components/cycle-day/index.js +++ b/components/cycle-day/index.js @@ -4,7 +4,7 @@ import { Text } from 'react-native' import cycleModule from '../../lib/cycle' -import getFertilityStatus from '../../lib/sympto-adapter' +import { getFertilityStatusStringForDay } from '../../lib/sympto-adapter' import DayView from './cycle-day-overview' import BleedingEditView from './symptoms/bleeding' import TemperatureEditView from './symptoms/temperature' @@ -33,7 +33,7 @@ export default class Day extends Component { render() { const cycleDayNumber = getCycleDayNumber(this.cycleDay.date) - const fertilityStatus = getFertilityStatus(this.cycleDay.date) + const fertilityStatus = getFertilityStatusStringForDay(this.cycleDay.date) return ( diff --git a/components/cycle-day/symptoms/mucus.js b/components/cycle-day/symptoms/mucus.js index f94a656..68ff292 100644 --- a/components/cycle-day/symptoms/mucus.js +++ b/components/cycle-day/symptoms/mucus.js @@ -94,7 +94,7 @@ export default class Mucus extends Component { saveSymptom('mucus', this.cycleDay, { feeling: this.state.currentFeelingValue, texture: this.state.currentTextureValue, - computedNfp: computeSensiplanValue(this.state.currentFeelingValue, this.state.currentTextureValue), + value: computeSensiplanValue(this.state.currentFeelingValue, this.state.currentTextureValue), exclude: this.state.exclude }) }, diff --git a/lib/sympto-adapter.js b/lib/sympto-adapter.js index 0d354cb..337a1a7 100644 --- a/lib/sympto-adapter.js +++ b/lib/sympto-adapter.js @@ -2,31 +2,59 @@ import getFertilityStatus from './sympto' import cycleModule from './cycle' import { fertilityStatus } from '../labels/labels' -const { getCycleDaysBeforeDay, getPreviousCycles } = cycleModule() +const { + getCycleForDay, + getCyclesBefore, + getPreviousCycle +} = cycleModule() -export default function (dateString) { - const cycle = getCycleDaysBeforeDay(dateString) +export function getFertilityStatusStringForDay(dateString) { + const cycle = getCycleForDay(dateString) if (!cycle) return fertilityStatus.unknown - const previousCycles = getPreviousCycles(cycle[0]) + const cycleInfo = {cycle: formatCycleForSympto(cycle)} - const status = getFertilityStatus({ - cycle: formatCycleForSympto(cycle), - previousCycles: previousCycles.map(formatCycleForSympto) - }) + const previousCycle = getPreviousCycle(dateString) - if (status.phases.postOvulatory) { - const phase = status.phases.postOvulatory - if (phase.start.date === dateString) { - return fertilityStatus.fertileUntilEvening + if (previousCycle) { + cycleInfo.previousCycle = formatCycleForSympto(previousCycle) + const earlierCycles = getCyclesBefore(previousCycle[0]) + if (earlierCycles) { + cycleInfo.earlierCycles = earlierCycles.map(formatCycleForSympto) } } - if (status.assumeFertility) { - return fertilityStatus.fertile - } else { - return fertilityStatus.infertile + const status = getFertilityStatus(cycleInfo) + + const phaseNameForDay = Object.keys(status.phases).find(phaseName => { + const phase = status.phases[phaseName] + const dayIsAfterPhaseStart = dateString >= phase.start.date + let dayIsBeforePhaseEnd + if (phase.end) { + dayIsBeforePhaseEnd = dateString <= phase.end.date + } else { + dayIsBeforePhaseEnd = true + } + return dayIsAfterPhaseStart && dayIsBeforePhaseEnd + }) + + return mapToString(phaseNameForDay, dateString, status) +} + +function mapToString(phaseNameForDay, dateString, status) { + const mapping = { + preOvulatory: () => fertilityStatus.infertile, + periOvulatory: (dateString, status) => { + const phaseEnd = status.phases.periOvulatory.end + if (phaseEnd && phaseEnd.date === dateString) { + return fertilityStatus.fertileUntilEvening + } + return fertilityStatus.fertile + }, + postOvulatory: () => fertilityStatus.infertile } + + return mapping[phaseNameForDay](dateString, status) } function formatCycleForSympto(cycle) {