Merge branch '258-phase-on-home-screen-seems-to-always-be-2' into 'master'

Fix cycle status edge case

Closes #258

See merge request bloodyhealth/drip!128
This commit is contained in:
Julia Friesel
2018-12-15 13:48:22 +00:00
2 changed files with 23 additions and 29 deletions
+23 -11
View File
@@ -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)
}
}
}