fix(web): a failed like reverts only itself
A like or unlike that failed restored the whole liked list from before it started, undoing any like made while it was in flight. Now only its own id goes back to how it was. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -69,4 +69,26 @@ describe('likes mutations', () => {
|
|||||||
| { track_ids: string[] } | undefined;
|
| { track_ids: string[] } | undefined;
|
||||||
expect(cached?.track_ids).not.toContain('t2');
|
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<typeof vi.fn>)
|
||||||
|
.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<typeof vi.fn>).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']);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
+28
-20
@@ -63,25 +63,39 @@ export function createLikedArtistsInfiniteQuery() {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// likeEntity does an optimistic insert into the likedIds cache, fires the
|
// setLiked marks one id liked or not in the cached liked sets, leaving every
|
||||||
// network call, and rolls back on error. Same shape for unlikeEntity.
|
// 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<LikedIdsResponse>(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<LikedIdsResponse>(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(
|
export async function likeEntity(
|
||||||
client: QueryClient,
|
client: QueryClient,
|
||||||
kind: EntityKind,
|
kind: EntityKind,
|
||||||
id: string
|
id: string
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
const setKey = SET_KEY_BY_KIND[kind];
|
const before = wasLiked(client, kind, id);
|
||||||
const previous = client.getQueryData<LikedIdsResponse>(qk.likedIds());
|
setLiked(client, kind, id, true);
|
||||||
if (previous) {
|
|
||||||
client.setQueryData<LikedIdsResponse>(qk.likedIds(), {
|
|
||||||
...previous,
|
|
||||||
[setKey]: previous[setKey].includes(id) ? previous[setKey] : [...previous[setKey], id]
|
|
||||||
});
|
|
||||||
}
|
|
||||||
try {
|
try {
|
||||||
await api.post(`/api/likes/${PATH_BY_KIND[kind]}/${id}`, undefined);
|
await api.post(`/api/likes/${PATH_BY_KIND[kind]}/${id}`, undefined);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
if (previous) client.setQueryData(qk.likedIds(), previous);
|
if (before !== undefined) setLiked(client, kind, id, before);
|
||||||
throw err;
|
throw err;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -91,18 +105,12 @@ export async function unlikeEntity(
|
|||||||
kind: EntityKind,
|
kind: EntityKind,
|
||||||
id: string
|
id: string
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
const setKey = SET_KEY_BY_KIND[kind];
|
const before = wasLiked(client, kind, id);
|
||||||
const previous = client.getQueryData<LikedIdsResponse>(qk.likedIds());
|
setLiked(client, kind, id, false);
|
||||||
if (previous) {
|
|
||||||
client.setQueryData<LikedIdsResponse>(qk.likedIds(), {
|
|
||||||
...previous,
|
|
||||||
[setKey]: previous[setKey].filter((x) => x !== id)
|
|
||||||
});
|
|
||||||
}
|
|
||||||
try {
|
try {
|
||||||
await api.del(`/api/likes/${PATH_BY_KIND[kind]}/${id}`);
|
await api.del(`/api/likes/${PATH_BY_KIND[kind]}/${id}`);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
if (previous) client.setQueryData(qk.likedIds(), previous);
|
if (before !== undefined) setLiked(client, kind, id, before);
|
||||||
throw err;
|
throw err;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user