fix(practice): update session counters client-side after each answer
The in-session progress bar reads session.cardsShown/cardsCorrect/cardsIncorrect, but the session store's answer() never refreshed the session object — the backend tracked the counters but the client kept the stale start values (all 0), so the bar appeared frozen. answer() now mirrors the backend's increment locally; end() still replaces with authoritative server totals. Adds an E2E regression test.
This commit is contained in:
@@ -40,11 +40,20 @@ export const useSession = create<SessionState>((set, get) => ({
|
||||
await sessionsApi.attempt(s.session.id, {
|
||||
cardId: s.current.cardId, direction: s.current.direction, result, timeToAnswerMs: ttm,
|
||||
});
|
||||
// Mirror the backend's counter increments locally so the in-session
|
||||
// progress bar updates immediately (end() later replaces these with
|
||||
// the authoritative server totals).
|
||||
const updatedSession: SessionRow = {
|
||||
...s.session,
|
||||
cardsShown: s.session.cardsShown + 1,
|
||||
cardsCorrect: s.session.cardsCorrect + (result === 'correct' ? 1 : 0),
|
||||
cardsIncorrect: s.session.cardsIncorrect + (result === 'incorrect' ? 1 : 0),
|
||||
};
|
||||
const nx = await sessionsApi.next(s.session.id);
|
||||
if (nx.done) {
|
||||
set({ done: true, current: null, showAnswer: false });
|
||||
set({ session: updatedSession, done: true, current: null, showAnswer: false });
|
||||
} else {
|
||||
set({ current: nx.item, showAnswer: false, shownAt: Date.now() });
|
||||
set({ session: updatedSession, current: nx.item, showAnswer: false, shownAt: Date.now() });
|
||||
}
|
||||
},
|
||||
|
||||
|
||||
Reference in New Issue
Block a user