mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-27 23:27:31 +00:00
feat(mobile): fold transports and bookings that are over into a Past section
Mid-trip the transports and bookings tabs kept every flight already landed and every check-in already done above what is still ahead. What is over (its end, or its start where it has none, before the moment the tab opened, in local wall-clock time) now stands in a Past section at the end, latest first. It starts folded while something is still ahead and open once the whole trip is over, to look back on. The desktop list is unchanged. Addresses discussion #3156.
This commit is contained in:
@@ -15,6 +15,7 @@ import {
|
||||
displayTitle,
|
||||
groupReservations,
|
||||
groupTransports,
|
||||
hasPassed,
|
||||
onTravelers,
|
||||
parseMeta,
|
||||
phaseOf,
|
||||
@@ -394,4 +395,31 @@ describe('onTravelers and the shared type colours', () => {
|
||||
expect(groups.transit.map(r => r.id)).toEqual([1]);
|
||||
expect(sortReservations([later, transit, open, sooner], days, 'date', 'asc', label).map(r => r.id)).toEqual([1, 3, 2, 4]);
|
||||
});
|
||||
|
||||
it('FE-PLANNER-BKMODEL-044: hasPassed reads the end, else the start, in local wall-clock time', () => {
|
||||
const now = new Date(2025, 5, 2, 12, 0);
|
||||
expect(hasPassed(buildReservation({ reservation_time: '2025-06-02T11:00' }), now)).toBe(true);
|
||||
expect(hasPassed(buildReservation({ reservation_time: '2025-06-02T13:00' }), now)).toBe(false);
|
||||
// The end counts: a flight that left in the morning and lands this afternoon is not over.
|
||||
expect(hasPassed(buildReservation({ reservation_time: '2025-06-02T08:00', reservation_end_time: '2025-06-02T15:00' }), now)).toBe(false);
|
||||
// An end time without a date is on the start's day.
|
||||
expect(hasPassed(buildReservation({ reservation_time: '2025-06-02T08:00', reservation_end_time: '11:30' }), now)).toBe(true);
|
||||
// A date without a time lasts until the day is over.
|
||||
expect(hasPassed(buildReservation({ reservation_time: '2025-06-02' }), now)).toBe(false);
|
||||
expect(hasPassed(buildReservation({ reservation_time: '2025-06-01' }), now)).toBe(true);
|
||||
// Without any date nothing says it is over.
|
||||
expect(hasPassed(buildReservation({ reservation_time: null, day_id: null }), now)).toBe(false);
|
||||
});
|
||||
|
||||
it('FE-PLANNER-BKMODEL-045: groupTransports moves what is over into past, latest first, only when given the time', () => {
|
||||
const early = buildReservation({ id: 1, type: 'train', status: 'confirmed', reservation_time: '2025-06-01T09:00' });
|
||||
const lastNight = buildReservation({ id: 2, type: 'transit', status: 'pending', reservation_time: '2025-06-01T21:00' });
|
||||
const ahead = buildReservation({ id: 3, type: 'bus', status: 'confirmed', reservation_time: '2025-06-03T09:00' });
|
||||
const now = new Date(2025, 5, 2, 12, 0);
|
||||
const groups = groupTransports([ahead, early, lastNight], days, now);
|
||||
expect(groups.confirmed.map(r => r.id)).toEqual([3]);
|
||||
expect(groups.transit).toEqual([]);
|
||||
expect(groups.past.map(r => r.id)).toEqual([2, 1]);
|
||||
expect(groupTransports([ahead, early, lastNight], days).past).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -252,21 +252,45 @@ export interface StatusGroups {
|
||||
confirmed: Reservation[]
|
||||
pending: Reservation[]
|
||||
transit: Reservation[]
|
||||
/** What is over already (#3156), latest first; empty unless the caller passes the time. */
|
||||
past: Reservation[]
|
||||
}
|
||||
|
||||
/**
|
||||
* The phone tabs' three sections, each in date order: confirmed and pending, and
|
||||
* Whether a booking is over: its end, or its start where it has none, lies
|
||||
* before `now`. The times are local wall-clock values, as everywhere a booking
|
||||
* is read. An end time without a date is on the start's day, and a date
|
||||
* without a time counts until that day is over. A booking without any date
|
||||
* (one only placed on a day) is never treated as over.
|
||||
*/
|
||||
export function hasPassed(r: Reservation, now: Date): boolean {
|
||||
const start = splitReservationDateTime(r.reservation_time)
|
||||
const end = r.reservation_end_time ? splitReservationDateTime(r.reservation_end_time) : null
|
||||
const date = end?.date ?? start.date
|
||||
if (!date) return false
|
||||
const time = end && (end.date || end.time) ? end.time : start.time
|
||||
const [y, m, d] = date.split('-').map(Number)
|
||||
const [hh, mm] = time ? time.split(':').map(Number) : [23, 59]
|
||||
return new Date(y, m - 1, d, hh, mm) < now
|
||||
}
|
||||
|
||||
/**
|
||||
* The phone tabs' sections, each in date order: confirmed and pending, and
|
||||
* transit (automated public transport, #1065) on its own whatever its status.
|
||||
* A booking type never matches `transit`, so the split fits the bookings tab too.
|
||||
* Given the time, what is over leaves those and stands in `past` (#3156).
|
||||
*/
|
||||
export function groupTransports(list: Reservation[], days: Day[]): StatusGroups {
|
||||
export function groupTransports(list: Reservation[], days: Day[], now?: Date): StatusGroups {
|
||||
const sorted = sortReservations(list, days, 'date', 'asc', () => '')
|
||||
const transit = sorted.filter(r => r.type === 'transit')
|
||||
const nonTransit = sorted.filter(r => r.type !== 'transit')
|
||||
const isPast = (r: Reservation) => !!now && hasPassed(r, now)
|
||||
const current = sorted.filter(r => !isPast(r))
|
||||
const transit = current.filter(r => r.type === 'transit')
|
||||
const nonTransit = current.filter(r => r.type !== 'transit')
|
||||
return {
|
||||
confirmed: nonTransit.filter(r => r.status === 'confirmed'),
|
||||
pending: nonTransit.filter(r => r.status !== 'confirmed'),
|
||||
transit,
|
||||
past: sorted.filter(isPast).reverse(),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
// FE-PLANNER-RESLISTFILTER-001 to -004: the traveller filter and folded sections
|
||||
// FE-PLANNER-RESLISTFILTER-001 to -005: the traveller filter and folded sections
|
||||
// of the phone's bookings and transports tabs.
|
||||
import { act, renderHook } from '@testing-library/react';
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
|
||||
import { buildDay, buildReservation } from '../../../../tests/helpers/factories';
|
||||
import type { Reservation } from '../../../types';
|
||||
@@ -24,6 +24,14 @@ const bob = buildReservation({
|
||||
const ride = buildReservation({ id: 12, type: 'transit', status: 'confirmed', reservation_time: '2026-03-01T07:00' });
|
||||
const ALL = [ann, bob, ride];
|
||||
|
||||
// The day before the trip: every booking above is still ahead.
|
||||
beforeEach(() => {
|
||||
vi.useFakeTimers({ toFake: ['Date'], now: new Date(2026, 1, 28, 12, 0) });
|
||||
});
|
||||
afterEach(() => {
|
||||
vi.useRealTimers();
|
||||
});
|
||||
|
||||
describe('useReservationListFilter', () => {
|
||||
it('FE-PLANNER-RESLISTFILTER-001: without a filter every booking lands in its status section', () => {
|
||||
const { result } = renderHook(() => useReservationListFilter(ALL, days, 2));
|
||||
@@ -37,7 +45,7 @@ describe('useReservationListFilter', () => {
|
||||
const { result } = renderHook(() => useReservationListFilter(ALL, days, 2));
|
||||
act(() => result.current.toggleTravelerFilter(2));
|
||||
expect([...result.current.travelerFilter]).toEqual([2]);
|
||||
expect(result.current.groups).toEqual({ confirmed: [], pending: [bob], transit: [] });
|
||||
expect(result.current.groups).toEqual({ confirmed: [], pending: [bob], transit: [], past: [] });
|
||||
|
||||
act(() => result.current.toggleTravelerFilter(1));
|
||||
expect(result.current.groups.confirmed.map((r) => r.id)).toEqual([10]);
|
||||
@@ -58,9 +66,24 @@ describe('useReservationListFilter', () => {
|
||||
it('FE-PLANNER-RESLISTFILTER-004: a section folds and unfolds on its own', () => {
|
||||
const { result } = renderHook(() => useReservationListFilter(ALL, days, 2));
|
||||
act(() => result.current.toggleSection('pending'));
|
||||
expect(result.current.collapsed).toEqual({ pending: true });
|
||||
expect(result.current.collapsed).toEqual({ pending: true, past: true });
|
||||
act(() => result.current.toggleSection('confirmed'));
|
||||
act(() => result.current.toggleSection('pending'));
|
||||
expect(result.current.collapsed).toEqual({ pending: false, confirmed: true });
|
||||
expect(result.current.collapsed).toEqual({ pending: false, confirmed: true, past: true });
|
||||
});
|
||||
|
||||
it('FE-PLANNER-RESLISTFILTER-005: what is over goes to Past, folded while something is ahead and open once all is over', () => {
|
||||
vi.setSystemTime(new Date(2026, 2, 1, 12, 0));
|
||||
const midTrip = renderHook(() => useReservationListFilter(ALL, days, 2)).result;
|
||||
expect(midTrip.current.groups.past.map((r) => r.id)).toEqual([11, 12]);
|
||||
expect(midTrip.current.groups.confirmed.map((r) => r.id)).toEqual([10]);
|
||||
expect(midTrip.current.collapsed.past).toBe(true);
|
||||
act(() => midTrip.current.toggleSection('past'));
|
||||
expect(midTrip.current.collapsed.past).toBe(false);
|
||||
|
||||
vi.setSystemTime(new Date(2026, 2, 3, 12, 0));
|
||||
const afterTrip = renderHook(() => useReservationListFilter(ALL, days, 2)).result;
|
||||
expect(afterTrip.current.groups.past.map((r) => r.id)).toEqual([10, 11, 12]);
|
||||
expect(afterTrip.current.collapsed.past).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -15,9 +15,13 @@ import { groupTransports, onTravelers } from './bookingsModel';
|
||||
*/
|
||||
export function useReservationListFilter(all: Reservation[], days: Day[], memberCount: number) {
|
||||
const [travelerFilter, setTravelerFilter] = useState<Set<number>>(new Set());
|
||||
// The moment the tab opened: what was over by then moves into the folded Past
|
||||
// section (#3156). Taken once, so a list does not reshuffle while it is read.
|
||||
const [now] = useState(() => new Date());
|
||||
const groups = groupTransports(
|
||||
all.filter((r) => onTravelers(r, travelerFilter)),
|
||||
days
|
||||
days,
|
||||
now
|
||||
);
|
||||
const showTravelerFilter = memberCount > 1 && all.some((r) => (r.travelers || []).length > 0);
|
||||
const toggleTravelerFilter = (id: number) =>
|
||||
@@ -29,8 +33,12 @@ export function useReservationListFilter(all: Reservation[], days: Day[], member
|
||||
});
|
||||
const clearTravelerFilter = () => setTravelerFilter(new Set());
|
||||
|
||||
const [collapsed, setCollapsed] = useState<Record<string, boolean>>({});
|
||||
const toggleSection = (id: string) => setCollapsed((c) => ({ ...c, [id]: !c[id] }));
|
||||
const [folded, setFolded] = useState<Record<string, boolean>>({});
|
||||
// Past starts folded while something is still ahead, since mid-trip that is
|
||||
// what counts; once the whole trip is over it opens, to look back on.
|
||||
const ahead = groups.confirmed.length + groups.pending.length + groups.transit.length > 0;
|
||||
const collapsed: Record<string, boolean> = { ...folded, past: folded.past ?? ahead };
|
||||
const toggleSection = (id: string) => setFolded((c) => ({ ...c, [id]: !collapsed[id] }));
|
||||
|
||||
return {
|
||||
travelerFilter,
|
||||
|
||||
Reference in New Issue
Block a user