mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-04 18:04:18 +00:00
fix(planner): places put on a day, day names, day notes and trip details changed offline wait in the queue and sync once the connection is back
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
This commit is contained in:
@@ -1,10 +1,10 @@
|
||||
import { assignmentsApi } from '../../api/client'
|
||||
import type { StoreApi } from 'zustand'
|
||||
import type { TripStoreState } from '../tripStore'
|
||||
import type { Assignment, AssignmentsMap } from '../../types'
|
||||
import { getApiErrorMessage } from '../../types'
|
||||
import { assignmentRepo, type AssignmentTimes } from '../../repo/assignmentRepo'
|
||||
import { mergeAssignmentPlace } from './placesSlice'
|
||||
import { nextTempId } from '../../sync/mutationQueue'
|
||||
|
||||
type SetState = StoreApi<TripStoreState>['setState']
|
||||
type GetState = StoreApi<TripStoreState>['getState']
|
||||
@@ -99,16 +99,16 @@ export const createAssignmentsSlice = (set: SetState, get: GetState): Assignment
|
||||
throw err
|
||||
}
|
||||
},
|
||||
// Who joins the visit and how its legs are travelled are online writes, as they are
|
||||
// from the place inspector and the leg menu; the answer is written into the day.
|
||||
// Who joins the visit and how its legs are travelled; offline both wait in the
|
||||
// queue. The participants the repo answers with are written into the day.
|
||||
setAssignmentParticipants: async (tripId, dayId, assignmentId, userIds) => {
|
||||
const data = await assignmentsApi.setParticipants(tripId, assignmentId, userIds)
|
||||
const participants = await assignmentRepo.setParticipants(tripId, assignmentId, userIds)
|
||||
set(state => ({
|
||||
assignments: { ...state.assignments, [String(dayId)]: (state.assignments[String(dayId)] || []).map(a => a.id === assignmentId ? { ...a, participants: data.participants } : a) },
|
||||
assignments: { ...state.assignments, [String(dayId)]: (state.assignments[String(dayId)] || []).map(a => a.id === assignmentId ? { ...a, participants } : a) },
|
||||
}))
|
||||
},
|
||||
setAssignmentLegMode: async (tripId, dayId, assignmentId, mode, direction = 'outgoing') => {
|
||||
await assignmentsApi.updateTransport(tripId, assignmentId, mode, direction)
|
||||
await assignmentRepo.setLegMode(tripId, assignmentId, mode, direction)
|
||||
const field = direction === 'incoming' ? 'incoming_leg_transport_mode' : 'leg_transport_mode'
|
||||
set(state => ({
|
||||
assignments: { ...state.assignments, [String(dayId)]: (state.assignments[String(dayId)] || []).map(a => a.id === assignmentId ? { ...a, [field]: mode } : a) },
|
||||
@@ -119,7 +119,7 @@ export const createAssignmentsSlice = (set: SetState, get: GetState): Assignment
|
||||
const place = state.places.find(p => p.id === Number.parseInt(String(placeId)))
|
||||
if (!place) return
|
||||
|
||||
const tempId = Date.now() * -1
|
||||
const tempId = nextTempId()
|
||||
// Sorted first, as moveAssignment does: the position is an index in the day as
|
||||
// it is shown, and that day is sorted by order_index. The stored list is not
|
||||
// always, a stop that arrived over the socket is appended wherever its
|
||||
@@ -145,11 +145,11 @@ export const createAssignmentsSlice = (set: SetState, get: GetState): Assignment
|
||||
}))
|
||||
|
||||
try {
|
||||
const data = await assignmentsApi.create(tripId, dayId, { place_id: placeId })
|
||||
const created = await assignmentRepo.create(tripId, Number(dayId), place)
|
||||
const newAssignment: Assignment = {
|
||||
...data.assignment,
|
||||
place: data.assignment.place || place,
|
||||
order_index: position != null ? insertIdx : data.assignment.order_index,
|
||||
...created,
|
||||
place: created.place || place,
|
||||
order_index: position != null ? insertIdx : created.order_index,
|
||||
}
|
||||
set(state => ({
|
||||
assignments: {
|
||||
@@ -161,10 +161,10 @@ export const createAssignmentsSlice = (set: SetState, get: GetState): Assignment
|
||||
}))
|
||||
if (position != null) {
|
||||
const updated = get().assignments[String(dayId)] || []
|
||||
const orderedIds = updated.map(a => a.id).filter(id => id > 0)
|
||||
const orderedIds = updated.map(a => a.id)
|
||||
if (orderedIds.length > 0) {
|
||||
try {
|
||||
await assignmentsApi.reorder(tripId, dayId, orderedIds)
|
||||
await assignmentRepo.reorder(tripId, Number(dayId), orderedIds)
|
||||
set(state => {
|
||||
const items = state.assignments[String(dayId)] || []
|
||||
const reordered = orderedIds.map((id, idx) => {
|
||||
@@ -181,7 +181,7 @@ export const createAssignmentsSlice = (set: SetState, get: GetState): Assignment
|
||||
} catch {}
|
||||
}
|
||||
}
|
||||
return data.assignment
|
||||
return created
|
||||
} catch (err: unknown) {
|
||||
set(state => ({
|
||||
assignments: {
|
||||
@@ -204,7 +204,7 @@ export const createAssignmentsSlice = (set: SetState, get: GetState): Assignment
|
||||
}))
|
||||
|
||||
try {
|
||||
await assignmentsApi.delete(tripId, dayId, assignmentId)
|
||||
await assignmentRepo.remove(tripId, Number(dayId), assignmentId)
|
||||
} catch (err: unknown) {
|
||||
set({ assignments: prevAssignments })
|
||||
throw new Error(getApiErrorMessage(err, 'Error removing assignment'))
|
||||
@@ -238,7 +238,7 @@ export const createAssignmentsSlice = (set: SetState, get: GetState): Assignment
|
||||
}))
|
||||
|
||||
try {
|
||||
await assignmentsApi.reorder(tripId, dayId, orderedIds)
|
||||
await assignmentRepo.reorder(tripId, Number(dayId), orderedIds)
|
||||
} catch (err: unknown) {
|
||||
set({ assignments: prevAssignments })
|
||||
throw new Error(getApiErrorMessage(err, 'Error reordering'))
|
||||
@@ -271,10 +271,7 @@ export const createAssignmentsSlice = (set: SetState, get: GetState): Assignment
|
||||
}))
|
||||
|
||||
try {
|
||||
await assignmentsApi.move(tripId, assignmentId, toDayId, insertAt)
|
||||
if (newToItems.length > 1) {
|
||||
await assignmentsApi.reorder(tripId, toDayId, newToItems.map(a => a.id))
|
||||
}
|
||||
await assignmentRepo.move(tripId, assignment, Number(toDayId), insertAt, newToItems.map(a => a.id))
|
||||
} catch (err: unknown) {
|
||||
set({ assignments: prevAssignments })
|
||||
throw new Error(getApiErrorMessage(err, 'Error moving assignment'))
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
import { daysApi, dayNotesApi } from '../../api/client'
|
||||
import { dayRepo } from '../../repo/dayRepo'
|
||||
import { dayNoteRepo } from '../../repo/dayNoteRepo'
|
||||
import type { StoreApi } from 'zustand'
|
||||
import type { TripStoreState } from '../tripStore'
|
||||
import type { DayNote } from '../../types'
|
||||
import { getApiErrorMessage } from '../../types'
|
||||
import { nextTempId } from '../../sync/mutationQueue'
|
||||
|
||||
type SetState = StoreApi<TripStoreState>['setState']
|
||||
type GetState = StoreApi<TripStoreState>['getState']
|
||||
@@ -19,7 +21,7 @@ export interface DayNotesSlice {
|
||||
export const createDayNotesSlice = (set: SetState, get: GetState): DayNotesSlice => ({
|
||||
updateDayNotes: async (tripId, dayId, notes) => {
|
||||
try {
|
||||
await daysApi.update(tripId, dayId, { notes })
|
||||
await dayRepo.update(tripId, Number(dayId), { notes })
|
||||
set(state => ({
|
||||
days: state.days.map(d => d.id === Number.parseInt(String(dayId)) ? { ...d, notes } : d)
|
||||
}))
|
||||
@@ -30,7 +32,7 @@ export const createDayNotesSlice = (set: SetState, get: GetState): DayNotesSlice
|
||||
|
||||
updateDayTitle: async (tripId, dayId, title) => {
|
||||
try {
|
||||
await daysApi.update(tripId, dayId, { title })
|
||||
await dayRepo.update(tripId, Number(dayId), { title })
|
||||
set(state => ({
|
||||
days: state.days.map(d => d.id === Number.parseInt(String(dayId)) ? { ...d, title } : d)
|
||||
}))
|
||||
@@ -40,7 +42,7 @@ export const createDayNotesSlice = (set: SetState, get: GetState): DayNotesSlice
|
||||
},
|
||||
|
||||
addDayNote: async (tripId, dayId, data) => {
|
||||
const tempId = Date.now() * -1
|
||||
const tempId = nextTempId()
|
||||
const tempNote: DayNote = { id: tempId, day_id: dayId as number, ...data, created_at: new Date().toISOString() } as DayNote
|
||||
set(state => ({
|
||||
dayNotes: {
|
||||
@@ -49,7 +51,7 @@ export const createDayNotesSlice = (set: SetState, get: GetState): DayNotesSlice
|
||||
}
|
||||
}))
|
||||
try {
|
||||
const result = await dayNotesApi.create(tripId, dayId, data)
|
||||
const result = await dayNoteRepo.create(tripId, Number(dayId), data)
|
||||
set(state => ({
|
||||
dayNotes: {
|
||||
...state.dayNotes,
|
||||
@@ -70,7 +72,8 @@ export const createDayNotesSlice = (set: SetState, get: GetState): DayNotesSlice
|
||||
|
||||
updateDayNote: async (tripId, dayId, id, data) => {
|
||||
try {
|
||||
const result = await dayNotesApi.update(tripId, dayId, id, data)
|
||||
const before = (get().dayNotes[String(dayId)] || []).find(n => n.id === id) ?? ({ id, day_id: Number(dayId), text: '' } as DayNote)
|
||||
const result = await dayNoteRepo.update(tripId, Number(dayId), before, data)
|
||||
set(state => ({
|
||||
dayNotes: {
|
||||
...state.dayNotes,
|
||||
@@ -92,7 +95,7 @@ export const createDayNotesSlice = (set: SetState, get: GetState): DayNotesSlice
|
||||
}
|
||||
}))
|
||||
try {
|
||||
await dayNotesApi.delete(tripId, dayId, id)
|
||||
await dayNoteRepo.delete(tripId, Number(dayId), id)
|
||||
} catch (err: unknown) {
|
||||
set({ dayNotes: prev })
|
||||
throw new Error(getApiErrorMessage(err, 'Error deleting note'))
|
||||
@@ -119,14 +122,15 @@ export const createDayNotesSlice = (set: SetState, get: GetState): DayNotesSlice
|
||||
// written: a move is a delete plus a create, so anything omitted here is
|
||||
// silently dropped — which is how a coloured note lost its colour on the
|
||||
// way to another day (#1629).
|
||||
const result = await dayNotesApi.create(tripId, toDayId, {
|
||||
// Offline both halves wait in the queue in this order, so the replay keeps it.
|
||||
const result = await dayNoteRepo.create(tripId, Number(toDayId), {
|
||||
text: note.text, time: note.time, icon: note.icon, color: note.color ?? null, sort_order,
|
||||
})
|
||||
try {
|
||||
await dayNotesApi.delete(tripId, fromDayId, noteId)
|
||||
await dayNoteRepo.delete(tripId, Number(fromDayId), noteId)
|
||||
} catch (delErr: unknown) {
|
||||
// The source survived, so drop the copy rather than leave a duplicate behind.
|
||||
await dayNotesApi.delete(tripId, toDayId, result.note.id).catch(() => {})
|
||||
await dayNoteRepo.delete(tripId, Number(toDayId), result.note.id).catch(() => {})
|
||||
throw delErr
|
||||
}
|
||||
set(s => ({
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
import { daysApi } from '../../api/client'
|
||||
import { dayRepo } from '../../repo/dayRepo'
|
||||
import type { StoreApi } from 'zustand'
|
||||
import type { TripStoreState } from '../tripStore'
|
||||
@@ -14,6 +13,7 @@ export interface DaysSlice {
|
||||
insertDay: (tripId: number | string, position?: number) => Promise<Day | undefined>
|
||||
appendDatedDay: (tripId: number | string) => Promise<Day>
|
||||
deleteDay: (tripId: number | string, dayId: number) => Promise<void>
|
||||
setDayTransportMode: (tripId: number | string, dayId: number, mode: string | null) => Promise<void>
|
||||
}
|
||||
|
||||
type DayState = Pick<TripStoreState, 'days' | 'assignments' | 'dayNotes' | 'selectedDayId'>
|
||||
@@ -60,7 +60,7 @@ export const createDaysSlice = (set: SetState, get: GetState): DaysSlice => ({
|
||||
set({ days: optimistic })
|
||||
|
||||
try {
|
||||
await daysApi.reorder(tripId, orderedIds)
|
||||
await dayRepo.reorder(tripId, orderedIds)
|
||||
await get().refreshDays(tripId)
|
||||
await get().loadReservations(tripId)
|
||||
} catch (err: unknown) {
|
||||
@@ -73,7 +73,7 @@ export const createDaysSlice = (set: SetState, get: GetState): DaysSlice => ({
|
||||
// trip this extends the trip by one day and re-pins dates server-side.
|
||||
insertDay: async (tripId, position) => {
|
||||
try {
|
||||
const result = await daysApi.create(tripId, { position })
|
||||
const result = await dayRepo.insert(tripId, position)
|
||||
await get().refreshDays(tripId)
|
||||
await get().loadReservations(tripId)
|
||||
return result.day
|
||||
@@ -118,4 +118,22 @@ export const createDaysSlice = (set: SetState, get: GetState): DaysSlice => ({
|
||||
await get().refreshDays(tripId)
|
||||
await get().loadReservations(tripId)
|
||||
},
|
||||
|
||||
// The whole-day default route mode. The day shows it at once; a refusal puts
|
||||
// the previous one back. Offline it waits in the queue.
|
||||
setDayTransportMode: async (tripId, dayId, mode) => {
|
||||
const before = get().days.find(d => d.id === dayId)?.default_transport_mode ?? null
|
||||
const apply = (value: string | null) => set(state => ({
|
||||
days: state.days.map(d => (d.id === dayId ? { ...d, default_transport_mode: value } : d)),
|
||||
}))
|
||||
apply(mode)
|
||||
const outcome = await dayRepo.setTransport(tripId, dayId, mode).then(
|
||||
() => null,
|
||||
(failure: unknown) => ({ failure }),
|
||||
)
|
||||
if (outcome) {
|
||||
apply(before)
|
||||
throw new Error(getApiErrorMessage(outcome.failure, 'Error updating day'))
|
||||
}
|
||||
},
|
||||
})
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import { create } from 'zustand'
|
||||
import type { StoreApi } from 'zustand'
|
||||
import { tripsApi, tagsApi, categoriesApi } from '../api/client'
|
||||
import { tagsApi, categoriesApi } from '../api/client'
|
||||
import { offlineDb } from '../db/offlineDb'
|
||||
import { tripRepo } from '../repo/tripRepo'
|
||||
import { tripRepo, TripNeedsConnectionError } from '../repo/tripRepo'
|
||||
import { dayRepo } from '../repo/dayRepo'
|
||||
import { placeRepo } from '../repo/placeRepo'
|
||||
import { packingRepo } from '../repo/packingRepo'
|
||||
@@ -243,7 +243,7 @@ export const useTripStore = create<TripStoreState>((set, get) => ({
|
||||
updateTrip: async (tripId: number | string, data: Partial<Trip> & { date_shift_mode?: 'keep_bookings' | 'shift_all' }) => {
|
||||
try {
|
||||
const before = get().trip
|
||||
const result = await tripsApi.update(tripId, data)
|
||||
const result = await tripRepo.update(tripId, data)
|
||||
set({ trip: result.trip })
|
||||
// New dates rebuild the day rows, and a stay on a day that went is gone
|
||||
// with it. Stays live in the planner, not here, so every caller of this
|
||||
@@ -266,6 +266,7 @@ export const useTripStore = create<TripStoreState>((set, get) => ({
|
||||
await get().loadReservations(tripId)
|
||||
return result.trip
|
||||
} catch (err: unknown) {
|
||||
if (err instanceof TripNeedsConnectionError) throw err
|
||||
throw new Error(getApiErrorMessage(err, 'Error updating trip'))
|
||||
}
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user