mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-03 17:41:51 +00:00
fix(vacay): open on the current year instead of the last one added (#2056)
The year list comes back sorted, so picking its last element meant Vacay always landed on the highest year — add 2030 to plan ahead and every visit from then on opened 2030. It now opens the leave-year period today falls into, falls back to the closest year the plan has when that period has none, and keeps the year the viewer is on when a live update reloads the list.
This commit is contained in:
@@ -2,6 +2,7 @@ import { describe, it, expect } from 'vitest'
|
||||
import {
|
||||
DEFAULT_YEAR_SETTINGS,
|
||||
currentPeriodYear,
|
||||
defaultPeriodYear,
|
||||
gridWindow,
|
||||
inGridWindow,
|
||||
resolveYearWindow,
|
||||
@@ -125,3 +126,31 @@ describe('currentPeriodYear', () => {
|
||||
expect(currentPeriodYear(fiscal(7), new Date(2026, 6, 1))).toBe(2026)
|
||||
})
|
||||
})
|
||||
|
||||
describe('defaultPeriodYear', () => {
|
||||
// FE-VACAY-YEARWINDOW-014
|
||||
it('picks the current period when the plan has it', () => {
|
||||
const today = new Date(2026, 2, 15)
|
||||
expect(defaultPeriodYear([2024, 2025, 2026, 2030], DEFAULT_YEAR_SETTINGS, today)).toBe(2026)
|
||||
})
|
||||
|
||||
// FE-VACAY-YEARWINDOW-015
|
||||
it('falls back to the closest year and prefers the coming one on a tie', () => {
|
||||
const today = new Date(2026, 2, 15)
|
||||
expect(defaultPeriodYear([2022, 2024], DEFAULT_YEAR_SETTINGS, today)).toBe(2024)
|
||||
expect(defaultPeriodYear([2028, 2030], DEFAULT_YEAR_SETTINGS, today)).toBe(2028)
|
||||
expect(defaultPeriodYear([2024, 2028], DEFAULT_YEAR_SETTINGS, today)).toBe(2028)
|
||||
})
|
||||
|
||||
// FE-VACAY-YEARWINDOW-016
|
||||
it('is the current period for an empty plan', () => {
|
||||
expect(defaultPeriodYear([], DEFAULT_YEAR_SETTINGS, new Date(2026, 2, 15))).toBe(2026)
|
||||
})
|
||||
|
||||
// FE-VACAY-YEARWINDOW-017
|
||||
it('follows a shifted leave year rather than the calendar year', () => {
|
||||
const today = new Date(2026, 2, 15)
|
||||
expect(defaultPeriodYear([2025, 2026], fiscal(7), today)).toBe(2025)
|
||||
expect(defaultPeriodYear([2026], fiscal(7), today)).toBe(2026)
|
||||
})
|
||||
})
|
||||
|
||||
@@ -100,3 +100,19 @@ export function currentPeriodYear(s: VacayYearSettings = DEFAULT_YEAR_SETTINGS,
|
||||
const iso = `${year}-${pad2(today.getMonth() + 1)}-${pad2(today.getDate())}`
|
||||
return iso < resolveYearWindow(year, s).start ? year - 1 : year
|
||||
}
|
||||
|
||||
/**
|
||||
* The year Vacay opens on. The period we are actually in wins; adding 2030 to plan
|
||||
* ahead should not make 2030 the year the page lands on from then on. When that
|
||||
* period has no year yet, the closest one the plan does have is used, and a tie
|
||||
* goes to the later year because planning looks forward.
|
||||
*/
|
||||
export function defaultPeriodYear(years: number[], s: VacayYearSettings = DEFAULT_YEAR_SETTINGS, today = new Date()): number {
|
||||
const current = currentPeriodYear(s, today)
|
||||
if (years.length === 0 || years.includes(current)) return current
|
||||
return years.reduce((best, year) => {
|
||||
const distance = Math.abs(year - current)
|
||||
const bestDistance = Math.abs(best - current)
|
||||
return distance < bestDistance || (distance === bestDistance && year > best) ? year : best
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user