Skip to content

Commit b7806fd

Browse files
authored
fix(site): paginate audit logs (coder#8513)
1 parent 9aae983 commit b7806fd

File tree

2 files changed

+37
-6
lines changed

2 files changed

+37
-6
lines changed

site/src/pages/AuditPage/AuditPage.test.tsx

+29-3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { server } from "testHelpers/server"
1515

1616
import * as CreateDayString from "utils/createDayString"
1717
import AuditPage from "./AuditPage"
18+
import { DEFAULT_RECORDS_PER_PAGE } from "components/PaginationWidget/utils"
1819

1920
interface RenderPageOptions {
2021
filter?: string
@@ -67,6 +68,27 @@ describe("AuditPage", () => {
6768
screen.getByTestId(`audit-log-row-${MockAuditLog2.id}`)
6869
})
6970

71+
it("renders page 5", async () => {
72+
// Given
73+
const page = 5
74+
const getAuditLogsSpy = jest.spyOn(API, "getAuditLogs").mockResolvedValue({
75+
audit_logs: [MockAuditLog, MockAuditLog2],
76+
count: 2,
77+
})
78+
79+
// When
80+
await renderPage({ page: page })
81+
82+
// Then
83+
expect(getAuditLogsSpy).toBeCalledWith({
84+
limit: DEFAULT_RECORDS_PER_PAGE,
85+
offset: DEFAULT_RECORDS_PER_PAGE * (page - 1),
86+
q: "",
87+
})
88+
screen.getByTestId(`audit-log-row-${MockAuditLog.id}`)
89+
screen.getByTestId(`audit-log-row-${MockAuditLog2.id}`)
90+
})
91+
7092
describe("Filtering", () => {
7193
it("filters by URL", async () => {
7294
const getAuditLogsSpy = jest
@@ -76,7 +98,11 @@ describe("AuditPage", () => {
7698
const query = "resource_type:workspace action:create"
7799
await renderPage({ filter: query })
78100

79-
expect(getAuditLogsSpy).toBeCalledWith({ limit: 25, offset: 1, q: query })
101+
expect(getAuditLogsSpy).toBeCalledWith({
102+
limit: DEFAULT_RECORDS_PER_PAGE,
103+
offset: 0,
104+
q: query,
105+
})
80106
})
81107

82108
it("resets page to 1 when filter is changed", async () => {
@@ -91,8 +117,8 @@ describe("AuditPage", () => {
91117

92118
await waitFor(() =>
93119
expect(getAuditLogsSpy).toBeCalledWith({
94-
limit: 25,
95-
offset: 1,
120+
limit: DEFAULT_RECORDS_PER_PAGE,
121+
offset: 0,
96122
q: query,
97123
}),
98124
)

site/src/pages/AuditPage/AuditPage.tsx

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import { nonInitialPage } from "components/PaginationWidget/utils"
1+
import {
2+
DEFAULT_RECORDS_PER_PAGE,
3+
nonInitialPage,
4+
} from "components/PaginationWidget/utils"
25
import { useFeatureVisibility } from "hooks/useFeatureVisibility"
36
import { FC } from "react"
47
import { Helmet } from "react-helmet-async"
@@ -49,9 +52,11 @@ const AuditPage: FC = () => {
4952
const { data, error } = useQuery({
5053
queryKey: ["auditLogs", filter.query, pagination.page],
5154
queryFn: () => {
55+
const limit = DEFAULT_RECORDS_PER_PAGE
56+
const page = pagination.page
5257
return getAuditLogs({
53-
offset: pagination.page,
54-
limit: 25,
58+
offset: page <= 0 ? 0 : (page - 1) * limit,
59+
limit: limit,
5560
q: filter.query,
5661
})
5762
},

0 commit comments

Comments
 (0)