From 7e3eae17588a3824bdd98c2db3020014a050d41c Mon Sep 17 00:00:00 2001 From: Julia Friesel <julia.friesel@gmail.com> Date: Tue, 2 Apr 2019 19:27:28 +0200 Subject: [PATCH] Use LocalDate for getting chart date range --- components/chart/chart.js | 33 ++++++++++++++++----------------- package-lock.json | 5 ----- package.json | 1 - 3 files changed, 16 insertions(+), 23 deletions(-) diff --git a/components/chart/chart.js b/components/chart/chart.js index df0b52a8..586ed416 100644 --- a/components/chart/chart.js +++ b/components/chart/chart.js @@ -1,6 +1,5 @@ import React, { Component } from 'react' import { View, FlatList, ActivityIndicator } from 'react-native' -import range from 'date-range' import { LocalDate } from 'js-joda' import { makeYAxisLabels, makeHorizontalGrid } from './y-axis' import nfpLines from './nfp-lines' @@ -124,14 +123,8 @@ export default class CycleChart extends Component { // we don't want the chart to end abruptly before the first data day amountOfCycleDays += 5 } - const jsDates = getTodayAndPreviousDays(amountOfCycleDays) - return jsDates.map(jsDate => { - return LocalDate.of( - jsDate.getFullYear(), - jsDate.getMonth() + 1, - jsDate.getDate() - ).toString() - }) + const localDates = getTodayAndPreviousDays(amountOfCycleDays) + return localDates.map(localDate => localDate.toString()) } render() { @@ -213,12 +206,18 @@ function LoadingMoreView(props) { } function getTodayAndPreviousDays(n) { - const today = new Date() - today.setHours(12) - today.setMinutes(0) - today.setSeconds(0) - today.setMilliseconds(0) - const earlierDate = new Date(today - (range.DAY * n)) - - return range(earlierDate, today).reverse() + const today = LocalDate.now() + const targetDate = today.minusDays(n) + + function getDaysInRange(currDate, range) { + if (currDate.isBefore(targetDate)) { + return range + } else { + range.push(currDate) + const next = currDate.minusDays(1) + return getDaysInRange(next, range) + } + } + + return getDaysInRange(today, []) } diff --git a/package-lock.json b/package-lock.json index df1788c3..a2ea7950 100644 --- a/package-lock.json +++ b/package-lock.json @@ -3220,11 +3220,6 @@ } } }, - "date-range": { - "version": "0.0.2", - "resolved": "https://registry.npmjs.org/date-range/-/date-range-0.0.2.tgz", - "integrity": "sha1-OVHZ5SWZgu3VMewx5APYImPvbjk=" - }, "debug": { "version": "2.6.9", "resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz", diff --git a/package.json b/package.json index a87b8f9f..ce863237 100644 --- a/package.json +++ b/package.json @@ -21,7 +21,6 @@ "ajv": "^5.5.2", "assert": "^1.4.1", "csvtojson": "^2.0.8", - "date-range": "0.0.2", "isobject": "^3.0.1", "js-base64": "^2.4.8", "js-joda": "^1.8.2", -- GitLab