mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-01 17:03:15 +00:00
feat(tours): tours read and write through the offline core
A tourRepo replaces the direct toursApi calls: reads fall back to a new Dexie tours table, and a tour saved or edited offline is written there and queued like a place, under one temporary id for the place and its facet. The queue moves both to the real id on replay and drops both when the server refuses the create. Trip sync caches each trip's tours while the addon is on, so a tour no longer reads as a plain place offline. The tour metrics move to shared so the offline copy shows the server's numbers, and useTourPlaceIds keeps one Set per change as intended.
This commit is contained in:
@@ -2,7 +2,7 @@ import React, { useEffect, useRef, useState } from 'react'
|
||||
import { Check, ChevronDown, FileDown } from 'lucide-react'
|
||||
import type { TourListItem } from '@trek/shared'
|
||||
import type { Day } from '../../types'
|
||||
import { toursApi } from '../../api/client'
|
||||
import { tourRepo } from '../../repo/tourRepo'
|
||||
import { useTripStore } from '../../store/tripStore'
|
||||
import { useTranslation } from '../../i18n'
|
||||
import { useToast } from '../shared/Toast'
|
||||
@@ -63,7 +63,7 @@ export default function ToursSidebar({ tripId, days, tours, loading = false, sel
|
||||
|
||||
setImporting(true)
|
||||
try {
|
||||
const result = await toursApi.importGpx(tripId, file)
|
||||
const result = await tourRepo.importGpx(tripId, file)
|
||||
if (result.tours.length > 0) {
|
||||
// The server publishes place:created with sender echo suppression.
|
||||
// Reload the importing client's trip so new places and route geometry
|
||||
|
||||
@@ -17,7 +17,7 @@ const { routeWalkingTour, enrichTourElevations, createTour, detailTour, updateTo
|
||||
}))
|
||||
|
||||
vi.mock('./tourRouting', () => ({ routeWalkingTour, enrichTourElevations }))
|
||||
vi.mock('../../../api/client', () => ({ toursApi: { create: createTour, detail: detailTour, update: updateTour } }))
|
||||
vi.mock('../../../repo/tourRepo', () => ({ tourRepo: { create: createTour, detail: detailTour, update: updateTour } }))
|
||||
vi.mock('../useTourPermissions', () => ({ useTourPermissions: ({ canEdit = true, canAssign = true }: { canEdit?: boolean; canAssign?: boolean }) => ({ canEdit, canAssign }) }))
|
||||
|
||||
import { useTourPlanner } from './useTourPlanner'
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { useCallback, useEffect, useMemo, useRef, useState } from 'react'
|
||||
import type { TourCreateResponse, TourListItem, TourWaypointRole, TourMaxHikingDifficulty } from '@trek/shared'
|
||||
import { toursApi } from '../../../api/client'
|
||||
import { tourRepo } from '../../../repo/tourRepo'
|
||||
import { randomId } from '../../../utils/randomId'
|
||||
import type { TourBaseLayer } from '../../Map/MapLayerSwitcher'
|
||||
import { analyzeRouteGeometry, type RouteProfileFocus } from '../../../utils/routeGeometry'
|
||||
@@ -412,7 +412,7 @@ export function useTourPlanner({ tripId, active = true, onSaved, canEdit: editPe
|
||||
setOpeningTourId(tour.place_id)
|
||||
setError(null)
|
||||
try {
|
||||
const result = await toursApi.detail(tripId, tour.place_id, controller.signal)
|
||||
const result = await tourRepo.detail(tripId, tour.place_id, controller.signal)
|
||||
if (!requestIsCurrent() || saveInFlightRef.current) return false
|
||||
if (result.waypoints.length < 2) {
|
||||
setError('open')
|
||||
@@ -591,8 +591,8 @@ export function useTourPlanner({ tripId, active = true, onSaved, canEdit: editPe
|
||||
&& draftRevisionRef.current === submittedRevision
|
||||
try {
|
||||
const result = submittedPlaceId === null
|
||||
? await toursApi.create(tripId, payload)
|
||||
: await toursApi.update(tripId, submittedPlaceId, payload)
|
||||
? await tourRepo.create(tripId, payload)
|
||||
: await tourRepo.update(tripId, submittedPlaceId, payload)
|
||||
if (stillCurrent()) {
|
||||
localStorage.removeItem(storageKey)
|
||||
setHasUnsavedChanges(false)
|
||||
|
||||
@@ -22,7 +22,7 @@ const state = vi.hoisted(() => ({
|
||||
vi.mock('../../store/tripStore', () => ({ useTripStore: (selector: (store: unknown) => unknown) => selector({ trip: state.trip, loadTrip: state.loadTrip }) }))
|
||||
vi.mock('../../store/authStore', () => ({ useAuthStore: (selector: (store: unknown) => unknown) => selector({ user: state.user }) }))
|
||||
vi.mock('../../store/settingsStore', () => ({ useSettingsStore: (selector: (store: unknown) => unknown) => selector({ settings: { distance_unit: 'metric' } }) }))
|
||||
vi.mock('../../api/client', () => ({ toursApi: { importGpx: state.importGpx, create: state.create, update: state.update, detail: state.detail } }))
|
||||
vi.mock('../../repo/tourRepo', () => ({ tourRepo: { importGpx: state.importGpx, create: state.create, update: state.update, detail: state.detail } }))
|
||||
vi.mock('./planner/tourRouting', () => ({ routeWalkingTour: state.route, enrichTourElevations: state.elevation }))
|
||||
vi.mock('../../i18n', () => ({ useTranslation: () => ({ t: (key: string) => key, locale: 'en' }), translateApiError: () => 'error' }))
|
||||
vi.mock('../shared/Toast', () => ({ useToast: () => ({ success: vi.fn(), error: vi.fn(), warning: vi.fn(), info: vi.fn() }) }))
|
||||
|
||||
@@ -9,7 +9,7 @@ import ToursSidebar from '../Tours/ToursSidebar'
|
||||
import type { Day } from '../../types'
|
||||
import CustomSelect from './CustomSelect'
|
||||
|
||||
vi.mock('../../api/client', () => ({ toursApi: {} }))
|
||||
vi.mock('../../repo/tourRepo', () => ({ tourRepo: {} }))
|
||||
vi.mock('../../store/settingsStore', () => ({
|
||||
useSettingsStore: (selector: (state: { settings: { language: string; distance_unit: string } }) => unknown) =>
|
||||
selector({ settings: { language: 'en', distance_unit: 'metric' } }),
|
||||
|
||||
Reference in New Issue
Block a user