mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-22 13:10:29 +00:00
56f26b5efd
The server sends accommodation:created and accommodation:updated to the whole trip, the assistant's writes included, but the client listed both as ignored and reloaded stays only after a delete. A stay the assistant, a fellow traveller, a plugin or a booking import added therefore showed in the sidebar, on the map and in the day panel only after a reload. Both events now refresh the stays like the deletes do, in the planner (desktop and phone, which share its hook) and in the day panel. The refresh is bundled per burst, since one assistant step can apply many changes, a newer answer always wins over an older one, and a stay being edited in the day panel that was deleted or moved elsewhere closes its editor instead of letting the form overwrite another stay. A browser test books a stay from a second context and sees it appear in the open planner, on the desktop and on the phone.
47 lines
1.7 KiB
TypeScript
47 lines
1.7 KiB
TypeScript
import { useCallback, useRef, useState } from 'react';
|
|
|
|
import { accommodationRepo } from '../../repo/accommodationRepo';
|
|
import { useTripStore } from '../../store/tripStore';
|
|
import type { Accommodation } from '../../types';
|
|
import { useStaysRefresh } from './useStaysRefresh';
|
|
|
|
/**
|
|
* The planner's stays: the trip's whole list, which the sidebar, the map and the
|
|
* phone's day sheet read. They live here rather than in the trip store, so a stay
|
|
* written elsewhere (the assistant, a fellow traveller, a plugin, a booking
|
|
* import) reaches them as a nudge, and `useStaysRefresh` folds a burst of nudges
|
|
* into one read.
|
|
*
|
|
* A read takes the stays' bookings along, since a stay is booked as a hotel
|
|
* reservation. The newest read wins: the read after an own write and the one for
|
|
* a stay written elsewhere can be on their way at once, and the older answer must
|
|
* not land over the newer one.
|
|
*/
|
|
export function useTripStays(tripId: number) {
|
|
const [tripAccommodations, setTripAccommodations] = useState<Accommodation[]>([]);
|
|
const latestRead = useRef(0);
|
|
|
|
const readStays = useCallback(async () => {
|
|
if (!tripId) return;
|
|
const read = ++latestRead.current;
|
|
await Promise.all([
|
|
accommodationRepo.list(tripId).then(
|
|
(d) => {
|
|
if (read === latestRead.current) setTripAccommodations(d.accommodations || []);
|
|
},
|
|
// A read that fails keeps the stays on show; the next nudge reads them again.
|
|
() => {}
|
|
),
|
|
useTripStore.getState().loadReservations(tripId),
|
|
]);
|
|
}, [tripId]);
|
|
|
|
const loadAccommodations = useCallback(() => {
|
|
void readStays();
|
|
}, [readStays]);
|
|
|
|
useStaysRefresh(readStays);
|
|
|
|
return { tripAccommodations, setTripAccommodations, loadAccommodations };
|
|
}
|