mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-06-19 13:21:46 +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
714 B
TypeScript
21 lines
714 B
TypeScript
import { accommodationsApi } from '../api/client'
|
|
import { offlineDb, upsertAccommodations } from '../db/offlineDb'
|
|
import { onlineThenCache } from './withOfflineFallback'
|
|
import type { Accommodation } from '../types'
|
|
|
|
export const accommodationRepo = {
|
|
async list(tripId: number | string): Promise<{ accommodations: Accommodation[] }> {
|
|
return onlineThenCache(
|
|
async () => {
|
|
const result = await accommodationsApi.list(tripId)
|
|
upsertAccommodations(result.accommodations || []).catch(() => {})
|
|
return result
|
|
},
|
|
async () => ({
|
|
accommodations: await offlineDb.accommodations
|
|
.where('trip_id').equals(Number(tripId)).toArray(),
|
|
}),
|
|
)
|
|
},
|
|
}
|