mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-05 02:14:05 +00:00
fix(trips): keep accommodations on their dates when the trip range shifts (#1288)
The v3.1.3 fix re-anchored dated bookings after a trip date change but
explicitly excluded hotels: day_accommodations has no absolute date columns,
so stays remained glued to positionally re-dated day rows and shifted with
the range. updateTrip now snapshots day dates before generateDays and a new
resyncAccommodationDays re-anchors each stay (and its linked hotel
reservation, restamping its stale reservation_time) to the days holding its
pre-change dates; out-of-range stays stay glued so whole-trip moves still
shift together. Unlinked dated hotels resync like any booking, and the
date-change block is wrapped in a transaction.
Changing the start date now also asks how plans should follow via a new
date_shift_mode field ('keep_bookings' default / 'shift_all', which reuses
the reorder/insert restamp path to glue everything), exposed in the trip
edit modal (all 22 locales), the shared contract, and the MCP update_trip
tool. Clients no longer show stale state: the initiator reloads
reservations + accommodations after saving, collaborators refetch on a
date-changing trip:updated, and reconnect hydration nudges the planner's
accommodations too.
This commit is contained in:
@@ -368,6 +368,79 @@ describe('TripFormModal', () => {
|
||||
expect(onSave).toHaveBeenCalledWith(expect.objectContaining({ currency: 'EUR' }));
|
||||
});
|
||||
|
||||
// Changing the start of a dated trip must go through the date-shift choice step (#1288).
|
||||
const changeStartDate = async (user: ReturnType<typeof userEvent.setup>, iso: string) => {
|
||||
await user.click(screen.getAllByRole('button', { name: 'Enter date manually' })[0]);
|
||||
const input = screen.getByPlaceholderText('DD.MM.YYYY');
|
||||
fireEvent.change(input, { target: { value: iso } });
|
||||
fireEvent.keyDown(input, { key: 'Enter' });
|
||||
};
|
||||
|
||||
it('FE-COMP-TRIPFORM-035: changing the start date shows the choice step and saves with keep_bookings by default', async () => {
|
||||
const user = userEvent.setup();
|
||||
const onSave = vi.fn().mockResolvedValue({});
|
||||
const trip = buildTrip({ id: 1, title: 'Dated Trip', start_date: '2025-06-01', end_date: '2025-06-05' });
|
||||
render(<TripFormModal {...defaultProps} trip={trip} onSave={onSave} />);
|
||||
|
||||
await changeStartDate(user, '2025-05-31');
|
||||
await user.click(screen.getByRole('button', { name: /Update/i }));
|
||||
|
||||
// The choice step appears instead of saving right away.
|
||||
await screen.findByText('Keep bookings on their dates');
|
||||
expect(onSave).not.toHaveBeenCalled();
|
||||
|
||||
await user.click(screen.getByRole('button', { name: /Update/i }));
|
||||
await waitFor(() => expect(onSave).toHaveBeenCalled());
|
||||
expect(onSave).toHaveBeenCalledWith(expect.objectContaining({
|
||||
start_date: '2025-05-31',
|
||||
date_shift_mode: 'keep_bookings',
|
||||
}));
|
||||
});
|
||||
|
||||
it('FE-COMP-TRIPFORM-036: picking "Shift everything" sends shift_all', async () => {
|
||||
const user = userEvent.setup();
|
||||
const onSave = vi.fn().mockResolvedValue({});
|
||||
const trip = buildTrip({ id: 1, title: 'Dated Trip', start_date: '2025-06-01', end_date: '2025-06-05' });
|
||||
render(<TripFormModal {...defaultProps} trip={trip} onSave={onSave} />);
|
||||
|
||||
await changeStartDate(user, '2025-06-02');
|
||||
await user.click(screen.getByRole('button', { name: /Update/i }));
|
||||
await user.click(await screen.findByRole('radio', { name: /Shift everything/i }));
|
||||
await user.click(screen.getByRole('button', { name: /Update/i }));
|
||||
|
||||
await waitFor(() => expect(onSave).toHaveBeenCalled());
|
||||
expect(onSave).toHaveBeenCalledWith(expect.objectContaining({ date_shift_mode: 'shift_all' }));
|
||||
});
|
||||
|
||||
it('FE-COMP-TRIPFORM-037: Back returns to the form without saving', async () => {
|
||||
const user = userEvent.setup();
|
||||
const onSave = vi.fn();
|
||||
const trip = buildTrip({ id: 1, title: 'Dated Trip', start_date: '2025-06-01', end_date: '2025-06-05' });
|
||||
render(<TripFormModal {...defaultProps} trip={trip} onSave={onSave} />);
|
||||
|
||||
await changeStartDate(user, '2025-05-30');
|
||||
await user.click(screen.getByRole('button', { name: /Update/i }));
|
||||
await screen.findByText('Keep bookings on their dates');
|
||||
|
||||
await user.click(screen.getByRole('button', { name: /Back/i }));
|
||||
await waitFor(() => expect(screen.queryByText('Keep bookings on their dates')).not.toBeInTheDocument());
|
||||
expect(screen.getByDisplayValue('Dated Trip')).toBeInTheDocument();
|
||||
expect(onSave).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('FE-COMP-TRIPFORM-038: an edit that keeps the dates saves directly without the choice step', async () => {
|
||||
const user = userEvent.setup();
|
||||
const onSave = vi.fn().mockResolvedValue({});
|
||||
const trip = buildTrip({ id: 1, title: 'Dated Trip', start_date: '2025-06-01', end_date: '2025-06-05' });
|
||||
render(<TripFormModal {...defaultProps} trip={trip} onSave={onSave} />);
|
||||
|
||||
await user.click(screen.getByRole('button', { name: /Update/i }));
|
||||
|
||||
await waitFor(() => expect(onSave).toHaveBeenCalled());
|
||||
expect(screen.queryByText('Keep bookings on their dates')).not.toBeInTheDocument();
|
||||
expect(onSave).toHaveBeenCalledWith(expect.not.objectContaining({ date_shift_mode: expect.anything() }));
|
||||
});
|
||||
|
||||
it('FE-COMP-TRIPFORM-034: picking a currency sends the new one on save', async () => {
|
||||
const user = userEvent.setup();
|
||||
const onSave = vi.fn().mockResolvedValue({});
|
||||
|
||||
@@ -14,12 +14,14 @@ import type { TripCreateRequest } from '@trek/shared'
|
||||
import { NumericInput } from '../shared/NumericInput'
|
||||
import { currenciesWith, SYMBOLS } from '../Budget/BudgetPanel.constants'
|
||||
|
||||
type DateShiftMode = 'keep_bookings' | 'shift_all'
|
||||
|
||||
interface TripFormModalProps {
|
||||
isOpen: boolean
|
||||
onClose: () => void
|
||||
// Create returns the new trip (so we can attach members / upload the cover);
|
||||
// update resolves without a payload.
|
||||
onSave: (data: TripCreateRequest) => Promise<{ trip?: Trip } | void> | void
|
||||
onSave: (data: TripCreateRequest & { date_shift_mode?: DateShiftMode }) => Promise<{ trip?: Trip } | void> | void
|
||||
trip: Trip | null
|
||||
onCoverUpdate?: (tripId: number, coverUrl: string | null) => void
|
||||
}
|
||||
@@ -70,6 +72,10 @@ export default function TripFormModal({ isOpen, onClose, onSave, trip, onCoverUp
|
||||
const [selectedMembers, setSelectedMembers] = useState<number[]>([])
|
||||
const [existingMembers, setExistingMembers] = useState<{ id: number; username: string }[]>([])
|
||||
const [memberSelectValue, setMemberSelectValue] = useState('')
|
||||
// Set when a start-date change on a dated trip needs the user to pick how the
|
||||
// itinerary follows the new dates (#1288); holds the payload awaiting that choice.
|
||||
const [pendingDateShift, setPendingDateShift] = useState<(TripCreateRequest & { date_shift_mode?: DateShiftMode }) | null>(null)
|
||||
const [dateShiftMode, setDateShiftMode] = useState<DateShiftMode>('keep_bookings')
|
||||
|
||||
useEffect(() => {
|
||||
if (trip) {
|
||||
@@ -97,6 +103,8 @@ export default function TripFormModal({ isOpen, onClose, onSave, trip, onCoverUp
|
||||
setCoverSearchResults([])
|
||||
setCoverSearchError('')
|
||||
setSelectedMembers([])
|
||||
setPendingDateShift(null)
|
||||
setDateShiftMode('keep_bookings')
|
||||
setError('')
|
||||
if (isOpen) {
|
||||
authApi.getAppConfig().then((c: { trip_reminders_enabled?: boolean }) => {
|
||||
@@ -130,17 +138,31 @@ export default function TripFormModal({ isOpen, onClose, onSave, trip, onCoverUp
|
||||
setError(t('dashboard.dayCountRequired')); return
|
||||
}
|
||||
}
|
||||
const payload: TripCreateRequest & { date_shift_mode?: DateShiftMode } = {
|
||||
title: formData.title.trim(),
|
||||
description: formData.description.trim() || null,
|
||||
start_date: formData.start_date || null,
|
||||
end_date: formData.end_date || null,
|
||||
currency: formData.currency,
|
||||
reminder_days: formData.reminder_days,
|
||||
...(!formData.start_date && !formData.end_date ? { day_count: Number(formData.day_count) } : {}),
|
||||
}
|
||||
// Moving the start of a dated trip shifts the whole day grid, so let the user
|
||||
// choose how bookings follow before anything is saved (#1288). End-date-only
|
||||
// changes don't shift days and dateless transitions have nothing to shift.
|
||||
if (isEditing && trip?.start_date && trip?.end_date
|
||||
&& payload.start_date && payload.end_date && payload.start_date !== trip.start_date) {
|
||||
setDateShiftMode('keep_bookings')
|
||||
setPendingDateShift(payload)
|
||||
return
|
||||
}
|
||||
await performSave(payload)
|
||||
}
|
||||
|
||||
const performSave = async (payload: TripCreateRequest & { date_shift_mode?: DateShiftMode }) => {
|
||||
setIsLoading(true)
|
||||
try {
|
||||
const result = await onSave({
|
||||
title: formData.title.trim(),
|
||||
description: formData.description.trim() || null,
|
||||
start_date: formData.start_date || null,
|
||||
end_date: formData.end_date || null,
|
||||
currency: formData.currency,
|
||||
reminder_days: formData.reminder_days,
|
||||
...(!formData.start_date && !formData.end_date ? { day_count: Number(formData.day_count) } : {}),
|
||||
})
|
||||
const result = await onSave(payload)
|
||||
const createdTrip = result ? result.trip : undefined
|
||||
// Add selected members for newly created trips
|
||||
if (selectedMembers.length > 0 && createdTrip?.id) {
|
||||
@@ -317,24 +339,64 @@ export default function TripFormModal({ isOpen, onClose, onSave, trip, onCoverUp
|
||||
<Modal
|
||||
isOpen={isOpen}
|
||||
onClose={onClose}
|
||||
title={isEditing ? t('dashboard.editTrip') : t('dashboard.createTrip')}
|
||||
title={pendingDateShift ? t('dashboard.dateShiftTitle') : isEditing ? t('dashboard.editTrip') : t('dashboard.createTrip')}
|
||||
size="md"
|
||||
footer={
|
||||
<div className="flex gap-3 justify-end">
|
||||
<button type="button" onClick={onClose}
|
||||
className="px-4 py-2 text-sm text-slate-600 hover:text-slate-800 border border-slate-200 rounded-lg hover:bg-slate-50 transition-colors">
|
||||
{t('common.cancel')}
|
||||
</button>
|
||||
<button onClick={handleSubmit} disabled={isLoading}
|
||||
className="px-4 py-2 text-sm bg-slate-900 hover:bg-slate-700 disabled:bg-slate-400 text-white rounded-lg transition-colors flex items-center gap-2">
|
||||
{isLoading
|
||||
? <><div className="w-4 h-4 border-2 border-white/30 border-t-white rounded-full animate-spin" />{t('common.saving')}</>
|
||||
: isEditing ? t('common.update') : t('dashboard.createTrip')}
|
||||
</button>
|
||||
{pendingDateShift ? (
|
||||
<>
|
||||
<button type="button" onClick={() => setPendingDateShift(null)} disabled={isLoading}
|
||||
className="px-4 py-2 text-sm text-slate-600 hover:text-slate-800 border border-slate-200 rounded-lg hover:bg-slate-50 transition-colors">
|
||||
{t('common.back')}
|
||||
</button>
|
||||
<button onClick={() => performSave({ ...pendingDateShift, date_shift_mode: dateShiftMode })} disabled={isLoading}
|
||||
className="px-4 py-2 text-sm bg-slate-900 hover:bg-slate-700 disabled:bg-slate-400 text-white rounded-lg transition-colors flex items-center gap-2">
|
||||
{isLoading
|
||||
? <><div className="w-4 h-4 border-2 border-white/30 border-t-white rounded-full animate-spin" />{t('common.saving')}</>
|
||||
: t('common.update')}
|
||||
</button>
|
||||
</>
|
||||
) : (
|
||||
<>
|
||||
<button type="button" onClick={onClose}
|
||||
className="px-4 py-2 text-sm text-slate-600 hover:text-slate-800 border border-slate-200 rounded-lg hover:bg-slate-50 transition-colors">
|
||||
{t('common.cancel')}
|
||||
</button>
|
||||
<button onClick={handleSubmit} disabled={isLoading}
|
||||
className="px-4 py-2 text-sm bg-slate-900 hover:bg-slate-700 disabled:bg-slate-400 text-white rounded-lg transition-colors flex items-center gap-2">
|
||||
{isLoading
|
||||
? <><div className="w-4 h-4 border-2 border-white/30 border-t-white rounded-full animate-spin" />{t('common.saving')}</>
|
||||
: isEditing ? t('common.update') : t('dashboard.createTrip')}
|
||||
</button>
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
}
|
||||
>
|
||||
<form onSubmit={handleSubmit} className="space-y-4" onPaste={handlePaste}>
|
||||
{pendingDateShift && (
|
||||
<div className="space-y-3">
|
||||
{error && (
|
||||
<div className="p-3 bg-red-50 border border-red-200 rounded-lg text-sm text-red-600">{error}</div>
|
||||
)}
|
||||
<p className="text-sm text-slate-600">{t('dashboard.dateShiftIntro')}</p>
|
||||
{([
|
||||
{ mode: 'keep_bookings' as DateShiftMode, label: t('dashboard.dateShiftKeepBookings'), desc: t('dashboard.dateShiftKeepBookingsDesc') },
|
||||
{ mode: 'shift_all' as DateShiftMode, label: t('dashboard.dateShiftAll'), desc: t('dashboard.dateShiftAllDesc') },
|
||||
]).map(({ mode, label, desc }) => (
|
||||
<label key={mode}
|
||||
className={`flex items-start gap-3 p-3 border rounded-lg cursor-pointer transition-colors ${dateShiftMode === mode ? 'border-slate-900 bg-slate-50' : 'border-slate-200 hover:bg-slate-50'}`}>
|
||||
<input type="radio" name="date_shift_mode" value={mode} checked={dateShiftMode === mode}
|
||||
onChange={() => setDateShiftMode(mode)} className="mt-1 accent-slate-900" />
|
||||
<span>
|
||||
<span className="block text-sm font-medium text-slate-800">{label}</span>
|
||||
<span className="block text-sm text-slate-500 mt-0.5">{desc}</span>
|
||||
</span>
|
||||
</label>
|
||||
))}
|
||||
<p className="text-xs text-slate-400">{t('dashboard.dateShiftHint')}</p>
|
||||
</div>
|
||||
)}
|
||||
<form onSubmit={handleSubmit} className={pendingDateShift ? 'hidden' : 'space-y-4'} onPaste={handlePaste}>
|
||||
{error && (
|
||||
<div className="p-3 bg-red-50 border border-red-200 rounded-lg text-sm text-red-600">{error}</div>
|
||||
)}
|
||||
|
||||
Reference in New Issue
Block a user