mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-27 07:07:31 +00:00
feat(trips): shortening a trip says first which days go and what is on them
The trip dialog on the desktop and the trip sheet on a phone now ask before a save whose new dates remove days with something on them. The question names those days the way the day list does and lists what goes with them in the same rows the delete day question uses: planned places stay in the place list, notes and day texts are deleted, bookings stay under Bookings, and a stay that checks in or out on a removed day goes as a whole while its booking and expense remain, shown in the danger tone. A moved start gets both questions in one step, with the booking hint following the chosen shift mode, and a save that only drops empty days asks nothing. The rule that lays the days out moved into shared as planDayGrid, with resolveDayGridRange deciding whether the rows are rebuilt at all. generateDays on the server now carries that plan out in one transaction, so the warning names exactly the days the save takes, and a seeded fuzz test holds the new rebuild to the old one over 300 random day grids. The update_trip tool says in its description what shortening takes and lists removed_days in its answer. Alongside, the dialog sends a day count only when it changed, so a stale count no longer trims a day just added, stays are reloaded after every date change including the one made on a phone, and the date shift tip now points to adding a day under Reorder days.
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
// FE-UTIL-DAYDEL-001 to FE-UTIL-DAYDEL-008
|
||||
// FE-UTIL-DAYDEL-001 to FE-UTIL-DAYDEL-012
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import { buildAssignment, buildDay, buildDayNote, buildPlace, buildReservation } from '../../tests/helpers/factories'
|
||||
import { contentOnDays, dayDeleteImpact, deleteDayBlockedReason, type DayContentData } from './dayDeleteImpact'
|
||||
@@ -122,3 +122,51 @@ describe('dayDeleteImpact', () => {
|
||||
expect(deleteDayBlockedReason(3, false, t)).toBeNull()
|
||||
})
|
||||
})
|
||||
|
||||
describe('contentOnDays over several days (the shrink warning)', () => {
|
||||
it('FE-UTIL-DAYDEL-009: counts across all the days at once', () => {
|
||||
const data = {
|
||||
...empty(),
|
||||
assignments: {
|
||||
'7': [buildAssignment({ day_id: 7, place: buildPlace({ id: 50 }) })],
|
||||
'8': [buildAssignment({ day_id: 8, place: buildPlace({ id: 51 }) })],
|
||||
},
|
||||
dayNotes: { '7': [buildDayNote({ day_id: 7 })], '8': [buildDayNote({ day_id: 8 }), buildDayNote({ day_id: 8 })] },
|
||||
reservations: [buildReservation({ id: 1, day_id: 7 }), buildReservation({ id: 2, day_id: 8 }), buildReservation({ id: 3, day_id: 9 })],
|
||||
}
|
||||
const days = [buildDay({ id: 7, title: 'Harbour day' }), buildDay({ id: 8, notes: 'Ferry at nine' })]
|
||||
expect(contentOnDays(days, data)).toEqual({ places: 2, notes: 3, texts: 2, bookings: 2, stays: [] })
|
||||
})
|
||||
|
||||
it('FE-UTIL-DAYDEL-010: a stay from one of the days to another counts once', () => {
|
||||
const data = { ...empty(), accommodations: [stay({ id: 9, start_day_id: 7, end_day_id: 8, place_name: 'Harbour Hotel' })] }
|
||||
expect(contentOnDays([buildDay({ id: 7 }), buildDay({ id: 8 })], data).stays).toEqual([
|
||||
{ id: 9, name: 'Harbour Hotel', booking: null },
|
||||
])
|
||||
})
|
||||
|
||||
it('FE-UTIL-DAYDEL-011: the hotel booking of such a stay is the stay, not another booking', () => {
|
||||
const data = {
|
||||
...empty(),
|
||||
accommodations: [stay({ id: 9, start_day_id: 7, end_day_id: 8 })],
|
||||
reservations: [buildReservation({ id: 4, day_id: 7, end_day_id: 8, type: 'hotel', title: 'Harbour, 1 night', accommodation_id: 9 })],
|
||||
}
|
||||
expect(contentOnDays([buildDay({ id: 7 }), buildDay({ id: 8 })], data)).toMatchObject({
|
||||
bookings: 0,
|
||||
stays: [{ id: 9, booking: 'Harbour, 1 night' }],
|
||||
})
|
||||
})
|
||||
|
||||
it('FE-UTIL-DAYDEL-012: the same place on two days is one place, and the stops a stay wrote are none', () => {
|
||||
const museum = buildPlace({ id: 50 })
|
||||
const hotel = buildPlace({ id: 52 })
|
||||
const data = {
|
||||
...empty(),
|
||||
assignments: {
|
||||
'7': [buildAssignment({ day_id: 7, place: museum }), buildAssignment({ day_id: 7, place: hotel, accommodation_id: 9 })],
|
||||
'8': [buildAssignment({ day_id: 8, place: museum }), buildAssignment({ day_id: 8, place: hotel, accommodation_id: 9 })],
|
||||
},
|
||||
}
|
||||
expect(contentOnDays([buildDay({ id: 7 }), buildDay({ id: 8 })], data).places).toBe(1)
|
||||
})
|
||||
})
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
// FE-UTIL-DAYLINES-001 to FE-UTIL-DAYLINES-004
|
||||
// FE-UTIL-DAYLINES-001 to FE-UTIL-DAYLINES-008
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import { BedDouble, CalendarCheck } from 'lucide-react'
|
||||
import { deleteDayLines, impactLines } from './dayImpactLines'
|
||||
import { BedDouble, CalendarCheck, CalendarClock } from 'lucide-react'
|
||||
import { dayChips, deleteDayLines, impactLines, shrinkTripLines } from './dayImpactLines'
|
||||
import type { DayDeleteImpact } from './dayDeleteImpact'
|
||||
import type { TripRangeImpact } from './tripRangeImpact'
|
||||
|
||||
const t = (key: string, params?: Record<string, string | number>) =>
|
||||
params ? `${key}(${Object.entries(params).map(([k, v]) => `${k}=${v}`).join(',')})` : key
|
||||
@@ -54,3 +55,48 @@ describe('deleteDayLines', () => {
|
||||
expect(lines).toEqual([{ key: 'empty', icon: CalendarCheck, tone: 'muted', text: 'dayplan.deleteDayEmpty' }])
|
||||
})
|
||||
})
|
||||
|
||||
describe('shrinkTripLines', () => {
|
||||
const shrink = (content: Partial<TripRangeImpact['content']>, startMoved = false): TripRangeImpact => ({
|
||||
removedDays: [],
|
||||
startMoved,
|
||||
content: { places: 0, notes: 0, texts: 0, bookings: 0, stays: [], ...content },
|
||||
})
|
||||
|
||||
it('FE-UTIL-DAYLINES-005: the same rows, with the shortening hints: a removed stay leaves its booking behind', () => {
|
||||
const lines = shrinkTripLines(shrink({ places: 2, stays: [{ id: 9, name: 'Harbour Hotel', booking: 'HH' }, { id: 10, name: 'Alma', booking: null }] }), t)
|
||||
expect(lines.map(l => [l.key, l.tone, l.hint])).toEqual([
|
||||
['stay-9', 'danger', 'dashboard.shrinkStayBookedHint(booking=HH)'],
|
||||
['stay-10', 'danger', 'dashboard.shrinkStayHint'],
|
||||
['places', 'neutral', 'dayplan.impactPlacesHint'],
|
||||
])
|
||||
})
|
||||
|
||||
it('FE-UTIL-DAYLINES-006: the booking hint follows the shift mode', () => {
|
||||
const hint = (mode?: 'keep_bookings' | 'shift_all') => shrinkTripLines(shrink({ bookings: 2 }), t, mode)[0].hint
|
||||
expect(hint()).toBe('dashboard.shrinkBookingsHint')
|
||||
expect(hint('keep_bookings')).toBe('dashboard.shrinkBookingsHint')
|
||||
expect(hint('shift_all')).toBe('dashboard.shrinkBookingsShiftHint')
|
||||
// Deleting a day keeps its own hint whatever mode is passed along.
|
||||
expect(impactLines(shrink({ bookings: 1 }).content, t, 'deleteDay', { shiftMode: 'shift_all' })[0].hint).toBe('dayplan.deleteDayBookingsHint')
|
||||
})
|
||||
|
||||
it('FE-UTIL-DAYLINES-007: a moved start that still takes the last days gets a last row that says so', () => {
|
||||
const lines = shrinkTripLines(shrink({ notes: 1 }, true), t)
|
||||
expect(lines[lines.length - 1]).toEqual({
|
||||
key: 'lastDays',
|
||||
icon: CalendarClock,
|
||||
tone: 'warning',
|
||||
text: 'dashboard.shrinkLastDays',
|
||||
hint: 'dashboard.shrinkLastDaysHint',
|
||||
})
|
||||
expect(shrinkTripLines(shrink({ notes: 1 }), t).map(l => l.key)).toEqual(['notes'])
|
||||
})
|
||||
|
||||
it('FE-UTIL-DAYLINES-008: day chips name six days and sum up the rest', () => {
|
||||
const labels = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun', 'Day 8']
|
||||
expect(dayChips(labels.slice(0, 6), t)).toEqual(labels.slice(0, 6))
|
||||
expect(dayChips(labels, t)).toEqual([...labels.slice(0, 6), 'dashboard.shrinkMoreDays(count=2)'])
|
||||
expect(dayChips(labels, t, 3)).toEqual(['Mon', 'Tue', 'Wed', 'dashboard.shrinkMoreDays(count=5)'])
|
||||
})
|
||||
})
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { BedDouble, CalendarCheck, CalendarClock, CalendarMinus, MapPin, StickyNote, Ticket, Type } from 'lucide-react'
|
||||
import type { LucideIcon } from 'lucide-react'
|
||||
import type { DayContent, DayDeleteImpact } from './dayDeleteImpact'
|
||||
import type { TripRangeImpact } from './tripRangeImpact'
|
||||
|
||||
type Translate = (key: string, params?: Record<string, string | number>) => string
|
||||
|
||||
@@ -21,11 +22,15 @@ export interface ImpactLine {
|
||||
}
|
||||
|
||||
/**
|
||||
* The question the list answers. Deleting a day cancels a stay cleanly and lets
|
||||
* go of its bookings; other ways of losing days treat both differently, so the
|
||||
* hints are looked up per variant while the rows themselves stay the same.
|
||||
* The question the list answers. Deleting a day cancels a stay cleanly, with
|
||||
* its booking and expense. Shortening a trip removes the stay through the day
|
||||
* rows instead and leaves its booking behind. So the hints are looked up per
|
||||
* variant while the rows themselves stay the same.
|
||||
*/
|
||||
export type ImpactVariant = 'deleteDay'
|
||||
export type ImpactVariant = 'deleteDay' | 'shrinkTrip'
|
||||
|
||||
/** How bookings follow new dates, as the trip dialog lets the traveller choose. */
|
||||
export type ShiftMode = 'keep_bookings' | 'shift_all'
|
||||
|
||||
const HINTS: Record<ImpactVariant, { bookings: string; stay: string; stayBooked: string }> = {
|
||||
deleteDay: {
|
||||
@@ -33,6 +38,11 @@ const HINTS: Record<ImpactVariant, { bookings: string; stay: string; stayBooked:
|
||||
stay: 'dayplan.deleteDayStayHint',
|
||||
stayBooked: 'dayplan.deleteDayStayBookedHint',
|
||||
},
|
||||
shrinkTrip: {
|
||||
bookings: 'dashboard.shrinkBookingsHint',
|
||||
stay: 'dashboard.shrinkStayHint',
|
||||
stayBooked: 'dashboard.shrinkStayBookedHint',
|
||||
},
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -40,8 +50,17 @@ const HINTS: Record<ImpactVariant, { bookings: string; stay: string; stayBooked:
|
||||
* money, then places, notes, day texts and bookings. Kinds with nothing in them
|
||||
* are left out.
|
||||
*/
|
||||
export function impactLines(content: DayContent, t: Translate, variant: ImpactVariant): ImpactLine[] {
|
||||
const hints = HINTS[variant]
|
||||
export function impactLines(
|
||||
content: DayContent,
|
||||
t: Translate,
|
||||
variant: ImpactVariant,
|
||||
options: { shiftMode?: ShiftMode } = {},
|
||||
): ImpactLine[] {
|
||||
// With "shift everything" a booking stays glued to its day row, so one on a
|
||||
// removed day keeps no day at all; the default puts it back by its date.
|
||||
const hints = variant === 'shrinkTrip' && options.shiftMode === 'shift_all'
|
||||
? { ...HINTS.shrinkTrip, bookings: 'dashboard.shrinkBookingsShiftHint' }
|
||||
: HINTS[variant]
|
||||
const lines: ImpactLine[] = content.stays.map(stay => ({
|
||||
key: `stay-${stay.id}`,
|
||||
icon: BedDouble,
|
||||
@@ -95,3 +114,31 @@ export function deleteDayLines(impact: DayDeleteImpact, t: Translate, formatDate
|
||||
}
|
||||
return lines
|
||||
}
|
||||
|
||||
/** Most day chips the list names before it sums up the rest. */
|
||||
export const MAX_DAY_CHIPS = 6
|
||||
|
||||
/** The removed days as chip labels: the first few by name, the rest as "+n more". */
|
||||
export function dayChips(labels: string[], t: Translate, max = MAX_DAY_CHIPS): string[] {
|
||||
if (labels.length <= max) return labels
|
||||
return [...labels.slice(0, max), t('dashboard.shrinkMoreDays', { count: labels.length - max })]
|
||||
}
|
||||
|
||||
/**
|
||||
* The full list for the warning before a trip is shortened: what sits on the
|
||||
* days that go, with the booking hint following the chosen shift mode, and a
|
||||
* last row when a moved start still takes the last days.
|
||||
*/
|
||||
export function shrinkTripLines(impact: TripRangeImpact, t: Translate, shiftMode: ShiftMode = 'keep_bookings'): ImpactLine[] {
|
||||
const lines = impactLines(impact.content, t, 'shrinkTrip', { shiftMode })
|
||||
if (impact.startMoved) {
|
||||
lines.push({
|
||||
key: 'lastDays',
|
||||
icon: CalendarClock,
|
||||
tone: 'warning',
|
||||
text: t('dashboard.shrinkLastDays'),
|
||||
hint: t('dashboard.shrinkLastDaysHint'),
|
||||
})
|
||||
}
|
||||
return lines
|
||||
}
|
||||
|
||||
@@ -0,0 +1,111 @@
|
||||
// FE-UTIL-RANGEDEL-001 to FE-UTIL-RANGEDEL-008
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import { planDayGrid } from '@trek/shared'
|
||||
import { buildAssignment, buildDay, buildPlace, buildReservation } from '../../tests/helpers/factories'
|
||||
import { hasVisibleContent, tripRangeImpact } from './tripRangeImpact'
|
||||
import type { DayContentData } from './dayDeleteImpact'
|
||||
import type { Accommodation, Day } from '../types'
|
||||
|
||||
/**
|
||||
* The trip of the plan: 1 to 10 October, a place on every day (ids 1 to 10),
|
||||
* then two days without a date: 11 is empty, 12 has a place on it.
|
||||
*/
|
||||
function tenDays(): Day[] {
|
||||
return [
|
||||
...Array.from({ length: 10 }, (_, i) =>
|
||||
buildDay({ id: i + 1, day_number: i + 1, date: `2026-10-${String(i + 1).padStart(2, '0')}` })),
|
||||
buildDay({ id: 11, day_number: 11, date: null }),
|
||||
buildDay({ id: 12, day_number: 12, date: null }),
|
||||
]
|
||||
}
|
||||
|
||||
function contentOn(days: Day[]): DayContentData {
|
||||
const assignments: DayContentData['assignments'] = {}
|
||||
for (const day of days) {
|
||||
if (day.id !== 11) assignments[String(day.id)] = [buildAssignment({ day_id: day.id, place: buildPlace({ id: 100 + day.id }) })]
|
||||
}
|
||||
return { assignments, dayNotes: {}, reservations: [], accommodations: [] }
|
||||
}
|
||||
|
||||
function impactFor(start: string | null, end: string | null, options = { startMoved: false }, days = tenDays(), data = contentOn(days)) {
|
||||
const plan = planDayGrid({
|
||||
days: days.map(d => ({
|
||||
id: d.id,
|
||||
day_number: d.day_number ?? 0,
|
||||
date: d.date,
|
||||
hasPlanItems: (data.assignments[String(d.id)]?.length ?? 0) > 0,
|
||||
})),
|
||||
stays: data.accommodations,
|
||||
startDate: start,
|
||||
endDate: end,
|
||||
})
|
||||
return tripRangeImpact(plan, days, data, options)
|
||||
}
|
||||
|
||||
const ids = (impact: ReturnType<typeof impactFor>) => impact.removedDays.map(r => r.day.id)
|
||||
|
||||
describe('tripRangeImpact', () => {
|
||||
it('FE-UTIL-RANGEDEL-001: an earlier end takes the last days with their places, and the empty spare day', () => {
|
||||
const impact = impactFor('2026-10-01', '2026-10-08')
|
||||
expect(ids(impact)).toEqual([9, 10, 11])
|
||||
expect(impact.content.places).toBe(2)
|
||||
expect(impact.startMoved).toBe(false)
|
||||
expect(hasVisibleContent(impact)).toBe(true)
|
||||
})
|
||||
|
||||
it('FE-UTIL-RANGEDEL-002: a later start with the same end still takes the last days, and says so', () => {
|
||||
const impact = impactFor('2026-10-03', '2026-10-10', { startMoved: true })
|
||||
expect(ids(impact)).toEqual([9, 10, 11])
|
||||
expect(impact.startMoved).toBe(true)
|
||||
})
|
||||
|
||||
it('FE-UTIL-RANGEDEL-003: moving the whole range drops only the empty spare day, which is not worth a word', () => {
|
||||
const impact = impactFor('2026-10-05', '2026-10-14', { startMoved: true })
|
||||
expect(ids(impact)).toEqual([11])
|
||||
// Nothing dated went, so the "last days" row would be wrong here.
|
||||
expect(impact.startMoved).toBe(false)
|
||||
expect(hasVisibleContent(impact)).toBe(false)
|
||||
})
|
||||
|
||||
it('FE-UTIL-RANGEDEL-004: a later end removes nothing', () => {
|
||||
const impact = impactFor('2026-10-01', '2026-10-11')
|
||||
expect(impact.removedDays).toEqual([])
|
||||
expect(hasVisibleContent(impact)).toBe(false)
|
||||
})
|
||||
|
||||
it('FE-UTIL-RANGEDEL-005: clearing the dates keeps every day', () => {
|
||||
const impact = impactFor(null, null)
|
||||
expect(impact.removedDays).toEqual([])
|
||||
expect(impact.content).toEqual({ places: 0, notes: 0, texts: 0, bookings: 0, stays: [] })
|
||||
})
|
||||
|
||||
it('FE-UTIL-RANGEDEL-006: a spare day with only a title goes, and its title is what the warning names', () => {
|
||||
const days = tenDays().map(d => (d.id === 11 ? { ...d, title: 'Buffer day' } : d))
|
||||
const impact = impactFor('2026-10-05', '2026-10-14', { startMoved: false }, days)
|
||||
expect(ids(impact)).toEqual([11])
|
||||
expect(impact.content.texts).toBe(1)
|
||||
expect(hasVisibleContent(impact)).toBe(true)
|
||||
})
|
||||
|
||||
it('FE-UTIL-RANGEDEL-007: a stay on a removed day goes as a whole, with its booking, and a booking there counts', () => {
|
||||
const days = tenDays()
|
||||
const data = contentOn(days)
|
||||
data.accommodations = [
|
||||
{ id: 5, trip_id: 1, start_day_id: 7, end_day_id: 9, place_name: 'Harbour Hotel' } as Accommodation,
|
||||
{ id: 6, trip_id: 1, start_day_id: 2, end_day_id: 4, place_name: 'Pension Alma' } as Accommodation,
|
||||
]
|
||||
data.reservations = [
|
||||
buildReservation({ id: 40, day_id: 7, type: 'hotel', title: 'Harbour, 2 nights', accommodation_id: 5 }),
|
||||
buildReservation({ id: 41, day_id: 10, title: 'Ferry' }),
|
||||
]
|
||||
const impact = impactFor('2026-10-01', '2026-10-08', { startMoved: false }, days, data)
|
||||
expect(impact.content.stays).toEqual([{ id: 5, name: 'Harbour Hotel', booking: 'Harbour, 2 nights' }])
|
||||
expect(impact.content.bookings).toBe(1)
|
||||
})
|
||||
|
||||
it('FE-UTIL-RANGEDEL-008: the removed days come in list order, each with its place in the list', () => {
|
||||
const days = [...tenDays()].reverse()
|
||||
const impact = impactFor('2026-10-01', '2026-10-09', { startMoved: false }, days)
|
||||
expect(impact.removedDays.map(r => [r.day.id, r.index])).toEqual([[10, 9], [11, 10]])
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,57 @@
|
||||
import type { DayGridPlan } from '@trek/shared'
|
||||
import type { Day } from '../types'
|
||||
import { contentOnDays, type DayContent, type DayContentData } from './dayDeleteImpact'
|
||||
|
||||
/** A day a save would remove, with its place in the day list (for "Day n"). */
|
||||
export interface RemovedDay {
|
||||
day: Day
|
||||
index: number
|
||||
}
|
||||
|
||||
/** What a new range takes away: the days, in list order, and what sits on them. */
|
||||
export interface TripRangeImpact {
|
||||
removedDays: RemovedDay[]
|
||||
/**
|
||||
* The start moved while dated days fall off the end. Plans follow their
|
||||
* position, so it is the last days that go, not the first: worth saying,
|
||||
* because it is the opposite of what moving the start suggests.
|
||||
*/
|
||||
startMoved: boolean
|
||||
content: DayContent
|
||||
}
|
||||
|
||||
const byNumber = (a: Day, b: Day): number => (a.day_number ?? 0) - (b.day_number ?? 0)
|
||||
|
||||
/**
|
||||
* Read a planDayGrid plan as the trip dialog shows it: which of the current
|
||||
* days go and everything on them, counted the same way the delete day
|
||||
* question counts a single day.
|
||||
*/
|
||||
export function tripRangeImpact(
|
||||
plan: DayGridPlan,
|
||||
days: Day[],
|
||||
data: DayContentData,
|
||||
options: { startMoved: boolean },
|
||||
): TripRangeImpact {
|
||||
const gone = new Set(plan.removed.map(r => r.id))
|
||||
const removedDays = [...days]
|
||||
.sort(byNumber)
|
||||
.map((day, index) => ({ day, index }))
|
||||
.filter(({ day }) => gone.has(day.id))
|
||||
const dropsDatedDays = plan.removed.some(r => r.reason === 'overflow')
|
||||
return {
|
||||
removedDays,
|
||||
startMoved: options.startMoved && dropsDatedDays,
|
||||
content: contentOnDays(removedDays.map(r => r.day), data),
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether the days that go hold anything a traveller would miss. Empty days
|
||||
* come and go with every change of dates; asking about them would teach people
|
||||
* to click the question away.
|
||||
*/
|
||||
export function hasVisibleContent(impact: TripRangeImpact): boolean {
|
||||
const { places, notes, texts, bookings, stays } = impact.content
|
||||
return places + notes + texts + bookings + stays.length > 0
|
||||
}
|
||||
Reference in New Issue
Block a user