fix: update votedBooks state immediately on swipe so history drawer isn't empty
doAction() was fire-and-forgetting the POST but never updating the client-side votedBooks array. History was only populated from SSR data.votedBooks (loaded at page init), so any votes cast during the current session were invisible in the drawer until a full page reload. Now we prepend/replace an entry in votedBooks optimistically the moment a swipe action fires.
This commit is contained in:
@@ -239,6 +239,16 @@
|
||||
body: JSON.stringify({ slug: book.slug, action })
|
||||
});
|
||||
|
||||
// Optimistically add/update the history list so the drawer shows it immediately.
|
||||
// If this slug was already voted (e.g. swiped twice via undo+re-swipe), replace it.
|
||||
const existing = votedBooks.findIndex((v) => v.slug === book.slug);
|
||||
const entry: VotedBook = { slug: book.slug, action, votedAt: new Date().toISOString(), book };
|
||||
if (existing !== -1) {
|
||||
votedBooks = [entry, ...votedBooks.filter((_, i) => i !== existing)];
|
||||
} else {
|
||||
votedBooks = [entry, ...votedBooks];
|
||||
}
|
||||
|
||||
// Fly out
|
||||
transitioning = true;
|
||||
const target = flyTargets[action];
|
||||
|
||||
Reference in New Issue
Block a user