mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-06-21 14:21:46 +00:00
69620e7276
All create/update/delete repo methods now write to IndexedDB optimistically and fire mutationQueue.flush() as fire-and-forget, returning immediately without waiting for the network. This eliminates the 8-second UX freeze previously seen when the API was unreachable but navigator.onLine was true. - Repos rewritten: trip, day, place, packing, todo, budget, accommodation, reservation, file — write methods never throw, always return optimistic data - mutationQueue.flush() changed to iterative (one item per loop iteration) so mutations enqueued mid-flush (e.g. bulk check-all) are picked up - fileRepo.toggleStar skips the IDB put when the file is not cached locally - DayDetailPanel passes place_name into accommodationRepo.create so the optimistic accommodation renders the correct hotel label immediately - Test suite updated throughout to reflect optimistic-first semantics: no more rollback assertions, IDB cleared in component test beforeEach hooks, FileManager tests switched from filesApi spy to MSW endpoint assertions
92 lines
3.0 KiB
TypeScript
92 lines
3.0 KiB
TypeScript
import { reservationsApi } from '../api/client'
|
|
import { offlineDb, upsertReservations } from '../db/offlineDb'
|
|
import { mutationQueue, generateUUID } from '../sync/mutationQueue'
|
|
import type { Reservation } from '../types'
|
|
|
|
export const reservationRepo = {
|
|
async list(tripId: number | string): Promise<{ reservations: Reservation[]; refresh: Promise<{ reservations: Reservation[] } | null> }> {
|
|
const cached = await offlineDb.reservations
|
|
.where('trip_id')
|
|
.equals(Number(tripId))
|
|
.toArray()
|
|
|
|
const refresh = (async () => {
|
|
if (!navigator.onLine) return null
|
|
try {
|
|
const result = await reservationsApi.list(tripId)
|
|
upsertReservations(result.reservations)
|
|
return result
|
|
} catch {
|
|
return null
|
|
}
|
|
})()
|
|
|
|
if (cached.length > 0) return { reservations: cached, refresh }
|
|
|
|
const fresh = await refresh
|
|
if (!fresh) return { reservations: [], refresh: Promise.resolve(null) }
|
|
return { reservations: fresh.reservations, refresh: Promise.resolve(fresh) }
|
|
},
|
|
|
|
async create(tripId: number | string, data: Record<string, unknown>): Promise<{ reservation: Reservation }> {
|
|
const tempId = -(Date.now())
|
|
const tempReservation: Reservation = {
|
|
...(data as Partial<Reservation>),
|
|
id: tempId,
|
|
trip_id: Number(tripId),
|
|
name: (data.name as string) ?? 'New reservation',
|
|
type: (data.type as string) ?? 'other',
|
|
status: 'pending',
|
|
date: (data.date as string) ?? null,
|
|
time: null,
|
|
confirmation_number: null,
|
|
notes: null,
|
|
url: null,
|
|
created_at: new Date().toISOString(),
|
|
} as Reservation
|
|
await offlineDb.reservations.put(tempReservation)
|
|
await mutationQueue.enqueue({
|
|
id: generateUUID(),
|
|
tripId: Number(tripId),
|
|
method: 'POST',
|
|
url: `/trips/${tripId}/reservations`,
|
|
body: data,
|
|
resource: 'reservations',
|
|
tempId,
|
|
})
|
|
mutationQueue.flush().catch(() => {})
|
|
return { reservation: tempReservation }
|
|
},
|
|
|
|
async update(tripId: number | string, id: number, data: Record<string, unknown>): Promise<{ reservation: Reservation }> {
|
|
const existing = await offlineDb.reservations.get(id)
|
|
const optimistic: Reservation = { ...(existing ?? {} as Reservation), ...(data as Partial<Reservation>), id }
|
|
await offlineDb.reservations.put(optimistic)
|
|
await mutationQueue.enqueue({
|
|
id: generateUUID(),
|
|
tripId: Number(tripId),
|
|
method: 'PUT',
|
|
url: `/trips/${tripId}/reservations/${id}`,
|
|
body: data,
|
|
resource: 'reservations',
|
|
})
|
|
mutationQueue.flush().catch(() => {})
|
|
return { reservation: optimistic }
|
|
},
|
|
|
|
async delete(tripId: number | string, id: number): Promise<unknown> {
|
|
await offlineDb.reservations.delete(id)
|
|
await mutationQueue.enqueue({
|
|
id: generateUUID(),
|
|
tripId: Number(tripId),
|
|
method: 'DELETE',
|
|
url: `/trips/${tripId}/reservations/${id}`,
|
|
body: undefined,
|
|
resource: 'reservations',
|
|
entityId: id,
|
|
})
|
|
mutationQueue.flush().catch(() => {})
|
|
return { success: true }
|
|
},
|
|
}
|