mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-26 06:37:33 +00:00
feat(settings): every date picker starts its week on the day you pick
Settings > General > Language & region gets "Week starts on" (Monday, Sunday or Saturday), and Admin > User Defaults a default for everyone who has not picked one. Monday stays the built-in value, so nothing changes for anyone who leaves it alone. Both calendar implementations follow it: CustomDatePicker, used by trip dates, bookings, costs, to-dos and the admin school holidays on desktop and phone, and the journey date picker, whose weekday headers were hardcoded English and now speak the app language. The grid math and the header row live in one helper (utils/calendarWeek.ts). The day names on the options come from Intl, so only the label and a hint needed translating. Vacay keeps its own per-plan week start. The value is validated like the other regional settings: VALID_VALUES for admin defaults, the display-settings MCP tool, and the managed key list. Addresses discussion #2029.
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
// FE-UTIL-CALWEEK-001 to FE-UTIL-CALWEEK-005
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import { leadingBlanks, weekdayLabels, weekStartDay, weekStartOptions } from './calendarWeek'
|
||||
|
||||
describe('calendarWeek (#2029)', () => {
|
||||
it('FE-UTIL-CALWEEK-001: maps the setting to getDay() and falls back to Monday', () => {
|
||||
expect(weekStartDay('monday')).toBe(1)
|
||||
expect(weekStartDay('sunday')).toBe(0)
|
||||
expect(weekStartDay('saturday')).toBe(6)
|
||||
// Missing, unknown and the Vacay-style number all read as Monday, what every picker showed before.
|
||||
expect(weekStartDay(undefined)).toBe(1)
|
||||
expect(weekStartDay('friday')).toBe(1)
|
||||
expect(weekStartDay(0)).toBe(1)
|
||||
})
|
||||
|
||||
it('FE-UTIL-CALWEEK-002: counts the blanks before the 1st for each first weekday', () => {
|
||||
// 1 March 2026 is a Sunday.
|
||||
expect(leadingBlanks(2026, 2, 1)).toBe(6)
|
||||
expect(leadingBlanks(2026, 2, 0)).toBe(0)
|
||||
expect(leadingBlanks(2026, 2, 6)).toBe(1)
|
||||
// 1 January 2024 is a Monday.
|
||||
expect(leadingBlanks(2024, 0, 1)).toBe(0)
|
||||
expect(leadingBlanks(2024, 0, 0)).toBe(1)
|
||||
expect(leadingBlanks(2024, 0, 6)).toBe(2)
|
||||
})
|
||||
|
||||
it('FE-UTIL-CALWEEK-003: rotates the header row to the first weekday', () => {
|
||||
expect(weekdayLabels('en-US', 1, 'short')).toEqual(['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'])
|
||||
expect(weekdayLabels('en-US', 0, 'short')).toEqual(['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'])
|
||||
expect(weekdayLabels('en-US', 6, 'short')).toEqual(['Sat', 'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri'])
|
||||
expect(weekdayLabels('en-US', 0).join('')).toBe('SMTWTFS')
|
||||
})
|
||||
|
||||
it('FE-UTIL-CALWEEK-004: speaks the app locale, not the browser one', () => {
|
||||
expect(weekdayLabels('de-DE', 1, 'short')[0]).toBe('Mo')
|
||||
})
|
||||
|
||||
it('FE-UTIL-CALWEEK-005: labels the choices with capitalised day names in the app language', () => {
|
||||
expect(weekStartOptions('en-US')).toEqual([
|
||||
{ value: 'monday', label: 'Monday' },
|
||||
{ value: 'sunday', label: 'Sunday' },
|
||||
{ value: 'saturday', label: 'Saturday' },
|
||||
])
|
||||
// French writes weekday names lower-case; standing alone on a button they start upper-case.
|
||||
expect(weekStartOptions('fr-FR').map(o => o.label)).toEqual(['Lundi', 'Dimanche', 'Samedi'])
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,42 @@
|
||||
import { DEFAULT_WEEK_START, WEEK_START_DAY, WEEK_START_VALUES, type WeekStart } from '@trek/shared'
|
||||
|
||||
/**
|
||||
* The calendar-grid half of the week_start setting (#2029): the one place a
|
||||
* date picker asks which weekday opens a row, how many blanks go before the
|
||||
* 1st, and what the header row says. Both pickers (CustomDatePicker and the
|
||||
* journey DatePicker) read it, so they cannot drift apart again.
|
||||
*/
|
||||
|
||||
/** Date#getDay() of the first weekday; a missing or unknown value is Monday, what every picker did before the setting. */
|
||||
export function weekStartDay(value: unknown): number {
|
||||
const known = (WEEK_START_VALUES as readonly unknown[]).includes(value)
|
||||
return WEEK_START_DAY[known ? value as WeekStart : DEFAULT_WEEK_START]
|
||||
}
|
||||
|
||||
/** Blank cells before the 1st of the month when rows begin on `weekStart`. */
|
||||
export function leadingBlanks(year: number, month: number, weekStart: number): number {
|
||||
return (new Date(year, month, 1).getDay() - weekStart + 7) % 7
|
||||
}
|
||||
|
||||
// 7 January 2024 is a Sunday, so the 7th plus a getDay() number is that weekday.
|
||||
function sampleDay(day: number): Date {
|
||||
return new Date(2024, 0, 7 + day)
|
||||
}
|
||||
|
||||
/** The header row of a month grid, starting on `weekStart`, in the app's locale. */
|
||||
export function weekdayLabels(locale: string, weekStart: number, weekday: 'narrow' | 'short' = 'narrow'): string[] {
|
||||
return Array.from({ length: 7 }, (_, i) => sampleDay((weekStart + i) % 7).toLocaleDateString(locale, { weekday }))
|
||||
}
|
||||
|
||||
/**
|
||||
* The choices for the setting, labelled with the day's own name in the app's
|
||||
* language, so no locale file has to spell out seven weekdays. Capitalised
|
||||
* because several languages write weekday names lower-case and these stand
|
||||
* alone on a button.
|
||||
*/
|
||||
export function weekStartOptions(locale: string): { value: WeekStart; label: string }[] {
|
||||
return WEEK_START_VALUES.map(value => {
|
||||
const name = sampleDay(WEEK_START_DAY[value]).toLocaleDateString(locale, { weekday: 'long' })
|
||||
return { value, label: name.charAt(0).toLocaleUpperCase(locale) + name.slice(1) }
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user