mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-06-21 06:11:45 +00:00
bcd2c8c959
Repos gated reads on raw navigator.onLine and the online branch had no try/catch, so a captive portal or connected-but-no-internet (navigator.onLine lying "true") threw a network error instead of serving the good cached copy — blanking the trip even though Dexie held it. - new onlineThenCache(onlineFn, cacheFn) helper: reads the cache when offline, and on a network-level failure (Axios error with no HTTP response). A genuine HTTP error (4xx/5xx — the server responded) is rethrown so callers still set error state / navigate, not masked by a stale cache. - gates only on navigator.onLine, NOT the connectivity probe: the probe is a coarse global flag and one failed health check would otherwise divert every read to the (possibly empty) cache even when the request would succeed. - every repo list/get read path routed through it (reads only — writes still go through the mutation queue so failures surface) - tests: captive-portal fallback, HTTP-error rethrow, non-Axios rethrow
21 lines
670 B
TypeScript
21 lines
670 B
TypeScript
import { reservationsApi } from '../api/client'
|
|
import { offlineDb, upsertReservations } from '../db/offlineDb'
|
|
import { onlineThenCache } from './withOfflineFallback'
|
|
import type { Reservation } from '../types'
|
|
|
|
export const reservationRepo = {
|
|
async list(tripId: number | string): Promise<{ reservations: Reservation[] }> {
|
|
return onlineThenCache(
|
|
async () => {
|
|
const result = await reservationsApi.list(tripId)
|
|
upsertReservations(result.reservations)
|
|
return result
|
|
},
|
|
async () => ({
|
|
reservations: await offlineDb.reservations
|
|
.where('trip_id').equals(Number(tripId)).toArray(),
|
|
}),
|
|
)
|
|
},
|
|
}
|