mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-04 18:04:18 +00:00
fix(costs): expenses and payments saved offline wait in the queue and sync once the connection is back
Every Costs write went straight to the API, so offline an expense failed with
"Unknown error" and was lost. Expenses (add, edit, delete, split, paid tick,
reorder) now go through budgetRepo and the mutation queue like places and
packing items: a temporary row offline, held behind an older queued write
online. The replay reads the expense from `item` only, so the split answer
({ members, item }) keeps the local payers and the paid answer ({ member })
never lands in the expense table. An expense linked to a place or booking
created offline is sent with the real id once that one has synced.
Payments are queued without a local row and a hint says they show once
sent; the ledger reads the balances again when the queue is through, and
offline it shows the last balances it read (new Dexie table, v10). Receipts
need an upload, so an offline save with new ones is stopped with a hint.
Refs #2601
This commit is contained in:
@@ -1,4 +1,3 @@
|
||||
import { budgetApi } from '../../api/client'
|
||||
import { budgetRepo } from '../../repo/budgetRepo'
|
||||
import type { StoreApi } from 'zustand'
|
||||
import type { TripStoreState } from '../tripStore'
|
||||
@@ -47,7 +46,7 @@ export const createBudgetSlice = (set: SetState, get: GetState): BudgetSlice =>
|
||||
try {
|
||||
// A foreign currency goes out with the rate the browser holds for it, so the server
|
||||
// can freeze one even when its own fetch fails.
|
||||
const result = await budgetApi.create(tripId, withFallbackFx(data, openTripCurrency(get, tripId)))
|
||||
const result = await budgetRepo.create(tripId, withFallbackFx(data, openTripCurrency(get, tripId)))
|
||||
set(state => ({ budgetItems: [...state.budgetItems, result.item] }))
|
||||
// The booking mirrors its expenses' total (#2084), so a new one on it changes its card.
|
||||
if (result.item.reservation_id) get().loadReservations(tripId)
|
||||
@@ -59,7 +58,7 @@ export const createBudgetSlice = (set: SetState, get: GetState): BudgetSlice =>
|
||||
|
||||
updateBudgetItem: async (tripId, id, data) => {
|
||||
try {
|
||||
const result = await budgetApi.update(tripId, id, withFallbackFx(data, openTripCurrency(get, tripId)))
|
||||
const result = await budgetRepo.update(tripId, id, withFallbackFx(data, openTripCurrency(get, tripId)))
|
||||
set(state => ({
|
||||
budgetItems: state.budgetItems.map(item => item.id === id ? result.item : item)
|
||||
}))
|
||||
@@ -78,7 +77,7 @@ export const createBudgetSlice = (set: SetState, get: GetState): BudgetSlice =>
|
||||
const prev = get().budgetItems
|
||||
set(state => ({ budgetItems: state.budgetItems.filter(item => item.id !== id) }))
|
||||
try {
|
||||
await budgetApi.delete(tripId, id)
|
||||
await budgetRepo.delete(tripId, id)
|
||||
} catch (err: unknown) {
|
||||
set({ budgetItems: prev })
|
||||
throw new Error(getApiErrorMessage(err, 'Error deleting budget item'))
|
||||
@@ -86,7 +85,7 @@ export const createBudgetSlice = (set: SetState, get: GetState): BudgetSlice =>
|
||||
},
|
||||
|
||||
setBudgetItemMembers: async (tripId, itemId, userIds) => {
|
||||
const result = await budgetApi.setMembers(tripId, itemId, userIds);
|
||||
const result = await budgetRepo.setMembers(tripId, itemId, userIds);
|
||||
set(state => ({
|
||||
budgetItems: state.budgetItems.map(item =>
|
||||
item.id === itemId ? { ...item, members: result.members, persons: result.item.persons } : item
|
||||
@@ -96,7 +95,7 @@ export const createBudgetSlice = (set: SetState, get: GetState): BudgetSlice =>
|
||||
},
|
||||
|
||||
toggleBudgetMemberPaid: async (tripId, itemId, userId, paid) => {
|
||||
await budgetApi.togglePaid(tripId, itemId, userId, paid);
|
||||
await budgetRepo.togglePaid(tripId, itemId, userId, paid);
|
||||
set(state => ({
|
||||
budgetItems: state.budgetItems.map(item =>
|
||||
item.id === itemId
|
||||
@@ -124,13 +123,13 @@ export const createBudgetSlice = (set: SetState, get: GetState): BudgetSlice =>
|
||||
return { budgetItems: [...reordered, ...remaining] }
|
||||
})
|
||||
try {
|
||||
await budgetApi.reorderItems(tripId, orderedIds)
|
||||
await budgetRepo.reorderItems(tripId, orderedIds)
|
||||
} catch (err: unknown) {
|
||||
// Reload on failure to restore the server's ordering, and tell the user
|
||||
// their reorder didn't stick (the caller fires this without awaiting, so
|
||||
// a failing reload must not escape as an unhandled rejection).
|
||||
try {
|
||||
const data = await budgetApi.list(tripId)
|
||||
const data = await budgetRepo.list(tripId)
|
||||
set({ budgetItems: data.items })
|
||||
} catch { /* offline too — the next successful load restores the order */ }
|
||||
notify(getApiErrorMessage(err, 'Error reordering budget items'), 'error')
|
||||
@@ -141,13 +140,13 @@ export const createBudgetSlice = (set: SetState, get: GetState): BudgetSlice =>
|
||||
// Optimistic: reorder items by new category order (Map preserves insertion order for numeric keys)
|
||||
set(state => ({ budgetItems: orderBudgetByCategories(state.budgetItems, orderedCategories) }))
|
||||
try {
|
||||
await budgetApi.reorderCategories(tripId, orderedCategories)
|
||||
await budgetRepo.reorderCategories(tripId, orderedCategories)
|
||||
} catch (err: unknown) {
|
||||
// Reload on failure to restore the server's ordering, and tell the user
|
||||
// their reorder didn't stick (the caller fires this without awaiting, so
|
||||
// a failing reload must not escape as an unhandled rejection).
|
||||
try {
|
||||
const data = await budgetApi.list(tripId)
|
||||
const data = await budgetRepo.list(tripId)
|
||||
set({ budgetItems: data.items })
|
||||
} catch { /* offline too — the next successful load restores the order */ }
|
||||
notify(getApiErrorMessage(err, 'Error reordering budget items'), 'error')
|
||||
@@ -157,7 +156,7 @@ export const createBudgetSlice = (set: SetState, get: GetState): BudgetSlice =>
|
||||
// Freezes a rate onto the rows the settlement could not count (see useFreezeMissingRates).
|
||||
// Not optimistic: nothing changes locally until the server says which rows it froze.
|
||||
freezeMissingRates: async (tripId, fallback) => {
|
||||
const result = await budgetApi.freezeRates(tripId, fallback ? { fallback_fx: fallback } : {})
|
||||
const result = await budgetRepo.freezeRates(tripId, fallback ? { fallback_fx: fallback } : {})
|
||||
if (result.items.length > 0) {
|
||||
const healed = new Map(result.items.map(item => [item.id, item]))
|
||||
set(state => ({ budgetItems: state.budgetItems.map(item => healed.get(item.id) ?? item) }))
|
||||
|
||||
Reference in New Issue
Block a user