mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-30 08:30:05 +00:00
38656eeffd
The trip sync and every repo list only ever upserted, so a place a collaborator deleted while this device was offline came back on the next offline read, and a trip the user had been removed from stayed on the device. A sync and a full list now replace the trip's rows, keeping only the ones created offline that have not synced yet, and a cached trip the server no longer lists is cleared.
21 lines
701 B
TypeScript
21 lines
701 B
TypeScript
import { reservationsApi } from '../api/client'
|
|
import { offlineDb, replaceTripRows } 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)
|
|
void replaceTripRows('reservations', Number(tripId), result.reservations)
|
|
return result
|
|
},
|
|
async () => ({
|
|
reservations: await offlineDb.reservations
|
|
.where('trip_id').equals(Number(tripId)).toArray(),
|
|
}),
|
|
)
|
|
},
|
|
}
|