Skip to content

Commit f011ba7

Browse files
authored
Switch to client instead of server side pagination (#144)
Visual-Regression-Tracker/Visual-Regression-Tracker#213
1 parent 77f91e2 commit f011ba7

File tree

4 files changed

+28
-55
lines changed

4 files changed

+28
-55
lines changed

src/_test/stub.helper.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ import { buildsService, projectsService, testRunService } from "../services";
33
import { Build, Project, TestRun } from "../types";
44

55
const projectStub = {
6-
getAll: (projects: Project[]) =>
6+
getAll: (projects: Array<Project>) =>
77
cy.stub(projectsService, "getAll").resolves(projects),
88
};
99

1010
const buildsServiceStub = {
1111
getDetails: (build: Build) =>
1212
cy.stub(buildsService, "getDetails").resolves(build),
13-
getList: (builds: Build[]) =>
13+
getList: (builds: Array<Build>) =>
1414
cy.stub(buildsService, "getList").resolves({
1515
data: builds,
1616
total: 100,
@@ -20,10 +20,8 @@ const buildsServiceStub = {
2020
};
2121

2222
const testRunServiceStub = {
23-
getList: (testRuns: TestRun[]) =>
24-
cy
25-
.stub(testRunService, "getList")
26-
.resolves({ data: testRuns, total: 100, skip: 0, take: 10 }),
23+
getList: (testRuns: Array<TestRun>) =>
24+
cy.stub(testRunService, "getList").resolves(testRuns),
2725
};
2826

2927
export { projectStub, buildsServiceStub, testRunServiceStub };

src/components/TestRunList/index.tsx

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import {
1515
ColDef,
1616
RowParams,
1717
CellParams,
18-
PageChangeParams,
1918
} from "@material-ui/data-grid";
2019
import { DataGridCustomToolbar } from "./DataGridCustomToolbar";
2120

@@ -53,36 +52,34 @@ const columns: ColDef[] = [
5352

5453
const TestRunList: React.FunctionComponent = () => {
5554
const { enqueueSnackbar } = useSnackbar();
56-
const { testRuns, loading, total, take } = useTestRunState();
55+
const { testRuns, loading } = useTestRunState();
5756
const { selectedBuildId } = useBuildState();
5857
const testRunDispatch = useTestRunDispatch();
5958
const history = useHistory();
6059

6160
const getTestRunListCalback = React.useCallback(
62-
(page: number) =>
61+
() =>
6362
selectedBuildId &&
64-
getTestRunList(testRunDispatch, selectedBuildId, page).catch(
65-
(err: string) =>
66-
enqueueSnackbar(err, {
67-
variant: "error",
68-
})
63+
getTestRunList(testRunDispatch, selectedBuildId).catch((err: string) =>
64+
enqueueSnackbar(err, {
65+
variant: "error",
66+
})
6967
),
7068
[testRunDispatch, enqueueSnackbar, selectedBuildId]
7169
);
7270

7371
React.useEffect(() => {
74-
getTestRunListCalback(1);
72+
getTestRunListCalback();
7573
}, [getTestRunListCalback]);
7674

7775
return (
7876
<React.Fragment>
7977
<DataGrid
8078
rows={testRuns}
8179
columns={columns}
82-
pageSize={take}
83-
rowCount={total}
80+
pageSize={10}
81+
rowsPerPageOptions={[10, 20, 30]}
8482
loading={loading}
85-
paginationMode={"server"}
8683
showToolbar={true}
8784
components={{
8885
Toolbar: DataGridCustomToolbar,
@@ -99,9 +96,6 @@ const TestRunList: React.FunctionComponent = () => {
9996
)
10097
);
10198
}}
102-
onPageChange={(param: PageChangeParams) =>
103-
getTestRunListCalback(param.page)
104-
}
10599
/>
106100
</React.Fragment>
107101
);

src/contexts/testRun.context.tsx

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from "react";
2-
import { PaginatedData, TestRun } from "../types";
2+
import { TestRun } from "../types";
33
import { testRunService } from "../services";
44

55
interface IRequestAction {
@@ -9,17 +9,17 @@ interface IRequestAction {
99

1010
interface IGetAction {
1111
type: "get";
12-
payload: PaginatedData<TestRun>;
12+
payload: Array<TestRun>;
1313
}
1414

1515
interface ISelectAction {
1616
type: "select";
17-
payload: string | undefined;
17+
payload?: string;
1818
}
1919

2020
interface IIndexAction {
2121
type: "index";
22-
payload: string | undefined;
22+
payload?: string;
2323
}
2424

2525
interface IDeleteAction {
@@ -60,12 +60,9 @@ type IAction =
6060

6161
type Dispatch = (action: IAction) => void;
6262
type State = {
63-
selectedTestRunId: string | undefined;
64-
selectedTestRunIndex: number | undefined;
65-
testRuns: TestRun[];
66-
total: number;
67-
take: number;
68-
skip: number;
63+
selectedTestRunId?: string;
64+
selectedTestRunIndex?: number;
65+
testRuns: Array<TestRun>;
6966
loading: boolean;
7067
};
7168

@@ -77,12 +74,7 @@ const TestRunDispatchContext = React.createContext<Dispatch | undefined>(
7774
);
7875

7976
const initialState: State = {
80-
selectedTestRunId: undefined,
81-
selectedTestRunIndex: undefined,
8277
testRuns: [],
83-
take: 10,
84-
skip: 0,
85-
total: 0,
8678
loading: false,
8779
};
8880

@@ -107,13 +99,9 @@ function testRunReducer(state: State, action: IAction): State {
10799
loading: true,
108100
};
109101
case "get":
110-
const { data, take, skip, total } = action.payload;
111102
return {
112103
...state,
113-
testRuns: data,
114-
take,
115-
skip,
116-
total,
104+
testRuns: action.payload,
117105
loading: false,
118106
};
119107
case "delete":
@@ -175,16 +163,13 @@ function useTestRunDispatch() {
175163

176164
async function getTestRunList(
177165
dispatch: Dispatch,
178-
buildId: string,
179-
page: number
166+
buildId: string
180167
): Promise<void> {
181168
dispatch({ type: "request" });
182169

183-
return testRunService
184-
.getList(buildId, initialState.take, initialState.take * (page - 1))
185-
.then((response) => {
186-
dispatch({ type: "get", payload: response });
187-
});
170+
return testRunService.getList(buildId).then((response) => {
171+
dispatch({ type: "get", payload: response });
172+
});
188173
}
189174

190175
async function deleteTestRun(dispatch: Dispatch, id: string) {

src/services/testRun.service.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,18 @@
1-
import { PaginatedData, TestRun } from "../types";
1+
import { TestRun } from "../types";
22
import { handleResponse, authHeader } from "../_helpers/service.helpers";
33
import { API_URL } from "../_config/env.config";
44
import { IgnoreArea } from "../types/ignoreArea";
55

66
const ENDPOINT_URL = "/test-runs";
77

8-
async function getList(
9-
buildId: string,
10-
take: number,
11-
skip: number
12-
): Promise<PaginatedData<TestRun>> {
8+
async function getList(buildId: string): Promise<TestRun[]> {
139
const requestOptions = {
1410
method: "GET",
1511
headers: authHeader(),
1612
};
1713

1814
return fetch(
19-
`${API_URL}${ENDPOINT_URL}?buildId=${buildId}&take=${take}&skip=${skip}`,
15+
`${API_URL}${ENDPOINT_URL}?buildId=${buildId}`,
2016
requestOptions
2117
).then(handleResponse);
2218
}

0 commit comments

Comments
 (0)