Refactor getStatusAsString
This commit is contained in:
+44
-16
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user