Skip to content

Commit 01bb7a1

Browse files
author
Laurie T. Malau
committed
pagination
1 parent 931b8eb commit 01bb7a1

File tree

3 files changed

+157
-1
lines changed

3 files changed

+157
-1
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
function Pagination(props: { numberOfPages: number; currentPage: number; setCurrentPage: any }) {
8+
const { numberOfPages, currentPage, setCurrentPage } = props;
9+
const availablePageNumbers = [...Array(numberOfPages + 1).keys()].slice(1);
10+
11+
const nextPage = () => {
12+
if (currentPage !== numberOfPages) setCurrentPage(currentPage + 1);
13+
};
14+
const prevPage = () => {
15+
if (currentPage !== 1) setCurrentPage(currentPage - 1);
16+
};
17+
18+
return (
19+
<nav>
20+
<ul className="flex justify-center space-x-5">
21+
<li className="text-gray-400">
22+
<span onClick={prevPage}>
23+
Previous
24+
</span>
25+
</li>
26+
{availablePageNumbers.map((pn) => (
27+
<li key={pn} className={`text-gray-500 ${currentPage === pn ? "bg-gray-200" : ""} `}>
28+
<span onClick={() => setCurrentPage(pn)}>
29+
{pn}
30+
</span>
31+
</li>
32+
))}
33+
<li className="text-gray-400">
34+
<span onClick={nextPage}>
35+
Next
36+
</span>
37+
</li>
38+
</ul>
39+
</nav>
40+
);
41+
}
42+
43+
export default Pagination;

components/dashboard/src/teams/TeamUsage.tsx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,16 @@ import { Item, ItemField, ItemsList } from "../components/ItemsList";
1717
import moment from "moment";
1818
import Property from "../admin/Property";
1919
import Arrow from "../components/Arrow";
20+
import Pagination from "../components/Pagination";
2021

