Skip to content

Commit 7567bdb

Browse files
author
Laurie T. Malau
committed
increase page resultsand add clclickability
1 parent 5cd44fd commit 7567bdb

File tree

5 files changed

+138
-54
lines changed

5 files changed

+138
-54
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/**
2+
* Copyright (c) 2022 Gitpod GmbH. All rights reserved.
3+
* Licensed under the GNU Affero General Public License (AGPL).
4+
* See License-AGPL.txt in the project root for license information.
5+
*/
6+
7+
import { getPaginationNumbers } from "./getPagination";
8+
import Arrow from "../components/Arrow";
9+
import React from "react";
10+
11+
interface PaginationProps {
12+
totalResults: number;
13+
totalNumberOfPages: number;
14+
currentPage: number;
15+
setCurrentPage: React.Dispatch<React.SetStateAction<number>>;
16+
}
17+
18+
function Pagination({ totalNumberOfPages, currentPage, setCurrentPage }: PaginationProps) {
19+
const calculatedPagination = getPaginationNumbers(totalNumberOfPages, currentPage);
20+
21+
const nextPage = () => {
22+
if (currentPage !== totalNumberOfPages) setCurrentPage(currentPage + 1);
23+
};
24+
const prevPage = () => {
25+
if (currentPage !== 1) setCurrentPage(currentPage - 1);
26+
};
27+
const getClassnames = (pageNumber: string | number) => {
28+
if (pageNumber === currentPage) {
29+
return "text-gray-500 w-8 text-center rounded-md hover:bg-gray-50 bg-gray-100 disabled pointer-events-none";
30+
}
31+
if (pageNumber === "...") {
32+
return "text-gray-500 w-8 text-center rounded-md hover:bg-gray-50 disabled pointer-events-none";
33+
}
34+
return "text-gray-500 w-8 text-center rounded-md hover:bg-gray-50 cursor-pointer";
35+
};
36+
37+
return (
38+
<nav className="mt-16 mb-16">
39+
<ul className="flex justify-center space-x-4">
40+
<li className={`text-gray-400 ${currentPage === 1 ? "disabled" : "cursor-pointer text-gray-500"}`}>
41+
<span onClick={prevPage}>
42+
<Arrow direction={"left"} />
43+
Previous
44+
</span>
45+
</li>
46+
{calculatedPagination.map((pn, i) => (
47+
<li key={i} className={getClassnames(pn)}>
48+
<span onClick={() => setCurrentPage(pn)}>{pn}</span>
49+
</li>
50+
))}
51+
<li
52+
className={`text-gray-400 ${
53+
currentPage === totalNumberOfPages ? "disabled" : "cursor-pointer text-gray-500"
54+
}`}
55+
>
56+
<span onClick={nextPage}>
57+
Next
58+
<Arrow direction={"right"} />
59+
</span>
60+
</li>
61+
</ul>
62+
</nav>
63+
);
64+
}
65+
66+
export default Pagination;
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* Copyright (c) 2022 Gitpod GmbH. All rights reserved.
3+
* Licensed under the GNU Affero General Public License (AGPL).
4+
* See License-AGPL.txt in the project root for license information.
5+
*/
6+
7+
import { getPaginationNumbers } from "./getPagination";
8+
9+
test("getPagination", () => {
10+
const totalNumberOfPages = 15;
11+
const currentPage = 1;
12+
13+
expect(getPaginationNumbers(totalNumberOfPages, currentPage)).toStrictEqual([1, 2, 3, "...", 15]);
14+
15+
expect(getPaginationNumbers(37, 4)).toStrictEqual([1, "...", 3, 4, 5, "...", 37]);
16+
17+
expect(getPaginationNumbers(28, 7)).toStrictEqual([1, "...", 6, 7, 8, "...", 28]);
18+
19+
expect(getPaginationNumbers(5, 1)).toStrictEqual([1, 2, 3, 4, 5]);
20+
});
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* Copyright (c) 2022 Gitpod GmbH. All rights reserved.
3+
* Licensed under the GNU Affero General Public License (AGPL).
4+
* See License-AGPL.txt in the project root for license information.
5+
*/
6+
7+
export function getPaginationNumbers(totalNumberOfPages: number, currentPage: number) {
8+
const adjacentToCurrentPage = 1; // This is the number(s) we see next to the currentPage
9+
const totalNumbersShownInPagination = 6;
10+
let calculatedPagination: number[] = [];
11+
12+
const pageNumbersAsArray = (startRange: number, endRange: number) => {
13+
return [...Array(endRange + 1).keys()].slice(startRange);
14+
};
15+
16+
const minimumAmountInBetweenToShowEllipsis = 2;
17+
// Without ellipsis aka normal case
18+
if (totalNumberOfPages <= totalNumbersShownInPagination) {
19+
return (calculatedPagination = pageNumbersAsArray(1, totalNumberOfPages));
20+
}
21+
22+
// Otherwise, we show the ellipses
23+
const toTheRightOfCurrent = Math.min(currentPage + adjacentToCurrentPage, totalNumberOfPages);
24+
const toTheLeftOfCurrent = Math.max(currentPage - adjacentToCurrentPage, 1);
25+
26+
const showRightEllipsis = toTheRightOfCurrent < totalNumberOfPages - minimumAmountInBetweenToShowEllipsis; // e.g. "1 2 3 ... 7"
27+
const showLeftEllipsis = toTheLeftOfCurrent > minimumAmountInBetweenToShowEllipsis; // e.g. 1 ... 5 6 7"
28+
29+
if (showRightEllipsis && !showLeftEllipsis) {
30+
let leftSideNumbers = 3;
31+
let leftPageNumbersAsArray = pageNumbersAsArray(1, leftSideNumbers);
32+
return [...leftPageNumbersAsArray, "...", totalNumberOfPages];
33+
}
34+
35+
if (showLeftEllipsis && !showRightEllipsis) {
36+
let rightSideNumbers = 3;
37+
let rightPageNumbersAsArray = pageNumbersAsArray(totalNumberOfPages - rightSideNumbers, totalNumberOfPages);
38+
return [1, "...", ...rightPageNumbersAsArray];
39+
}
40+
41+
if (showRightEllipsis && showLeftEllipsis) {
42+
let middleNumbers = pageNumbersAsArray(toTheLeftOfCurrent, toTheRightOfCurrent);
43+
return [1, "...", ...middleNumbers, "...", totalNumberOfPages];
44+
}
45+
46+
return calculatedPagination;
47+
}

