From 01053f26c1df55b52b5311f7357e3d5f88bc6b4f Mon Sep 17 00:00:00 2001 From: pmandler-umass Date: Wed, 15 Jan 2025 15:03:32 -0500 Subject: [PATCH] Week indexing starts w/ zero and is adjusted for getting correct files. --- src/components/Timeline.tsx | 15 +++++++-------- src/hooks/dataUrl.tsx | 3 ++- src/utils/utils.tsx | 7 +++---- src/views/Home.tsx | 24 +++++++++--------------- 4 files changed, 21 insertions(+), 28 deletions(-) diff --git a/src/components/Timeline.tsx b/src/components/Timeline.tsx index 7df1b21..ef87196 100644 --- a/src/components/Timeline.tsx +++ b/src/components/Timeline.tsx @@ -1,12 +1,12 @@ import { useEffect, useState } from 'react'; import { Slider } from '@mantine/core'; -import { isMobile } from '../utils/utils'; +import { isMobile, MIN_WEEK, MAX_WEEK } from '../utils/utils'; import ab_dates from '../assets/abundance_dates.json'; import mv_dates from '../assets/movement_dates.json'; const months: Array = [ - 'Jan','Feb','Mar','Apr', 'May' ,'Jun','Jul','Aug', 'Sep','Oct','Nov','Dec' + 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ]; // Keeps track of the props and prop type going into the component (look up interfaces in TypeScript) @@ -60,7 +60,7 @@ function Timeline(props: TimelineProps) { useEffect(() => { // update the label WHEN the check for overlay is complete if (labelInit) { - setWeekLabel(myLabels[dataset][week-1]); + setWeekLabel(myLabels[dataset][week]); } }, [week, props.dataset]); @@ -70,9 +70,8 @@ function Timeline(props: TimelineProps) { { const [dataIndex, setDataIndex] = useState(0); // Sets state for the species type const [speciesIndex, setSpeciesIndex] = useState(0); - const [week, setWeek] = useState(0); + const [week, setWeek] = useState(MIN_WEEK); const [adjustWeek, setAdjustWeek] = useState(0); // default state of the map overlay url for the current data displayed. const [overlayUrl, setOverlayUrl] = useState(""); @@ -81,14 +80,14 @@ const HomePage = () => { return; } if (adjustWeek > 0) { - // increments active index (wraps around when at top) - let temp = week + MIN_WEEK; + // increments active index (wraps around when at end of year) + let temp = week + 1; if (temp > MAX_WEEK) temp = MIN_WEEK; checkImageAndUpdate(temp); } else { - // decrements active index (wraps around when at bottom) + // decrements active index (wraps around when at beginning of year) let temp = week - 1; - if (temp < 1) temp = MAX_WEEK; + if (temp < MIN_WEEK) temp = MAX_WEEK; checkImageAndUpdate(temp); } setAdjustWeek(0); @@ -97,10 +96,9 @@ const HomePage = () => { // Called once on startup. Adds a listener for user keyboard events. // Note: it appears this is not called when using Firefox on the iPhone PM 11/9/2024 useEffect(() => { - // determine current week - find msec between today and beginning fo the year - const this_week = dateToWeek(new Date()) loadOutbreaks(); - checkImageAndUpdate(this_week); + // determine current week + checkImageAndUpdate(dateToWeek(new Date())); handleWindowSizeChange(); document.addEventListener('keydown', handleSelection); window.addEventListener('resize', handleWindowSizeChange); @@ -111,10 +109,6 @@ const HomePage = () => { }, []); async function checkImageAndUpdate(this_week: number) { - if (this_week <= 0) { - // the week is not initialized yet - return; - } var image_url = imageURL(dataIndex, speciesIndex, this_week); var response = await fetch(image_url); if (!response.ok) {