From 3ec4d2218edf2fc0e5154627c965a8d96eabc890 Mon Sep 17 00:00:00 2001 From: emelko Date: Thu, 25 Apr 2019 11:41:41 +0200 Subject: [PATCH] Adds getTime function for bleedingPredictions reuse; minor style formatting --- components/home.js | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) 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 {