From 2634bd0db05eaf09a5a504f51f6752b870f73a2f Mon Sep 17 00:00:00 2001 From: Robin Dittmar Date: Fri, 21 Aug 2026 22:22:36 +0200 Subject: [PATCH] fix: adding user to list now only works online, and it never queued --- src/database/db.ts | 1 - src/stores/__tests__/lists.spec.ts | 36 ++++++++++++++++++++++++++++++ src/stores/lists.ts | 15 ++++--------- 3 files changed, 40 insertions(+), 12 deletions(-) diff --git a/src/database/db.ts b/src/database/db.ts index abe2f40..990ebb9 100644 --- a/src/database/db.ts +++ b/src/database/db.ts @@ -14,7 +14,6 @@ export type SyncOperationType = | 'createListItem' | 'updateListItem' | 'setListItemCompleted' - | 'addUserToList' | 'removeUserFromList' | 'deleteList' | 'deleteListItem' diff --git a/src/stores/__tests__/lists.spec.ts b/src/stores/__tests__/lists.spec.ts index 117652d..c7d14ed 100644 --- a/src/stores/__tests__/lists.spec.ts +++ b/src/stores/__tests__/lists.spec.ts @@ -319,4 +319,40 @@ describe('useListsStore', () => { expect(listsApiMocks.deleteListItemApi).toHaveBeenCalledWith('item-to-delete') expect(store.pendingCount).toBe(0) }) + + it('shares list with server when online without adding to sync queue', async () => { + listsApiMocks.addUserToListApi.mockResolvedValueOnce(undefined) + + const store = useListsStore() + await store.addUserToList('list-1', 'friend@example.com') + + expect(listsApiMocks.addUserToListApi).toHaveBeenCalledWith({ + list_id: 'list-1', + email: 'friend@example.com', + }) + expect(store.pendingCount).toBe(0) + }) + + it('throws error when sharing list while offline without calling API or adding to sync queue', async () => { + Object.defineProperty(navigator, 'onLine', { value: false, configurable: true }) + + const store = useListsStore() + await expect(store.addUserToList('list-1', 'friend@example.com')).rejects.toThrow( + 'Cannot share list while offline', + ) + + expect(listsApiMocks.addUserToListApi).not.toHaveBeenCalled() + expect(store.pendingCount).toBe(0) + }) + + it('propagates error when sharing list fails on server without adding to sync queue', async () => { + listsApiMocks.addUserToListApi.mockRejectedValueOnce(new Error('User not found')) + + const store = useListsStore() + await expect(store.addUserToList('list-1', 'unknown@example.com')).rejects.toThrow( + 'User not found', + ) + + expect(store.pendingCount).toBe(0) + }) }) diff --git a/src/stores/lists.ts b/src/stores/lists.ts index 2327761..2f453d0 100644 --- a/src/stores/lists.ts +++ b/src/stores/lists.ts @@ -145,13 +145,10 @@ export const useListsStore = defineStore('lists', () => { } async function addUserToList(listId: string, email: string) { - await enqueue({ - type: 'addUserToList', - payload: { list_id: listId, email }, - localListId: listId, - }) - await refresh() - void sync() + if (typeof navigator !== 'undefined' && !navigator.onLine) { + throw new Error('Cannot share list while offline') + } + await addUserToListApi({ list_id: listId, email }) } async function removeUserFromList(listId: string, email: string) { @@ -290,10 +287,6 @@ export const useListsStore = defineStore('lists', () => { await db.listItems.update(entry.localListItemId, { pendingSync: false }) break } - case 'addUserToList': { - await addUserToListApi(entry.payload as { list_id: string; email: string }) - break - } case 'removeUserFromList': { await removeUserFromListApi(entry.payload as { list_id: string; email: string }) break