mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-27 23:27:31 +00:00
fix(journey): keep the journal shown when a reload after a change fails
The journal views read the journal again after their own changes (saved settings, a finished entry, an added trip, picked provider photos) by calling loadJourney and dropping the promise. loadJourney rethrows every failure, so a reload that failed, offline or timed out, became an unhandled rejection with nothing shown to the user. In CI the same happened whenever a test ended with such a read still open: the teardown runs the pending timers, the request timeout fired, and the run failed on the unhandled error. The store gets refreshJourney, which reads the journal and answers whether the read landed instead of rejecting; a 404 still turns into notFound. The shared journal hook hands it to the desktop and the phone view and to the map layers in place of the raw loader, and the Studio uses it for its own reads.
This commit is contained in:
@@ -152,6 +152,19 @@ describe('journeyStore', () => {
|
||||
expect(useJourneyStore.getState().notFound).toBe(true);
|
||||
});
|
||||
|
||||
it('FE-STORE-JOURNEY-022: refreshJourney answers whether the read landed and never rejects', async () => {
|
||||
const detail = buildJourneyDetail({ id: 5 });
|
||||
server.use(http.get('/api/journeys/5', () => HttpResponse.json(detail)));
|
||||
await expect(useJourneyStore.getState().refreshJourney(5)).resolves.toBe(true);
|
||||
expect(useJourneyStore.getState().current?.id).toBe(5);
|
||||
|
||||
// A failed read keeps the journal as shown instead of throwing into a view callback.
|
||||
server.use(http.get('/api/journeys/5', () => HttpResponse.json({ error: 'down' }, { status: 500 })));
|
||||
await expect(useJourneyStore.getState().refreshJourney(5)).resolves.toBe(false);
|
||||
expect(useJourneyStore.getState().current?.id).toBe(5);
|
||||
expect(useJourneyStore.getState().notFound).toBe(false);
|
||||
});
|
||||
|
||||
// ── createJourney ────────────────────────────────────────────────────────
|
||||
|
||||
it('FE-STORE-JOURNEY-005: createJourney adds to store and returns journey', async () => {
|
||||
|
||||
@@ -191,6 +191,11 @@ interface JourneyState {
|
||||
|
||||
loadJourneys: () => Promise<void>
|
||||
loadJourney: (id: number) => Promise<void>
|
||||
/**
|
||||
* Reads the journal again after a change the server already took, or when an
|
||||
* event says it changed. Never rejects: it answers whether the read landed.
|
||||
*/
|
||||
refreshJourney: (id: number) => Promise<boolean>
|
||||
createJourney: (data: { title: string; subtitle?: string; trip_ids?: number[] }) => Promise<Journey>
|
||||
updateJourney: (id: number, data: Record<string, unknown>) => Promise<void>
|
||||
deleteJourney: (id: number) => Promise<void>
|
||||
@@ -276,6 +281,14 @@ export const useJourneyStore = create<JourneyState>((set, get) => ({
|
||||
}
|
||||
},
|
||||
|
||||
refreshJourney: (id) =>
|
||||
// A failed read keeps the journal as shown: a 404 has become `notFound` in
|
||||
// loadJourney, anything else is read again with the next change or event.
|
||||
get().loadJourney(id).then(
|
||||
() => true,
|
||||
() => false,
|
||||
),
|
||||
|
||||
createJourney: async (data) => {
|
||||
const journey = await journeyApi.create(data)
|
||||
set(s => ({ journeys: [journey, ...s.journeys] }))
|
||||
|
||||
Reference in New Issue
Block a user