mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-26 22:57:32 +00:00
fix(client): cache a visit note saved from the place dialog
The place dialog stopped reloading the days after a save, so the new visit note only reached the Zustand store. Nothing wrote it to Dexie, the tab never sees its own assignment:updated echo, and the time save before it cached the server row with the old note. A trip reopened offline then showed the old note until the next full sync. The note now goes through assignmentRepo.setNotes, which caches the saved row on its day online and queues the write behind any other write of the same visit offline, like setTimes does. A new store action applies it, so the page hook no longer patches the store by hand. usePlaceEdits drops its direct API import and leaves the offline baseline.
This commit is contained in:
@@ -11,7 +11,7 @@ import type { Assignment, Day } from '../types'
|
||||
vi.mock('../api/assignmentEndDay', () => ({ saveAssignmentEndDay: vi.fn() }))
|
||||
vi.mock('../sync/networkMode', () => ({ isEffectivelyOffline: vi.fn(() => true) }))
|
||||
vi.mock('../sync/authGate', () => ({ isAuthed: () => true }))
|
||||
vi.mock('../api/client', () => ({ apiClient: { request: vi.fn() }, assignmentsApi: { updateTime: vi.fn(), clearDay: vi.fn() } }))
|
||||
vi.mock('../api/client', () => ({ apiClient: { request: vi.fn() }, assignmentsApi: { updateTime: vi.fn(), updateNotes: vi.fn(), clearDay: vi.fn() } }))
|
||||
const assignment = { id: 7, day_id: 1, place_id: 2, order_index: 0, assignment_time: '07:00', place: { id: 2, name: 'Berlin' } } as Assignment
|
||||
|
||||
beforeEach(async () => {
|
||||
@@ -80,6 +80,34 @@ describe('assignment time persistence', () => {
|
||||
})
|
||||
})
|
||||
|
||||
describe('assignment note persistence', () => {
|
||||
it('saves through the note route online and caches the saved row on the day', async () => {
|
||||
vi.mocked(isEffectivelyOffline).mockReturnValue(false)
|
||||
vi.mocked(assignmentsApi.updateNotes).mockResolvedValue({ assignment: { ...assignment, notes: 'Bring cash' } })
|
||||
const saved = await assignmentRepo.setNotes(9, assignment, 'Bring cash')
|
||||
expect(assignmentsApi.updateNotes).toHaveBeenCalledWith(9, 7, { notes: 'Bring cash' })
|
||||
expect(saved.notes).toBe('Bring cash')
|
||||
expect((await offlineDb.days.get(1))?.assignments?.[0]).toMatchObject({ notes: 'Bring cash', assignment_time: '07:00' })
|
||||
expect(await offlineDb.mutationQueue.count()).toBe(0)
|
||||
})
|
||||
|
||||
it('stores the offline note with a replayable write', async () => {
|
||||
const saved = await assignmentRepo.setNotes(9, assignment, null)
|
||||
expect(saved.notes).toBeNull()
|
||||
expect((await offlineDb.days.get(1))?.assignments?.[0].notes).toBeNull()
|
||||
expect(await offlineDb.mutationQueue.toArray()).toEqual([expect.objectContaining({
|
||||
url: '/trips/9/assignments/7/notes', method: 'PUT', body: { notes: null }, resource: 'assignments', entityId: 7,
|
||||
})])
|
||||
})
|
||||
|
||||
it('keeps the cached visit unchanged after a refused save', async () => {
|
||||
vi.mocked(isEffectivelyOffline).mockReturnValue(false)
|
||||
vi.mocked(assignmentsApi.updateNotes).mockRejectedValue(new Error('Denied'))
|
||||
await expect(assignmentRepo.setNotes(9, assignment, 'x')).rejects.toThrow('Denied')
|
||||
expect((await offlineDb.days.get(1))?.assignments?.[0]).toEqual(assignment)
|
||||
})
|
||||
})
|
||||
|
||||
describe('an online edit of a visit with an older write still queued', () => {
|
||||
const first = { place_time: '07:00', end_time: null }
|
||||
const second = { place_time: '09:00', end_time: null }
|
||||
|
||||
Reference in New Issue
Block a user