Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions frontend/src/components/NotebookView.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,34 @@ describe("cross-course notebook", () => {
).toHaveAttribute("href", "#/account");
expect(request).not.toHaveBeenCalled();
});

it("returns to the last available page when notes disappear during pagination", async () => {
const user = userEvent.setup();
const firstPage = {
...page,
items: Array.from({ length: page.limit }, (_, index) => ({
...page.items[0],
lesson_id: `note-${index + 1}`,
lesson_title: `Lesson ${index + 1}`,
})),
};
const request = vi
.spyOn(api, "notebook")
.mockResolvedValueOnce({ ...firstPage, total: 51 })
.mockResolvedValueOnce({ ...page, items: [], total: 50, offset: 50 })
.mockResolvedValueOnce({ ...firstPage, total: 50 });
render(
<NotebookView hasSession courses={[course]} needsVersionReview={false} />,
);

await user.click(await screen.findByRole("button", { name: "More notes" }));

expect(await screen.findByText("50 saved notes")).toBeInTheDocument();
expect(screen.getByRole("link", { name: "Lesson 50" })).toBeInTheDocument();
expect(request.mock.calls).toEqual([
["", undefined, 0],
["", undefined, 50],
["", undefined, 0],
]);
});
});
20 changes: 15 additions & 5 deletions frontend/src/components/NotebookView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,23 @@ export function NotebookView({
api
.notebook(query, courseId || undefined, offset)
.then((result) => {
if (active) setPage(result);
if (!active) return;
if (offset > 0 && result.items.length === 0) {
// Notes may have changed since the learner opened the previous page.
const lastPage = Math.max(
0,
Math.ceil(result.total / pageSize) - 1,
);
setOffset(Math.min(lastPage * pageSize, offset - pageSize));
return;
}
setPage(result);
setLoading(false);
})
.catch((requestError) => {
if (active) setError(errorMessage(requestError));
})
.finally(() => {
if (active) setLoading(false);
if (!active) return;
setError(errorMessage(requestError));
setLoading(false);
});
}, 200);

Expand Down
Loading