diff --git a/web/src/lib/api/likes.test.ts b/web/src/lib/api/likes.test.ts index f5dc3b46..7c749829 100644 --- a/web/src/lib/api/likes.test.ts +++ b/web/src/lib/api/likes.test.ts @@ -69,4 +69,26 @@ describe('likes mutations', () => { | { track_ids: string[] } | undefined; expect(cached?.track_ids).not.toContain('t2'); }); + + test('a failed like reverts only itself, not a like made while it was out', async () => { + let fail!: (e: Error) => void; + (api.post as ReturnType) + .mockImplementationOnce(() => new Promise((_, reject) => { fail = reject; })) + .mockResolvedValueOnce(null); + const first = likeEntity(client, 'track', 'tA'); + await likeEntity(client, 'track', 'tB'); + fail(new Error('boom')); + await expect(first).rejects.toThrow('boom'); + const cached = client.getQueryData(qk.likedIds()) as { track_ids: string[] } | undefined; + expect(cached?.track_ids).toContain('tB'); + expect(cached?.track_ids).not.toContain('tA'); + expect(cached?.track_ids).toContain('t1'); + }); + + test('a failed unlike puts back only its own like', async () => { + (api.del as ReturnType).mockRejectedValueOnce(new Error('boom')); + await expect(unlikeEntity(client, 'track', 't1')).rejects.toThrow('boom'); + const cached = client.getQueryData(qk.likedIds()) as { track_ids: string[] } | undefined; + expect(cached?.track_ids).toEqual(['t1']); + }); }); diff --git a/web/src/lib/api/likes.ts b/web/src/lib/api/likes.ts index 85da6b0c..41a6d373 100644 --- a/web/src/lib/api/likes.ts +++ b/web/src/lib/api/likes.ts @@ -63,25 +63,39 @@ export function createLikedArtistsInfiniteQuery() { }); } -// likeEntity does an optimistic insert into the likedIds cache, fires the -// network call, and rolls back on error. Same shape for unlikeEntity. +// setLiked marks one id liked or not in the cached liked sets, leaving every +// other entry as it is. Nothing is cached before the first fetch, and then +// there is nothing to change. +function setLiked(client: QueryClient, kind: EntityKind, id: string, liked: boolean): void { + const setKey = SET_KEY_BY_KIND[kind]; + client.setQueryData(qk.likedIds(), (prev) => { + if (!prev || prev[setKey].includes(id) === liked) return prev; + return { + ...prev, + [setKey]: liked ? [...prev[setKey], id] : prev[setKey].filter((x) => x !== id) + }; + }); +} + +// wasLiked is the cached state of one id, or undefined before the first fetch. +function wasLiked(client: QueryClient, kind: EntityKind, id: string): boolean | undefined { + return client.getQueryData(qk.likedIds())?.[SET_KEY_BY_KIND[kind]].includes(id); +} + +// likeEntity shows the like straight away, then tells the server. If the +// server refuses, only this id goes back to how it was: a like or unlike made +// while the request was out keeps its own state. unlikeEntity is the same. export async function likeEntity( client: QueryClient, kind: EntityKind, id: string ): Promise { - const setKey = SET_KEY_BY_KIND[kind]; - const previous = client.getQueryData(qk.likedIds()); - if (previous) { - client.setQueryData(qk.likedIds(), { - ...previous, - [setKey]: previous[setKey].includes(id) ? previous[setKey] : [...previous[setKey], id] - }); - } + const before = wasLiked(client, kind, id); + setLiked(client, kind, id, true); try { await api.post(`/api/likes/${PATH_BY_KIND[kind]}/${id}`, undefined); } catch (err) { - if (previous) client.setQueryData(qk.likedIds(), previous); + if (before !== undefined) setLiked(client, kind, id, before); throw err; } } @@ -91,18 +105,12 @@ export async function unlikeEntity( kind: EntityKind, id: string ): Promise { - const setKey = SET_KEY_BY_KIND[kind]; - const previous = client.getQueryData(qk.likedIds()); - if (previous) { - client.setQueryData(qk.likedIds(), { - ...previous, - [setKey]: previous[setKey].filter((x) => x !== id) - }); - } + const before = wasLiked(client, kind, id); + setLiked(client, kind, id, false); try { await api.del(`/api/likes/${PATH_BY_KIND[kind]}/${id}`); } catch (err) { - if (previous) client.setQueryData(qk.likedIds(), previous); + if (before !== undefined) setLiked(client, kind, id, before); throw err; } }