mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-06-21 14:21:46 +00:00
fix: remove navigator.onLine guards and fix upsert races in all repos
navigator.onLine returns false transiently during service worker activation
(skipWaiting + clientsClaim), causing all repo refresh IIFEs to return null
immediately on first page load — leaving the UI with empty data until F5.
Fixes applied across all list repos (trip, day, place, packing, todo, budget,
reservation, accommodation, file):
- Drop navigator.onLine guard; let fetch fail naturally when truly offline
- Await all upsert calls (some were fire-and-forget, risking race conditions
against subsequent reads and silent swallowed failures)
- Return Promise.resolve(null) instead of Promise.resolve(fresh) in the
IDB-empty network path, so loadTrip's background refresh Promise.all
resolves null and skips set({trip}), preventing a spurious reference change
that was resetting the 1500ms splash timer
Tests updated: placeRepo and packingRepo "empty cache" tests now simulate
genuine network failure (HttpResponse.error) instead of relying on the
navigator.onLine guard that no longer exists; DashboardPage tests clear IDB
before each test and use a query-safe assertion after background refresh.
This commit is contained in:
@@ -7,10 +7,12 @@ import { resetAllStores, seedStore } from '../../tests/helpers/store';
|
||||
import { buildUser, buildAdmin, buildTrip } from '../../tests/helpers/factories';
|
||||
import { useAuthStore } from '../store/authStore';
|
||||
import { usePermissionsStore } from '../store/permissionsStore';
|
||||
import { offlineDb } from '../db/offlineDb';
|
||||
import DashboardPage from './DashboardPage';
|
||||
|
||||
beforeEach(() => {
|
||||
beforeEach(async () => {
|
||||
vi.clearAllMocks();
|
||||
await Promise.all(offlineDb.tables.map(t => t.clear()));
|
||||
resetAllStores();
|
||||
// Seed auth with authenticated user
|
||||
seedStore(useAuthStore, { isAuthenticated: true, user: buildUser() });
|
||||
@@ -329,7 +331,8 @@ describe('DashboardPage', () => {
|
||||
const tokyoTrip = screen.getAllByText('Tokyo Trip')[0];
|
||||
await user.click(tokyoTrip);
|
||||
|
||||
expect(tokyoTrip).toBeInTheDocument();
|
||||
// Re-query after click — background refresh may re-render the list
|
||||
expect(screen.getAllByText('Tokyo Trip').length).toBeGreaterThan(0);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -9,10 +9,9 @@ export const accommodationRepo = {
|
||||
.where('trip_id').equals(Number(tripId)).toArray()
|
||||
|
||||
const refresh = (async () => {
|
||||
if (!navigator.onLine) return null
|
||||
try {
|
||||
const result = await accommodationsApi.list(tripId)
|
||||
upsertAccommodations(result.accommodations || []).catch(() => {})
|
||||
await upsertAccommodations(result.accommodations || [])
|
||||
return result
|
||||
} catch {
|
||||
return null
|
||||
@@ -23,7 +22,7 @@ export const accommodationRepo = {
|
||||
|
||||
const fresh = await refresh
|
||||
if (!fresh) return { accommodations: [], refresh: Promise.resolve(null) }
|
||||
return { accommodations: fresh.accommodations, refresh: Promise.resolve(fresh) }
|
||||
return { accommodations: fresh.accommodations, refresh: Promise.resolve(null) }
|
||||
},
|
||||
|
||||
async create(tripId: number | string, data: Record<string, unknown>): Promise<{ accommodation: Accommodation }> {
|
||||
|
||||
@@ -11,10 +11,9 @@ export const budgetRepo = {
|
||||
.toArray()
|
||||
|
||||
const refresh = (async () => {
|
||||
if (!navigator.onLine) return null
|
||||
try {
|
||||
const result = await budgetApi.list(tripId)
|
||||
upsertBudgetItems(result.items)
|
||||
await upsertBudgetItems(result.items)
|
||||
return result
|
||||
} catch {
|
||||
return null
|
||||
@@ -25,7 +24,7 @@ export const budgetRepo = {
|
||||
|
||||
const fresh = await refresh
|
||||
if (!fresh) return { items: [], refresh: Promise.resolve(null) }
|
||||
return { items: fresh.items, refresh: Promise.resolve(fresh) }
|
||||
return { items: fresh.items, refresh: Promise.resolve(null) }
|
||||
},
|
||||
|
||||
async create(tripId: number | string, data: Record<string, unknown>): Promise<{ item: BudgetItem }> {
|
||||
|
||||
@@ -11,10 +11,9 @@ export const dayRepo = {
|
||||
.sortBy('day_number' as keyof Day)) as Day[]
|
||||
|
||||
const refresh = (async () => {
|
||||
if (!navigator.onLine) return null
|
||||
try {
|
||||
const result = await daysApi.list(tripId)
|
||||
upsertDays(result.days)
|
||||
await upsertDays(result.days)
|
||||
return result
|
||||
} catch {
|
||||
return null
|
||||
@@ -25,7 +24,7 @@ export const dayRepo = {
|
||||
|
||||
const fresh = await refresh
|
||||
if (!fresh) return { days: [], refresh: Promise.resolve(null) }
|
||||
return { days: fresh.days, refresh: Promise.resolve(fresh) }
|
||||
return { days: fresh.days, refresh: Promise.resolve(null) }
|
||||
},
|
||||
|
||||
async update(tripId: number | string, dayId: number | string, data: Record<string, unknown>): Promise<{ day: Day }> {
|
||||
|
||||
@@ -11,10 +11,9 @@ export const fileRepo = {
|
||||
.toArray()
|
||||
|
||||
const refresh = (async () => {
|
||||
if (!navigator.onLine) return null
|
||||
try {
|
||||
const result = await filesApi.list(tripId)
|
||||
upsertTripFiles(result.files)
|
||||
await upsertTripFiles(result.files)
|
||||
return result
|
||||
} catch {
|
||||
return null
|
||||
@@ -25,7 +24,7 @@ export const fileRepo = {
|
||||
|
||||
const fresh = await refresh
|
||||
if (!fresh) return { files: [], refresh: Promise.resolve(null) }
|
||||
return { files: fresh.files, refresh: Promise.resolve(fresh) }
|
||||
return { files: fresh.files, refresh: Promise.resolve(null) }
|
||||
},
|
||||
|
||||
async update(tripId: number | string, id: number, data: Record<string, unknown>): Promise<{ file: TripFile }> {
|
||||
|
||||
@@ -11,10 +11,9 @@ export const packingRepo = {
|
||||
.toArray()
|
||||
|
||||
const refresh = (async () => {
|
||||
if (!navigator.onLine) return null
|
||||
try {
|
||||
const result = await packingApi.list(tripId)
|
||||
upsertPackingItems(result.items)
|
||||
await upsertPackingItems(result.items)
|
||||
return result
|
||||
} catch {
|
||||
return null
|
||||
@@ -25,7 +24,7 @@ export const packingRepo = {
|
||||
|
||||
const fresh = await refresh
|
||||
if (!fresh) return { items: [], refresh: Promise.resolve(null) }
|
||||
return { items: fresh.items, refresh: Promise.resolve(fresh) }
|
||||
return { items: fresh.items, refresh: Promise.resolve(null) }
|
||||
},
|
||||
|
||||
async create(tripId: number | string, data: Record<string, unknown>): Promise<{ item: PackingItem }> {
|
||||
|
||||
@@ -11,10 +11,9 @@ export const placeRepo = {
|
||||
.toArray()
|
||||
|
||||
const refresh = (async () => {
|
||||
if (!navigator.onLine) return null
|
||||
try {
|
||||
const result = await placesApi.list(tripId, params)
|
||||
upsertPlaces(result.places)
|
||||
await upsertPlaces(result.places)
|
||||
return result
|
||||
} catch {
|
||||
return null
|
||||
@@ -25,7 +24,7 @@ export const placeRepo = {
|
||||
|
||||
const fresh = await refresh
|
||||
if (!fresh) return { places: [], refresh: Promise.resolve(null) }
|
||||
return { places: fresh.places, refresh: Promise.resolve(fresh) }
|
||||
return { places: fresh.places, refresh: Promise.resolve(null) }
|
||||
},
|
||||
|
||||
async create(tripId: number | string, data: Record<string, unknown>): Promise<{ place: Place }> {
|
||||
|
||||
@@ -11,10 +11,9 @@ export const reservationRepo = {
|
||||
.toArray()
|
||||
|
||||
const refresh = (async () => {
|
||||
if (!navigator.onLine) return null
|
||||
try {
|
||||
const result = await reservationsApi.list(tripId)
|
||||
upsertReservations(result.reservations)
|
||||
await upsertReservations(result.reservations)
|
||||
return result
|
||||
} catch {
|
||||
return null
|
||||
@@ -25,7 +24,7 @@ export const reservationRepo = {
|
||||
|
||||
const fresh = await refresh
|
||||
if (!fresh) return { reservations: [], refresh: Promise.resolve(null) }
|
||||
return { reservations: fresh.reservations, refresh: Promise.resolve(fresh) }
|
||||
return { reservations: fresh.reservations, refresh: Promise.resolve(null) }
|
||||
},
|
||||
|
||||
async create(tripId: number | string, data: Record<string, unknown>): Promise<{ reservation: Reservation }> {
|
||||
|
||||
@@ -11,10 +11,9 @@ export const todoRepo = {
|
||||
.toArray()
|
||||
|
||||
const refresh = (async () => {
|
||||
if (!navigator.onLine) return null
|
||||
try {
|
||||
const result = await todoApi.list(tripId)
|
||||
upsertTodoItems(result.items)
|
||||
await upsertTodoItems(result.items)
|
||||
return result
|
||||
} catch {
|
||||
return null
|
||||
@@ -25,7 +24,7 @@ export const todoRepo = {
|
||||
|
||||
const fresh = await refresh
|
||||
if (!fresh) return { items: [], refresh: Promise.resolve(null) }
|
||||
return { items: fresh.items, refresh: Promise.resolve(fresh) }
|
||||
return { items: fresh.items, refresh: Promise.resolve(null) }
|
||||
},
|
||||
|
||||
async create(tripId: number | string, data: Record<string, unknown>): Promise<{ item: TodoItem }> {
|
||||
|
||||
@@ -11,14 +11,15 @@ export const tripRepo = {
|
||||
const all = await offlineDb.trips.toArray()
|
||||
|
||||
const refresh: TripsRefresh = (async () => {
|
||||
if (!navigator.onLine) return null
|
||||
try {
|
||||
const [active, archived] = await Promise.all([
|
||||
tripsApi.list(),
|
||||
tripsApi.list({ archived: 1 }),
|
||||
])
|
||||
active.trips.forEach(t => upsertTrip(t))
|
||||
archived.trips.forEach(t => upsertTrip(t))
|
||||
await Promise.all([
|
||||
...active.trips.map(t => upsertTrip(t)),
|
||||
...archived.trips.map(t => upsertTrip(t)),
|
||||
])
|
||||
return { trips: active.trips, archivedTrips: archived.trips }
|
||||
} catch {
|
||||
return null
|
||||
@@ -35,17 +36,17 @@ export const tripRepo = {
|
||||
|
||||
const fresh = await refresh
|
||||
if (!fresh) return { trips: [], archivedTrips: [], refresh: Promise.resolve(null) }
|
||||
return { ...fresh, refresh: Promise.resolve(fresh) }
|
||||
// Data came straight from network — no background re-fetch needed
|
||||
return { ...fresh, refresh: Promise.resolve(null) }
|
||||
},
|
||||
|
||||
async get(tripId: number | string): Promise<{ trip: Trip; refresh: TripRefresh }> {
|
||||
const cached = await offlineDb.trips.get(Number(tripId))
|
||||
|
||||
const refresh: TripRefresh = (async () => {
|
||||
if (!navigator.onLine) return null
|
||||
try {
|
||||
const result = await tripsApi.get(tripId)
|
||||
upsertTrip(result.trip)
|
||||
await upsertTrip(result.trip)
|
||||
return result
|
||||
} catch {
|
||||
return null
|
||||
@@ -56,7 +57,7 @@ export const tripRepo = {
|
||||
|
||||
const fresh = await refresh
|
||||
if (!fresh) throw new Error('No cached trip data available offline')
|
||||
return { trip: fresh.trip, refresh: Promise.resolve(fresh) }
|
||||
return { trip: fresh.trip, refresh: Promise.resolve(null) }
|
||||
},
|
||||
|
||||
async update(tripId: number | string, data: Partial<Trip>): Promise<{ trip: Trip }> {
|
||||
|
||||
Reference in New Issue
Block a user