components/dashboard/src/components/Pagination.tsx

Lines changed: 0 additions & 50 deletions
This file was deleted.

components/dashboard/src/teams/TeamUsage.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import {
1717
import { AttributionId } from "@gitpod/gitpod-protocol/lib/attribution";
1818
import { Item, ItemField, ItemsList } from "../components/ItemsList";
1919
import moment from "moment";
20-
import Pagination from "../components/Pagination";
20+
import Pagination from "../Pagination/Pagination";
2121
import Header from "../components/Header";
2222
import { ErrorCodes } from "@gitpod/gitpod-protocol/lib/messaging/error";
2323
import { FeatureFlagContext } from "../contexts/FeatureFlagContext";
@@ -32,7 +32,7 @@ function TeamUsage() {
3232
const team = getCurrentTeam(location, teams);
3333
const [billedUsage, setBilledUsage] = useState<BillableSession[]>([]);
3434
const [currentPage, setCurrentPage] = useState(1);
35-
const [resultsPerPage] = useState(10);
35+
const [resultsPerPage] = useState(15);
3636
const [errorMessage, setErrorMessage] = useState("");
3737
const today = new Date();
3838
const startOfCurrentMonth = new Date(today.getFullYear(), today.getMonth(), 1);
@@ -126,7 +126,7 @@ function TeamUsage() {
126126

127127
const lastResultOnCurrentPage = currentPage * resultsPerPage;
128128
const firstResultOnCurrentPage = lastResultOnCurrentPage - resultsPerPage;
129-
const numberOfPages = Math.ceil(billedUsage.length / resultsPerPage);
129+
const totalNumberOfPages = Math.ceil(billedUsage.length / resultsPerPage);
130130
const currentPaginatedResults = billedUsage.slice(firstResultOnCurrentPage, lastResultOnCurrentPage);
131131

132132
return (
@@ -236,9 +236,10 @@ function TeamUsage() {
236236
</ItemsList>
237237
{billedUsage.length > resultsPerPage && (
238238
<Pagination
239+
totalResults={billedUsage.length}
239240
currentPage={currentPage}
240241
setCurrentPage={setCurrentPage}
241-
numberOfPages={numberOfPages}
242+
totalNumberOfPages={totalNumberOfPages}
242243
/>
243244
)}
244245
</div>

0 commit comments

Comments
 (0)