mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-21 19:40:34 +00:00
ec08915a16
Adding a day before a trip's start with "Keep bookings on their dates" moved almost everything a day earlier and emptied the last day. The day grid was always rebuilt by position: the n-th day row took the n-th date of the new range. With bookings kept on their dates, only timed bookings and stays were re-anchored afterwards. Places, day notes, bookings with no time and a booking's place in a day's order stayed on their re-dated rows and slid to the day before. Days added at both ends had the same problem. planDayGrid now takes keepDates. When the new range still holds every dated day, each of them keeps its date and the new days go around them. The server passes it unless the traveller chose to shift everything, and the trip dialog's warning check uses the same rule. A moved or shorter range still follows the positional rule the dialog describes, and "Shift everything" still moves the whole plan with the new start. Two things keyed outside the day rows followed the old rule. Road trip day boundaries are keyed by day number, so a new first day left each one on the day before its own. They now follow their days to their new numbers and go out to every screen. The owner's leave days moved by the change of start even when days were only added. They now move by as far as the trip's first day moved, which is nothing when days are only added. The help text of the option and the MCP description say what happens when days are added. Closes #3119
100 lines
4.3 KiB
TypeScript
100 lines
4.3 KiB
TypeScript
import { useCallback, useState } from 'react'
|
|
import { planDayGrid, resolveDayGridRange } from '@trek/shared'
|
|
import { useTranslation } from '../i18n'
|
|
import { dayRepo } from '../repo/dayRepo'
|
|
import { reservationRepo } from '../repo/reservationRepo'
|
|
import { accommodationRepo } from '../repo/accommodationRepo'
|
|
import { budgetRepo } from '../repo/budgetRepo'
|
|
import { dayLabel } from '../utils/dayLabel'
|
|
import { hasVisibleContent, tripRangeImpact, type TripRangeImpact } from '../utils/tripRangeImpact'
|
|
import type { AssignmentsMap, DayNotesMap, Trip } from '../types'
|
|
|
|
/** Days a save would remove, with the names the day list gives them. */
|
|
export interface RangeRemoval extends TripRangeImpact {
|
|
dayLabels: string[]
|
|
}
|
|
|
|
/**
|
|
* The answer before a save: the days it removes with something on them, null
|
|
* when it removes nothing worth a question, or 'unknown' when the trip's days
|
|
* could not be read, so the dialog can still warn in general terms.
|
|
*/
|
|
export type RangeCheck = RangeRemoval | 'unknown' | null
|
|
|
|
/** The fields of a trip save that move the day grid. */
|
|
export interface RangePayload {
|
|
start_date?: string | null
|
|
end_date?: string | null
|
|
day_count?: number | null
|
|
}
|
|
|
|
/**
|
|
* Asks, before a trip dialog saves new dates, which days the save would remove
|
|
* and what is on them. It reads the trip's days, bookings and stays through the
|
|
* repos and lays the days out with planDayGrid, the rule the server rebuilds
|
|
* them by, so the warning names exactly the days the save takes. A save that
|
|
* does not rebuild the days is answered without reading anything. The expenses
|
|
* only tell whether a removed stay's booking has one; when they cannot be read,
|
|
* the warning says it may.
|
|
*
|
|
* Used by the desktop trip dialog and the phone's trip sheet alike; both only
|
|
* render what comes back.
|
|
*/
|
|
export function useTripRangeGuard(): { checking: boolean; check: (trip: Pick<Trip, 'id' | 'start_date' | 'end_date'>, payload: RangePayload) => Promise<RangeCheck> } {
|
|
const { t, locale } = useTranslation()
|
|
const [checking, setChecking] = useState(false)
|
|
|
|
const check = useCallback(async (trip: Pick<Trip, 'id' | 'start_date' | 'end_date'>, payload: RangePayload): Promise<RangeCheck> => {
|
|
const range = resolveDayGridRange(trip, payload)
|
|
if (!range.regenerate) return null
|
|
setChecking(true)
|
|
try {
|
|
const [{ days }, { reservations }, { accommodations }, expenses] = await Promise.all([
|
|
dayRepo.list(trip.id),
|
|
reservationRepo.list(trip.id),
|
|
accommodationRepo.list(trip.id),
|
|
budgetRepo.list(trip.id).catch((err: unknown) => {
|
|
console.warn('Expenses not read for the warning before new dates:', err)
|
|
return null
|
|
}),
|
|
])
|
|
const plan = planDayGrid({
|
|
days: days.map(d => ({
|
|
id: d.id,
|
|
day_number: d.day_number ?? 0,
|
|
date: d.date,
|
|
hasPlanItems: (d.assignments?.length ?? 0) > 0 || (d.notes_items?.length ?? 0) > 0,
|
|
})),
|
|
stays: accommodations,
|
|
startDate: range.newStart,
|
|
endDate: range.newEnd,
|
|
dayCount: range.dayCount,
|
|
// The rule the server lays the days out by when bookings keep their dates,
|
|
// the default either dialog saves with. Shifting everything moves no day
|
|
// that this keeps, so the dated days it removes are the same.
|
|
keepDates: true,
|
|
})
|
|
if (plan.removed.length === 0) return null
|
|
|
|
const assignments: AssignmentsMap = {}
|
|
const dayNotes: DayNotesMap = {}
|
|
for (const day of days) {
|
|
assignments[String(day.id)] = day.assignments ?? []
|
|
dayNotes[String(day.id)] = day.notes_items ?? []
|
|
}
|
|
const startMoved = !!trip.start_date && !!range.newStart && !!range.newEnd && range.newStart !== trip.start_date
|
|
const budget = expenses ? { items: expenses.items } : undefined
|
|
const impact = tripRangeImpact(plan, days, { assignments, dayNotes, reservations, accommodations, budget }, { startMoved })
|
|
if (!hasVisibleContent(impact)) return null
|
|
return { ...impact, dayLabels: impact.removedDays.map(({ day, index }) => dayLabel(day, index, t, locale)) }
|
|
} catch (err: unknown) {
|
|
console.error('Failed to read the days a new range removes:', err)
|
|
return 'unknown'
|
|
} finally {
|
|
setChecking(false)
|
|
}
|
|
}, [t, locale])
|
|
|
|
return { checking, check }
|
|
}
|