From c428b51b5055d246a9e13436d2061fc3d20ecc02 Mon Sep 17 00:00:00 2001 From: Nate-Wessel Date: Mon, 15 Jul 2024 20:59:40 +0000 Subject: [PATCH] implement data-dependant date limits --- frontend/src/dateRange.js | 17 ++++++++++++----- frontend/src/spatialData.js | 8 ++++++++ 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/frontend/src/dateRange.js b/frontend/src/dateRange.js index 96b90ed..d038f9a 100644 --- a/frontend/src/dateRange.js +++ b/frontend/src/dateRange.js @@ -7,8 +7,10 @@ import { DataContext } from './Layout' export class DateRange extends Factor { #startDate #endDate + #dataContext constructor(dataContext){ super(dataContext) + this.#dataContext = dataContext } get isComplete(){ return this.#startDate && this.#endDate && this.#startDate < this.#endDate @@ -71,6 +73,14 @@ export class DateRange extends Factor { } return dayCount } + get maxDate(){ + // default to today if actual max date not known (yet) + return this.#dataContext.dateRange.maxDate ?? new Date() + } + get minDate(){ + // default to today if actual max date not known (yet) + return this.#dataContext.dateRange.minDate ?? new Date('2017-09-01') + } } function formatISODate(dt){ // this is waaay too complicated... alas @@ -80,9 +90,6 @@ function formatISODate(dt){ // this is waaay too complicated... alas return `${year}-${('0'+month).slice(-2)}-${('0'+day).slice(-2)}` } -const today = new Date() -const earliestDataDate = new Date('2017-01-01') - function DateRangeElement({dateRange}){ const { logActivity } = useContext(DataContext) const [ selectedRange, setSelectedRange ] = useState(undefined) @@ -102,8 +109,8 @@ function DateRangeElement({dateRange}){ selectRange={true} allowPartialRange={true} onChange={setSelectedRange} - maxDate={today} - minDate={earliestDataDate} + maxDate={dateRange.maxDate} + minDate={dateRange.minDate} /> } diff --git a/frontend/src/spatialData.js b/frontend/src/spatialData.js index 1533af4..55c7c92 100644 --- a/frontend/src/spatialData.js +++ b/frontend/src/spatialData.js @@ -12,6 +12,7 @@ export class SpatialData { #factors = [] #queries = new Map() // store/cache for travelTimeQueries, letting them remember their results if any #knownHolidays = [] + #dataDateRange = { minDate: undefined, maxDate: undefined } #queue = new PQueue({concurrency: 3}) constructor(){ this.#factors.push(new Days(this)) @@ -19,6 +20,12 @@ export class SpatialData { fetch(`${domain}/holidays`) .then( response => response.json() ) .then( holidayList => this.#knownHolidays = holidayList ) + fetch(`${domain}/date-range`) + .then( response => response.json() ) + .then( dates => { + this.#dataDateRange.minDate = new Date(dates.minDate) + this.#dataDateRange.maxDate = new Date(dates.maxDate) + } ) } get corridors(){ return this.#factors.filter( f => f instanceof Corridor ) } get timeRanges(){ return this.#factors.filter( f => f instanceof TimeRange ) } @@ -31,6 +38,7 @@ export class SpatialData { return this.corridors.find( cor => cor.isActive ) } get holidays(){ return this.#knownHolidays } + get dateRange(){ return this.#dataDateRange } createCorridor(){ let corridor = new Corridor(this) this.#factors.push(corridor)