2122
function TeamUsage() {
2223
const { teams } = useContext(TeamsContext);
2324
const { showPaymentUI, showUsageBasedUI } = useContext(PaymentContext);
2425
const location = useLocation();
2526
const team = getCurrentTeam(location, teams);
2627
const [billedUsage, setBilledUsage] = useState<BillableSession[]>([]);
28+
const [currentPage, setCurrentPage] = useState(1);
29+
const [resultsPerPage] = useState(10);
2730

2831
useEffect(() => {
2932
if (!team) {
@@ -51,6 +54,11 @@ function TeamUsage() {
5154
return (endTime - startTime) / (1000 * 60 * 60) + "hrs";
5255
};
5356

57+
const lastResultOnCurrentPage = currentPage * resultsPerPage;
58+
const firstResultOnCurrentPage = lastResultOnCurrentPage - resultsPerPage;
59+
const numberOfPages = Math.ceil(billedUsage.length / resultsPerPage);
60+
const currentPaginatedResults = billedUsage.slice(firstResultOnCurrentPage, lastResultOnCurrentPage);
61+
5462
return (
5563
<PageWithSubMenu
5664
subMenu={getTeamSettingsMenu({ team, showPaymentUI, showUsageBasedUI })}
@@ -80,7 +88,7 @@ function TeamUsage() {
8088
</ItemField>
8189
<ItemField className="my-auto" />
8290
</Item>
83-
{billedUsage.map((usage) => (
91+
{currentPaginatedResults.map((usage) => (
8492
<div
8593
key={usage.instanceId}
8694
className="flex p-3 grid grid-cols-6 justify-between transition ease-in-out rounded-xl focus:bg-gitpod-kumquat-light"
@@ -112,6 +120,7 @@ function TeamUsage() {
112120
</div>
113121
))}
114122
</ItemsList>
123+
{(billedUsage.length > resultsPerPage) && <Pagination currentPage={currentPage} setCurrentPage={setCurrentPage} numberOfPages={numberOfPages} />}
115124
</PageWithSubMenu>
116125
);
117126
}

components/gitpod-protocol/src/usage.ts

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,4 +143,108 @@ export const billableSessionDummyData: BillableSession[] = [
143143
credits: 200,
144144
projectId: "project-345",
145145
},
146+
{
147+
attributionId: "some-attribution-id",
148+
userId: "prebuild",
149+
teamId: "prebuild",
150+
instanceId: "some-instance-id",
151+
workspaceId: "some-workspace-id",
152+
workspaceType: "prebuild",
153+
workspaceClass: "XL",
154+
startTime: new Date(Date.now() + -3 * 24 * 3600 * 1000).toISOString(), // 3 days ago
155+
endTime: new Date().toISOString(),
156+
credits: 320,
157+
projectId: "project-123",
158+
},
159+
{
160+
attributionId: "some-attribution-id2",
161+
userId: "some-user",
162+
teamId: "some-team",
163+
instanceId: "some-instance-id2",
164+
workspaceId: "some-workspace-id2",
165+
workspaceType: "regular",
166+
workspaceClass: "standard",
167+
startTime: new Date(Date.now() + -5 * 24 * 3600 * 1000).toISOString(),
168+
endTime: new Date().toISOString(),
169+
credits: 130,
170+
projectId: "project-123",
171+
},
172+
{
173+
attributionId: "some-attribution-id3",
174+
userId: "some-other-user",
175+
teamId: "some-other-team",
176+
instanceId: "some-instance-id3",
177+
workspaceId: "some-workspace-id3",
178+
workspaceType: "regular",
179+
workspaceClass: "XL",
180+
startTime: new Date(Date.now() + -5 * 24 * 3600 * 1000).toISOString(),
181+
endTime: new Date(Date.now() + -4 * 24 * 3600 * 1000).toISOString(),
182+
credits: 150,
183+
projectId: "project-134",
184+
},
185+
{
186+
attributionId: "some-attribution-id4",
187+
userId: "some-other-user2",
188+
teamId: "some-other-team2",
189+
instanceId: "some-instance-id4",
190+
workspaceId: "some-workspace-id4",
191+
workspaceType: "regular",
192+
workspaceClass: "standard",
193+
startTime: new Date(Date.now() + -10 * 24 * 3600 * 1000).toISOString(),
194+
endTime: new Date(Date.now() + -9 * 24 * 3600 * 1000).toISOString(),
195+
credits: 330,
196+
projectId: "project-137",
197+
},
198+
{
199+
attributionId: "some-attribution-id5",
200+
userId: "some-other-user3",
201+
teamId: "some-other-team3",
202+
instanceId: "some-instance-id5",
203+
workspaceId: "some-workspace-id5",
204+
workspaceType: "regular",
205+
workspaceClass: "XL",
206+
startTime: new Date(Date.now() + -2 * 24 * 3600 * 1000).toISOString(),
207+
endTime: new Date().toISOString(),
208+
credits: 222,
209+
projectId: "project-138",
210+
},
211+
{
212+
attributionId: "some-attribution-id6",
213+
userId: "some-other-user4",
214+
teamId: "some-other-team4",
215+
instanceId: "some-instance-id6",
216+
workspaceId: "some-workspace-id3",
217+
workspaceType: "regular",
218+
workspaceClass: "XL",
219+
startTime: new Date(Date.now() + -7 * 24 * 3600 * 1000).toISOString(),
220+
endTime: new Date(Date.now() + -6 * 24 * 3600 * 1000).toISOString(),
221+
credits: 300,
222+
projectId: "project-134",
223+
},
224+
{
225+
attributionId: "some-attribution-id8",
226+
userId: "some-other-user5",
227+
teamId: "some-other-team5",
228+
instanceId: "some-instance-id8",
229+
workspaceId: "some-workspace-id3",
230+
workspaceType: "regular",
231+
workspaceClass: "standard",
232+
startTime: new Date(Date.now() + -1 * 24 * 3600 * 1000).toISOString(),
233+
endTime: new Date().toISOString(),
234+
credits: 100,
235+
projectId: "project-567",
236+
},
237+
{
238+
attributionId: "some-attribution-id7",
239+
userId: "prebuild",
240+
teamId: "some-other-team7",
241+
instanceId: "some-instance-id7",
242+
workspaceId: "some-workspace-id7",
243+
workspaceType: "prebuild",
244+
workspaceClass: "XL",
245+
startTime: new Date(Date.now() + -1 * 24 * 3600 * 1000).toISOString(),
246+
endTime: new Date().toISOString(),
247+
credits: 200,
248+
projectId: "project-345",
249+
}
146250
];

0 commit comments

Comments
 (0)