diff --git a/components/home.js b/components/home.js index b173435..73a4ef7 100644 --- a/components/home.js +++ b/components/home.js @@ -179,13 +179,17 @@ export default class Home extends Component { } } +function getTimes(prediction) { + const todayDate = LocalDate.now() + const predictedBleedingStart = LocalDate.parse(prediction[0][0]) + const predictedBleedingEnd = LocalDate.parse(prediction[0][ prediction[0].length - 1 ]) + const daysToEnd = todayDate.until(predictedBleedingEnd, ChronoUnit.DAYS) + return { todayDate, predictedBleedingStart, predictedBleedingEnd, daysToEnd } +} + function determinePredictionText(bleedingPrediction) { if (!bleedingPrediction.length) return predictLabels.noPrediction - const todayDate = LocalDate.now() - const predictedBleedingStart = LocalDate.parse(bleedingPrediction[0][0]) - const predictedBleedingEnd = LocalDate.parse( - bleedingPrediction[0][ bleedingPrediction[0].length - 1 ] - ) + const { todayDate, predictedBleedingStart, predictedBleedingEnd, daysToEnd } = getTimes(bleedingPrediction) if (todayDate.isBefore(predictedBleedingStart)) { return predictLabels.predictionInFuture( todayDate.until(predictedBleedingStart, ChronoUnit.DAYS), @@ -194,10 +198,10 @@ function determinePredictionText(bleedingPrediction) { } if (todayDate.isAfter(predictedBleedingEnd)) { return predictLabels.predictionInPast( - formatDateForShortText(predictedBleedingStart), formatDateForShortText(predictedBleedingEnd) + formatDateForShortText(predictedBleedingStart), + formatDateForShortText(predictedBleedingEnd) ) } - const daysToEnd = todayDate.until(predictedBleedingEnd, ChronoUnit.DAYS) if (daysToEnd === 0) { return predictLabels.predictionStartedNoDaysLeft } else if (daysToEnd === 1) { @@ -209,16 +213,13 @@ function determinePredictionText(bleedingPrediction) { function getBleedingPredictionRange(prediction) { if (!prediction.length) return labels.unknown - const todayDate = LocalDate.now() - const predictedBleedingStart = LocalDate.parse(prediction[0][0]) - const predictedBleedingEnd = LocalDate.parse(prediction[0][ prediction[0].length - 1 ]) + const { todayDate, predictedBleedingStart, predictedBleedingEnd, daysToEnd } = getTimes(prediction) if (todayDate.isBefore(predictedBleedingStart)) { return `${todayDate.until(predictedBleedingStart, ChronoUnit.DAYS)}-${todayDate.until(predictedBleedingEnd, ChronoUnit.DAYS)}` } if (todayDate.isAfter(predictedBleedingEnd)) { return labels.unknown } - const daysToEnd = todayDate.until(predictedBleedingEnd, ChronoUnit.DAYS) if (daysToEnd === 0) { return '0' } else {