mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-30 08:30:05 +00:00
bdd8c08dd4
The day plan wrote straight to the API, so offline a place dropped on a day snapped back with "Error assigning place", a renamed day or a new day note failed, and a moved stop, a leg's travel mode or who joins a visit were lost. Visits (put on a day, taken off, reordered, moved, travellers, leg mode), the day's name, notes text and travel mode, and the day notes (add, edit, delete, move) now go through assignmentRepo, dayRepo and dayNoteRepo and the mutation queue: the cached day changes at once, a visit or note made offline gets a temporary id that the edits after it wait for, and an online write held behind an older queued one joins the queue, since none of these carry a version token. A new order of a day that names a visit made offline is sent with its real id, or without it when its create failed. Desktop, the phone timeline and note sheet all write through the store, so both are covered. The replay reads each answer for what it names: the day answer keeps the cached notes and visits, the participants answer lands on its visit, and a visit the server refuses with 409 (a Tour already on that day) fails instead of waiting as a conflict no retry can solve. The trip's name, description, reminder and the archive flag wait in the queue too, from the planner, the phone and the dashboard. What only the server can do says so instead of failing: new dates, a day count, a currency or a cover, a new trip, a copy or a delete, members, guests, share and invite links, and a new order of the days, which moves the dates of the bookings. Refs #2601
143 lines
5.7 KiB
TypeScript
143 lines
5.7 KiB
TypeScript
import { tripsApi } from '../api/client'
|
|
import { offlineDb, upsertTrip } from '../db/offlineDb'
|
|
import { generateUUID, mutationQueue } from '../sync/mutationQueue'
|
|
import { isEffectivelyOffline } from '../sync/networkMode'
|
|
import { onlineThenCache } from './withOfflineFallback'
|
|
import type { Trip } from '../types'
|
|
import type { ActiveTripResponse, TripUpdateRequest } from '@trek/shared'
|
|
|
|
/** A trip change refused offline because only the server can carry it out. */
|
|
export class TripNeedsConnectionError extends Error {
|
|
constructor() {
|
|
super('Changing the dates, the number of days, the currency or the cover needs a connection')
|
|
this.name = 'TripNeedsConnectionError'
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Does this change need the server? New dates or a day count rebuild the days,
|
|
* another currency rebases the costs, a cover is stored there. A field sent
|
|
* with the value the trip already has changes nothing, so the edit form, which
|
|
* always sends every field, can still save a new name offline.
|
|
*/
|
|
export function tripChangeNeedsConnection(trip: Pick<Trip, 'start_date' | 'end_date' | 'currency'> | undefined, data: TripUpdateRequest): boolean {
|
|
const differs = (key: 'start_date' | 'end_date' | 'currency') =>
|
|
data[key] !== undefined && (!trip || (data[key] ?? null) !== (trip[key] ?? null))
|
|
return differs('start_date') || differs('end_date') || differs('currency')
|
|
|| data.day_count !== undefined || data.cover_image !== undefined
|
|
}
|
|
|
|
/**
|
|
* Offline stand-in for GET /trips/active. Mirrors the server's ranking
|
|
* (trips.service.ts activeTrip): the trip running today, else the next one to
|
|
* start, else the most recently started. Dates are plain local calendar dates.
|
|
*/
|
|
function pickActive(trips: Trip[]): Trip | null {
|
|
const now = new Date()
|
|
const today = `${now.getFullYear()}-${String(now.getMonth() + 1).padStart(2, '0')}-${String(now.getDate()).padStart(2, '0')}`
|
|
const relevance = (t: Trip): number => {
|
|
if (t.start_date && t.end_date && t.start_date <= today && t.end_date >= today) return 0
|
|
if (t.start_date && t.start_date >= today) return 1
|
|
return 2
|
|
}
|
|
const ranked = [...trips].sort((a, b) => {
|
|
const ra = relevance(a)
|
|
const rb = relevance(b)
|
|
if (ra !== rb) return ra - rb
|
|
// Upcoming: soonest first. Everything else: most recently started first.
|
|
return ra === 2
|
|
? (b.start_date ?? '').localeCompare(a.start_date ?? '')
|
|
: (a.start_date ?? '').localeCompare(b.start_date ?? '')
|
|
})
|
|
return ranked[0] ?? null
|
|
}
|
|
|
|
export const tripRepo = {
|
|
async list(): Promise<{ trips: Trip[]; archivedTrips: Trip[] }> {
|
|
return onlineThenCache(
|
|
async () => {
|
|
const [active, archived] = await Promise.all([
|
|
tripsApi.list(),
|
|
tripsApi.list({ archived: 1 }),
|
|
])
|
|
active.trips.forEach(t => upsertTrip(t))
|
|
archived.trips.forEach(t => upsertTrip(t))
|
|
return { trips: active.trips, archivedTrips: archived.trips }
|
|
},
|
|
async () => {
|
|
const all = await offlineDb.trips.toArray()
|
|
return {
|
|
trips: all.filter(t => !t.is_archived),
|
|
archivedTrips: all.filter(t => t.is_archived),
|
|
}
|
|
},
|
|
)
|
|
},
|
|
|
|
/**
|
|
* The startup redirect asks for this on the very first paint, so it has to
|
|
* answer offline too — otherwise "open my active trip on startup" drops the
|
|
* user on the dashboard whenever the launch has no network.
|
|
*/
|
|
async active(): Promise<ActiveTripResponse> {
|
|
return onlineThenCache(
|
|
() => tripsApi.active(),
|
|
async () => {
|
|
const all = await offlineDb.trips.toArray()
|
|
const trip = pickActive(all.filter(t => !t.is_archived))
|
|
return {
|
|
trip: trip
|
|
? { id: trip.id, title: trip.title, start_date: trip.start_date, end_date: trip.end_date }
|
|
: null,
|
|
}
|
|
},
|
|
)
|
|
},
|
|
|
|
/**
|
|
* A change to the trip itself. Its name, description, reminder and the archive
|
|
* flag wait in the queue offline, and online too while an older write to the
|
|
* trip still waits, since trip writes carry no version token. New dates, a new
|
|
* day count, another currency or a cover need the server (it rebuilds the days,
|
|
* rebases the costs or stores the image), so offline they are refused with
|
|
* TripNeedsConnectionError and nothing is written.
|
|
*/
|
|
async update(tripId: number | string, data: TripUpdateRequest): Promise<{ trip: Trip }> {
|
|
const id = Number(tripId)
|
|
if (!(await mutationQueue.mustQueue('trips', id))) {
|
|
const result: { trip: Trip } = await tripsApi.update(tripId, data)
|
|
const cached = await offlineDb.trips.get(id)
|
|
void upsertTrip({ ...cached, ...result.trip })
|
|
return result
|
|
}
|
|
const cached = await offlineDb.trips.get(id)
|
|
if (isEffectivelyOffline() && tripChangeNeedsConnection(cached, data)) throw new TripNeedsConnectionError()
|
|
const { date_shift_mode: _mode, ...fields } = data
|
|
const trip = { ...(cached ?? { id }), ...fields } as Trip
|
|
await offlineDb.transaction('rw', offlineDb.trips, offlineDb.mutationQueue, async () => {
|
|
await offlineDb.trips.put(trip)
|
|
await mutationQueue.enqueue({
|
|
id: generateUUID(), tripId: id, method: 'PUT', url: `/trips/${id}`,
|
|
body: data, resource: 'trips', entityId: id,
|
|
})
|
|
})
|
|
mutationQueue.sendSoon()
|
|
return { trip }
|
|
},
|
|
|
|
async get(tripId: number | string): Promise<{ trip: Trip }> {
|
|
return onlineThenCache(
|
|
async () => {
|
|
const result = await tripsApi.get(tripId)
|
|
void upsertTrip(result.trip)
|
|
return result
|
|
},
|
|
async () => {
|
|
const cached = await offlineDb.trips.get(Number(tripId))
|
|
if (cached) return { trip: cached }
|
|
throw new Error('No cached trip data available offline')
|
|
},
|
|
)
|
|
},
|
|
}
|