mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-04 01:44:13 +00:00
feat(dayplan): a day can be deleted from the reorder dialog, after a warning that says what goes with it
Every row of the reorder dialog, and of the days sheet on a phone, now has a delete button. It first asks, and the question lists what goes with the day: planned places stay in the place list, notes and day texts are deleted, bookings stay without a day, a stay that checks in or out on the day is cancelled with its booking and expense (shown in the danger tone), and on a dated trip the later days and their bookings move one date earlier or the trip ends a day earlier. The server side moved from a bare DELETE into DayRemovalService, which REST, the delete_day tool and the plugin RPC all use. It closes the numbering, keeps the dates on their positions like a reorder, re-stamps the moved bookings, shifts the road trip boundaries, cancels touching stays through the accommodations domain, refuses the last day of a trip, and fans out day:deleted, day:reordered and trip:updated so collaborators renumber and see the new day count and end date. The trip query moved into a leaf file so the days domain can read it without a module cycle. On the client the delete is online only, runs optimistically through the days slice with the same reducer collaborators apply, and the ConfirmDialog gained a slot for extra content, an Escape that only closes itself, and the danger and accent tokens.
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
// FE-TSLICE-DAYS-001 to FE-TSLICE-DAYS-008 (whole-day reorder + insert, #589)
|
||||
// FE-TSLICE-DAYS-001 to FE-TSLICE-DAYS-011 (whole-day reorder + insert, #589; delete)
|
||||
import { http, HttpResponse } from 'msw';
|
||||
import { server } from '../../../tests/helpers/msw/server';
|
||||
import { resetAllStores, seedStore } from '../../../tests/helpers/store';
|
||||
import { buildDay, buildReservation } from '../../../tests/helpers/factories';
|
||||
import { buildAssignment, buildDay, buildDayNote, buildReservation, buildTrip } from '../../../tests/helpers/factories';
|
||||
import { useTripStore } from '../tripStore';
|
||||
import type { Day } from '../../types';
|
||||
|
||||
@@ -178,4 +178,74 @@ describe('daysSlice', () => {
|
||||
expect(useTripStore.getState().days.map(d => d.id)).toEqual([1, 2, 3]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('deleteDay', () => {
|
||||
const seedDayContent = () => seedStore(useTripStore, {
|
||||
trip: buildTrip({ id: 1, start_date: '2025-06-01', end_date: '2025-06-03' }),
|
||||
days: datedDays(),
|
||||
assignments: { '1': [], '2': [buildAssignment({ day_id: 2 })], '3': [] },
|
||||
dayNotes: { '1': [], '2': [buildDayNote({ day_id: 2 })], '3': [] },
|
||||
selectedDayId: 2,
|
||||
});
|
||||
|
||||
it('FE-TSLICE-DAYS-009: closes the gap at once, keeps the dates on their slots and takes the trip it is answered with', async () => {
|
||||
seedDayContent();
|
||||
const serverDays = [
|
||||
buildDay({ id: 1, trip_id: 1, day_number: 1, date: '2025-06-01', title: 'Arrival' }),
|
||||
buildDay({ id: 3, trip_id: 1, day_number: 2, date: '2025-06-02', title: 'Departure' }),
|
||||
];
|
||||
let optimistic: ReturnType<typeof useTripStore.getState> | null = null;
|
||||
server.use(
|
||||
http.delete('/api/trips/1/days/2', () => {
|
||||
optimistic = useTripStore.getState();
|
||||
return HttpResponse.json({ success: true, trip: buildTrip({ id: 1, start_date: '2025-06-01', end_date: '2025-06-02' }) });
|
||||
}),
|
||||
http.get('/api/trips/1/days', () => HttpResponse.json({ days: serverDays })),
|
||||
http.get('/api/trips/1/reservations', () => HttpResponse.json({ reservations: [buildReservation({ id: 5, trip_id: 1, title: 'Let go' })] })),
|
||||
);
|
||||
|
||||
await useTripStore.getState().deleteDay(1, 2);
|
||||
|
||||
const during = optimistic as unknown as ReturnType<typeof useTripStore.getState>;
|
||||
expect(during.days.map(d => [d.id, d.day_number, d.date])).toEqual([[1, 1, '2025-06-01'], [3, 2, '2025-06-02']]);
|
||||
expect('2' in during.assignments).toBe(false);
|
||||
expect('2' in during.dayNotes).toBe(false);
|
||||
const after = useTripStore.getState();
|
||||
expect(after.trip?.end_date).toBe('2025-06-02');
|
||||
expect(after.days.map(d => d.id)).toEqual([1, 3]);
|
||||
expect(after.reservations.map(r => r.title)).toEqual(['Let go']);
|
||||
});
|
||||
|
||||
it('FE-TSLICE-DAYS-010: a refusal puts every field back and throws the server sentence', async () => {
|
||||
seedDayContent();
|
||||
server.use(http.delete('/api/trips/1/days/2', () => HttpResponse.json({ error: 'A trip needs at least one day.' }, { status: 400 })));
|
||||
|
||||
await expect(useTripStore.getState().deleteDay(1, 2)).rejects.toThrow('A trip needs at least one day.');
|
||||
|
||||
const state = useTripStore.getState();
|
||||
expect(state.days.map(d => d.id)).toEqual([1, 2, 3]);
|
||||
expect(state.assignments['2']).toHaveLength(1);
|
||||
expect(state.dayNotes['2']).toHaveLength(1);
|
||||
expect(state.selectedDayId).toBe(2);
|
||||
expect(state.trip?.end_date).toBe('2025-06-03');
|
||||
});
|
||||
|
||||
it('FE-TSLICE-DAYS-011: a selection on the deleted day clears, one on another day stays', async () => {
|
||||
seedDayContent();
|
||||
server.use(
|
||||
http.delete('/api/trips/1/days/:id', () => HttpResponse.json({ success: true })),
|
||||
http.get('/api/trips/1/days', () => HttpResponse.json({ days: datedDays() })),
|
||||
http.get('/api/trips/1/reservations', () => HttpResponse.json({ reservations: [] })),
|
||||
);
|
||||
|
||||
await useTripStore.getState().deleteDay(1, 2);
|
||||
expect(useTripStore.getState().selectedDayId).toBeNull();
|
||||
|
||||
seedDayContent();
|
||||
await useTripStore.getState().deleteDay(1, 3);
|
||||
expect(useTripStore.getState().selectedDayId).toBe(2);
|
||||
// Answered without a trip, the store keeps the one it has.
|
||||
expect(useTripStore.getState().trip?.end_date).toBe('2025-06-03');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { daysApi } from '../../api/client'
|
||||
import { dayRepo } from '../../repo/dayRepo'
|
||||
import type { StoreApi } from 'zustand'
|
||||
import type { TripStoreState } from '../tripStore'
|
||||
import type { Day } from '../../types'
|
||||
@@ -10,6 +11,31 @@ type GetState = StoreApi<TripStoreState>['getState']
|
||||
export interface DaysSlice {
|
||||
reorderDays: (tripId: number | string, orderedIds: number[]) => Promise<void>
|
||||
insertDay: (tripId: number | string, position?: number) => Promise<Day | undefined>
|
||||
deleteDay: (tripId: number | string, dayId: number) => Promise<void>
|
||||
}
|
||||
|
||||
type DayState = Pick<TripStoreState, 'days' | 'assignments' | 'dayNotes' | 'selectedDayId'>
|
||||
|
||||
/**
|
||||
* The day state without one day, the way the server leaves it: the days after it
|
||||
* move up one place, and on a dated trip the dates stay on their positions, so
|
||||
* every later day takes the date one slot earlier. The day's assignments and
|
||||
* notes go with it, and a selection on it is cleared.
|
||||
*
|
||||
* Both the optimistic delete below and a collaborator's day:deleted apply this,
|
||||
* so the two ends of the same delete cannot drift apart field by field.
|
||||
*/
|
||||
export function withoutDay(state: DayState, dayId: number): DayState {
|
||||
const ordered = [...state.days].sort((a, b) => (a.day_number ?? 0) - (b.day_number ?? 0))
|
||||
const dates = ordered.map(d => d.date).filter((d): d is string => !!d).sort()
|
||||
const days = ordered
|
||||
.filter(d => d.id !== dayId)
|
||||
.map((d, i) => ({ ...d, day_number: i + 1, date: dates.length ? (dates[i] ?? null) : d.date }))
|
||||
const assignments = { ...state.assignments }
|
||||
delete assignments[String(dayId)]
|
||||
const dayNotes = { ...state.dayNotes }
|
||||
delete dayNotes[String(dayId)]
|
||||
return { days, assignments, dayNotes, selectedDayId: state.selectedDayId === dayId ? null : state.selectedDayId }
|
||||
}
|
||||
|
||||
export const createDaysSlice = (set: SetState, get: GetState): DaysSlice => ({
|
||||
@@ -53,4 +79,26 @@ export const createDaysSlice = (set: SetState, get: GetState): DaysSlice => ({
|
||||
throw new Error(getApiErrorMessage(err, 'Error adding day'))
|
||||
}
|
||||
},
|
||||
|
||||
// Delete a day. The list closes up at once; a refusal (the last day, no
|
||||
// connection, a server error) puts every field back. Afterwards the store pulls
|
||||
// the re-dated days and the bookings, which were re-stamped or let go of, and
|
||||
// takes the trip the server answered with for its day count and end date.
|
||||
deleteDay: async (tripId, dayId) => {
|
||||
const { days, assignments, dayNotes, selectedDayId } = get()
|
||||
const previous: DayState = { days, assignments, dayNotes, selectedDayId }
|
||||
set(withoutDay(previous, dayId))
|
||||
|
||||
const outcome = await dayRepo.remove(tripId, dayId).then(
|
||||
answer => ({ trip: answer.trip }),
|
||||
(failure: unknown) => ({ failure }),
|
||||
)
|
||||
if ('failure' in outcome) {
|
||||
set(previous)
|
||||
throw new Error(getApiErrorMessage(outcome.failure, 'Error deleting day'))
|
||||
}
|
||||
if (outcome.trip) set({ trip: outcome.trip })
|
||||
await get().refreshDays(tripId)
|
||||
await get().loadReservations(tripId)
|
||||
},
|
||||
})
|
||||
|
||||
@@ -5,6 +5,7 @@ import type { Assignment, Place, Day, DayNote, PackingItem, TodoItem, BudgetItem
|
||||
import { offlineDb } from '../../db/offlineDb'
|
||||
import { useAuthStore } from '../authStore'
|
||||
import { mergeAssignmentPlace } from './placesSlice'
|
||||
import { withoutDay } from './daysSlice'
|
||||
|
||||
type SetState = StoreApi<TripStoreState>['setState']
|
||||
type GetState = StoreApi<TripStoreState>['getState']
|
||||
@@ -330,18 +331,9 @@ export const STATE_APPLIERS: Partial<Record<TrekWsTripEventName, StateApplier>>
|
||||
'day:updated': (payload, state) => ({
|
||||
days: state.days.map(d => d.id === (payload.day as Day).id ? payload.day as Day : d),
|
||||
}),
|
||||
'day:deleted': (payload, state) => {
|
||||
const removedDayId = String(payload.dayId)
|
||||
const newAssignments = { ...state.assignments }
|
||||
delete newAssignments[removedDayId]
|
||||
const newDayNotes = { ...state.dayNotes }
|
||||
delete newDayNotes[removedDayId]
|
||||
return {
|
||||
days: state.days.filter(d => d.id !== payload.dayId),
|
||||
assignments: newAssignments,
|
||||
dayNotes: newDayNotes,
|
||||
}
|
||||
},
|
||||
// The same reducer the deleting tab ran optimistically: the later days move up
|
||||
// one place, the dates stay on their positions, a selection on the day clears.
|
||||
'day:deleted': (payload, state) => withoutDay(state, Number(payload.dayId)),
|
||||
'day:reordered': (payload, state) => {
|
||||
// Apply the new order instantly when we know all ids; the authoritative
|
||||
// dates + re-stamped booking times are pulled by the refresh below.
|
||||
@@ -564,6 +556,10 @@ export function handleRemoteEvent(set: SetState, get: GetState, event: WebSocket
|
||||
if (tripId) get().loadReservations(tripId)
|
||||
}
|
||||
|
||||
// A deleted day cancels a stay that checked in or out on it, and the stays
|
||||
// live in page-local planner state rather than this store.
|
||||
if (type === 'day:deleted') window.dispatchEvent(new CustomEvent('accommodations:refresh'))
|
||||
|
||||
// A reorder/insert re-pins dates and re-stamps booking times server-side, so
|
||||
// pull the authoritative days + reservations for collaborators.
|
||||
if (type === 'day:reordered') {
|
||||
|
||||
Reference in New Issue
Block a user