mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-30 00:20:07 +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:
@@ -1,4 +1,4 @@
|
||||
// FE-STORE-VCY-001 to FE-STORE-VCY-018
|
||||
// FE-STORE-VCY-001 to FE-STORE-VCY-023
|
||||
// Complements tests/unit/stores/vacayStore.test.ts: optimistic toggles and their
|
||||
// rollbacks, the leave-year window plumbing and the holiday-marker merge rules.
|
||||
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
||||
@@ -366,3 +366,60 @@ describe('vacayStore holiday markers', () => {
|
||||
expect(useVacayStore.getState().holidays).toEqual({});
|
||||
});
|
||||
});
|
||||
|
||||
describe('vacayStore year selection', () => {
|
||||
it('FE-STORE-VCY-019: the first load opens on the current period, not on the highest year', async () => {
|
||||
const current = new Date().getFullYear();
|
||||
server.use(http.get('/api/addons/vacay/years', () =>
|
||||
HttpResponse.json({ years: [current - 1, current, current + 4] })));
|
||||
|
||||
await useVacayStore.getState().loadYears();
|
||||
|
||||
expect(useVacayStore.getState().selectedYear).toBe(current);
|
||||
});
|
||||
|
||||
it('FE-STORE-VCY-020: with no year for the current period the closest one is opened', async () => {
|
||||
const current = new Date().getFullYear();
|
||||
server.use(http.get('/api/addons/vacay/years', () =>
|
||||
HttpResponse.json({ years: [current - 3, current + 1] })));
|
||||
|
||||
await useVacayStore.getState().loadYears();
|
||||
|
||||
expect(useVacayStore.getState().selectedYear).toBe(current + 1);
|
||||
});
|
||||
|
||||
it('FE-STORE-VCY-021: a reload keeps the year the viewer is looking at', async () => {
|
||||
const current = new Date().getFullYear();
|
||||
useVacayStore.setState({ years: [current, current + 1], selectedYear: current + 1 });
|
||||
server.use(http.get('/api/addons/vacay/years', () =>
|
||||
HttpResponse.json({ years: [current, current + 1] })));
|
||||
|
||||
await useVacayStore.getState().loadYears();
|
||||
|
||||
expect(useVacayStore.getState().selectedYear).toBe(current + 1);
|
||||
});
|
||||
|
||||
it('FE-STORE-VCY-022: a reload that lost the selected year falls back to the current period', async () => {
|
||||
const current = new Date().getFullYear();
|
||||
useVacayStore.setState({ years: [current, current + 1], selectedYear: current + 1 });
|
||||
server.use(http.get('/api/addons/vacay/years', () =>
|
||||
HttpResponse.json({ years: [current] })));
|
||||
|
||||
await useVacayStore.getState().loadYears();
|
||||
|
||||
expect(useVacayStore.getState().selectedYear).toBe(current);
|
||||
});
|
||||
|
||||
it('FE-STORE-VCY-023: removing the open year drops back to the current period', async () => {
|
||||
const current = new Date().getFullYear();
|
||||
useVacayStore.setState({ years: [current, current + 1], selectedYear: current + 1 });
|
||||
server.use(
|
||||
http.delete('/api/addons/vacay/years/:year', () => HttpResponse.json({ years: [current] })),
|
||||
http.get('/api/addons/vacay/stats/:year', () => HttpResponse.json({ stats: [] })),
|
||||
);
|
||||
|
||||
await useVacayStore.getState().removeYear(current + 1);
|
||||
|
||||
expect(useVacayStore.getState().selectedYear).toBe(current);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -7,7 +7,7 @@ import type {
|
||||
VacayShareOutgoing, VacayShareIncoming, SharedVacayCalendar, VacayYearSettings,
|
||||
} from '../types'
|
||||
import { isSchoolHolidayCountrySupported } from '../vacay/schoolHolidayCountries'
|
||||
import { DEFAULT_YEAR_SETTINGS, currentPeriodYear, inGridWindow, windowCalendarYears } from '../vacay/yearWindow'
|
||||
import { DEFAULT_YEAR_SETTINGS, defaultPeriodYear, inGridWindow, windowCalendarYears } from '../vacay/yearWindow'
|
||||
import type {
|
||||
VacaySetColorRequest, VacayInviteRequest, VacayInviteActionRequest,
|
||||
VacayAddYearRequest, VacayToggleEntryRequest, VacayCompanyHolidayRequest,
|
||||
@@ -302,12 +302,15 @@ export const useVacayStore = create<VacayState>((set, get) => ({
|
||||
|
||||
loadYears: async () => {
|
||||
const data = await api.getYears()
|
||||
set({ years: data.years })
|
||||
if (data.years.length > 0) {
|
||||
set({ selectedYear: data.years[data.years.length - 1] })
|
||||
} else {
|
||||
set({ selectedYear: currentPeriodYear(get().yearSettings) })
|
||||
}
|
||||
// Opening Vacay lands on the period we are in, not on whichever year sorts
|
||||
// last. A reload (invite, live settings update) keeps the year the viewer is
|
||||
// looking at as long as it still exists — only the first load picks one.
|
||||
const previous = get().selectedYear
|
||||
const keepSelection = get().years.length > 0 && data.years.includes(previous)
|
||||
set({
|
||||
years: data.years,
|
||||
selectedYear: keepSelection ? previous : defaultPeriodYear(data.years, get().yearSettings),
|
||||
})
|
||||
},
|
||||
|
||||
addYear: async (year: number) => {
|
||||
@@ -320,9 +323,7 @@ export const useVacayStore = create<VacayState>((set, get) => ({
|
||||
const data = await api.removeYear(year)
|
||||
const updates: Partial<VacayState> = { years: data.years }
|
||||
if (get().selectedYear === year) {
|
||||
updates.selectedYear = data.years.length > 0
|
||||
? data.years[data.years.length - 1]
|
||||
: currentPeriodYear(get().yearSettings)
|
||||
updates.selectedYear = defaultPeriodYear(data.years, get().yearSettings)
|
||||
}
|
||||
set(updates)
|
||||
await get().loadStats()
|
||||
|
||||
Reference in New Issue
Block a user