Skip to content

Commit

Permalink
feat: add day/month inc/dec buttons [PT-188059193]
Browse files Browse the repository at this point in the history
  • Loading branch information
pjanik committed Aug 8, 2024
1 parent e7c4a99 commit 88d887d
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 7 deletions.
17 changes: 14 additions & 3 deletions src/grasp-seasons/components/seasons.scss
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,20 @@ body {
height: 20px;
}

.day {
margin-top: 20px;
margin-bottom: 12px;
.day-row {
margin-top: 10px;
margin-bottom: 5px;
display: flex;
justify-content: space-between;
align-items: center;
width: 290px;

.day {
display: flex;
align-items: center;
justify-content: center;
width: 130px;
}
}

.day-slider {
Expand Down
30 changes: 26 additions & 4 deletions src/grasp-seasons/components/seasons.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { useState, useEffect, useRef, ChangeEvent, useCallback } from "react";
import Slider from "./slider/slider";
import { getSolarNoonIntensity, isValidLatitude, isValidLongitude } from "../../utils/daylight-utils";
import { changeMonthOfDayOfYear, getSolarNoonIntensity, isValidLatitude, isValidLongitude } from "../../utils/daylight-utils";
import InfiniteDaySlider from "./slider/infinite-day-slider";
import MyLocations from "./my-locations";
import getURLParam from "../utils/utils";
Expand All @@ -13,6 +13,7 @@ import { useAnimation } from "../hooks/use-animation";
import { ILocation } from "../../types";

import ForwardBackIcon from "../../assets/images/forward-back-icon.svg";
import ForwardBackJumpIcon from "../../assets/images/forward-back-jump-icon.svg";

import "./seasons.scss";

Expand Down Expand Up @@ -165,6 +166,21 @@ const Seasons: React.FC<IProps> = ({ lang = "en_us", initialState = {}, log = (a
setSimState(prevState => ({ ...prevState, day: ui.value }));
};

const handleDayIncrement = (increment: number) => () => {
setSimState(prevState => {
// Make sure day is within [0, 364] range
const day = (prevState.day + increment + 365) % 365;
return { ...prevState, day };
});
};

const handleMonthIncrement = (monthIncrement: number) => () => {
setSimState(prevState => {
const day = changeMonthOfDayOfYear(prevState.day, monthIncrement);
return { ...prevState, day };
});
};

const handleSimCheckboxChange = (event: ChangeEvent<HTMLInputElement>) => {
setSimState(prevState => ({ ...prevState, [event.target.name as any as keyof ISimState]: event.target.checked }));
logCheckboxChange(event);
Expand Down Expand Up @@ -274,9 +290,15 @@ const Seasons: React.FC<IProps> = ({ lang = "en_us", initialState = {}, log = (a
</div>
</div>
</div>
<div className="day">
<label>{ t("~DAY", simLang) }:</label>
{ getFormattedDay() }
<div className="day-row">
<SquareButton onClick={handleMonthIncrement(-1)}><ForwardBackJumpIcon /></SquareButton>
<SquareButton onClick={handleDayIncrement(-1)}><ForwardBackIcon /></SquareButton>
<div className="day">
<label>{ t("~DAY", simLang) }:</label>
{ getFormattedDay() }
</div>
<SquareButton onClick={handleDayIncrement(1)}><ForwardBackIcon style={{transform: "rotate(180deg"}} /></SquareButton>
<SquareButton onClick={handleMonthIncrement(1)}><ForwardBackJumpIcon style={{transform: "rotate(180deg"}} /></SquareButton>
</div>
<div className="day-slider">
<InfiniteDaySlider
Expand Down
29 changes: 29 additions & 0 deletions src/utils/daylight-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,32 @@ export function isValidLatitude(latitude: string): boolean {
const parsed = Number(latitude);
return !isNaN(parsed) && parsed >= -90 && parsed <= 90;
}

export function changeMonthOfDayOfYear(dayOfYearIndex: number, monthsToAdd: number): number {
if (dayOfYearIndex < 0 || dayOfYearIndex >= 365) {
throw new Error("dayOfYearIndex must be between 0 and 364");
}

// Days in each month for a leap year
const daysInMonth = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];

// Calculate the current month and the day of the month
let accumulatedDays = 0;
let currentMonth = 0;
for (currentMonth = 0; currentMonth < daysInMonth.length; currentMonth++) {
if (dayOfYearIndex < accumulatedDays + daysInMonth[currentMonth]) {
break;
}
accumulatedDays += daysInMonth[currentMonth];
}

const dayOfMonth = dayOfYearIndex - accumulatedDays;
let newMonth = (currentMonth + monthsToAdd + 12) % 12;

let newDayOfYearIndex = dayOfMonth;
for (let i = 0; i < newMonth; i++) {
newDayOfYearIndex += daysInMonth[i];
}

return newDayOfYearIndex % 365;
}

0 comments on commit 88d887d

Please sign in to comment.