Skip to content

[dashboard] add pagination to admin search #13161

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 22, 2022
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
7 changes: 6 additions & 1 deletion components/dashboard/src/Pagination/Pagination.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@ interface PaginationProps {
setPage: (page: number) => void;
}

function Pagination({ totalNumberOfPages, currentPage, setPage }: PaginationProps) {
function Pagination(props: PaginationProps) {
const { totalNumberOfPages, setPage } = props;
if (totalNumberOfPages <= 1 || props.currentPage < 1) {
return <></>;
}
const currentPage = Math.min(totalNumberOfPages, props.currentPage);
const calculatedPagination = getPaginationNumbers(totalNumberOfPages, currentPage);

const nextPage = () => {
Expand Down
15 changes: 13 additions & 2 deletions components/dashboard/src/admin/ProjectsSearch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import ProjectDetail from "./ProjectDetail";
import { getGitpodService } from "../service/service";
import { AdminGetListResult, Project } from "@gitpod/gitpod-protocol";
import { PageWithAdminSubMenu } from "./PageWithAdminSubMenu";
import Pagination from "../Pagination/Pagination";

export default function ProjectsSearchPage() {
return (
Expand All @@ -29,6 +30,11 @@ export function ProjectsSearch() {
const [searchResult, setSearchResult] = useState<AdminGetListResult<Project>>({ total: 0, rows: [] });
const [currentProject, setCurrentProject] = useState<Project | undefined>(undefined);
const [currentProjectOwner, setCurrentProjectOwner] = useState<string | undefined>("");
const pageLength = 50;
const [currentPage, setCurrentPage] = useState(1);
useEffect(() => {
search();
}, [currentPage]);

useEffect(() => {
const projectId = location.pathname.split("/")[3];
Expand Down Expand Up @@ -75,9 +81,9 @@ export function ProjectsSearch() {
try {
const result = await getGitpodService().server.adminGetProjectsBySearchTerm({
searchTerm,
limit: 50,
limit: pageLength,
orderBy: "creationTime",
offset: 0,
offset: (currentPage - 1) * pageLength,
orderDir: "desc",
});
setSearchResult(result);
Expand Down Expand Up @@ -131,6 +137,11 @@ export function ProjectsSearch() {
<ProjectResultItem project={project} />
))}
</div>
<Pagination
currentPage={currentPage}
setPage={setCurrentPage}
totalNumberOfPages={Math.ceil(searchResult.total / pageLength)}
/>
</>
);

Expand Down
15 changes: 13 additions & 2 deletions components/dashboard/src/admin/TeamsSearch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { getGitpodService } from "../service/service";
import { AdminGetListResult, Team } from "@gitpod/gitpod-protocol";
import Label from "./Label";
import { PageWithAdminSubMenu } from "./PageWithAdminSubMenu";
import Pagination from "../Pagination/Pagination";

export default function TeamsSearchPage() {
return (
Expand All @@ -29,6 +30,11 @@ export function TeamsSearch() {
const [searchTerm, setSearchTerm] = useState("");
const [currentTeam, setCurrentTeam] = useState<Team | undefined>(undefined);
const [searchResult, setSearchResult] = useState<AdminGetListResult<Team>>({ total: 0, rows: [] });
const pageLength = 50;
const [currentPage, setCurrentPage] = useState(1);
useEffect(() => {
search();
}, [currentPage]);

useEffect(() => {
const teamId = location.pathname.split("/")[3];
Expand Down Expand Up @@ -56,9 +62,9 @@ export function TeamsSearch() {
try {
const result = await getGitpodService().server.adminGetTeams({
searchTerm,
limit: 100,
limit: pageLength,
orderBy: "creationTime",
offset: 0,
offset: (currentPage - 1) * pageLength,
orderDir: "desc",
});
setSearchResult(result);
Expand Down Expand Up @@ -120,6 +126,11 @@ export function TeamsSearch() {
<TeamResultItem team={team} />
))}
</div>
<Pagination
currentPage={currentPage}
setPage={setCurrentPage}
totalNumberOfPages={Math.ceil(searchResult.total / pageLength)}
/>
</>
);

Expand Down
15 changes: 13 additions & 2 deletions components/dashboard/src/admin/UserSearch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import moment from "moment";
import { useEffect, useState } from "react";
import { useLocation } from "react-router";
import { Link } from "react-router-dom";
import Pagination from "../Pagination/Pagination";
import { getGitpodService } from "../service/service";
import { PageWithAdminSubMenu } from "./PageWithAdminSubMenu";
import UserDetail from "./UserDetail";
Expand All @@ -19,6 +20,11 @@ export default function UserSearch() {
const [searchTerm, setSearchTerm] = useState("");
const [searching, setSearching] = useState(false);
const [currentUser, setCurrentUserState] = useState<User | undefined>(undefined);
const pageLength = 50;
const [currentPage, setCurrentPage] = useState(1);
useEffect(() => {
search();
}, [currentPage]);

useEffect(() => {
const userId = location.pathname.split("/")[3];
Expand Down Expand Up @@ -46,9 +52,9 @@ export default function UserSearch() {
try {
const result = await getGitpodService().server.adminGetUsers({
searchTerm,
limit: 50,
limit: pageLength,
orderBy: "creationDate",
offset: 0,
offset: (currentPage - 1) * pageLength,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

currentPage starts at value of 0, wouldn't this cause the offset to be negative?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

currentPage should start at 1.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now it does, but before it was definitely initialized to 0.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes on 3 out of 4 occasions 😬 (the four search pages are a bit copy and paste and of course I only tested on one of them, which also explains why the limit was set to 2 only on one of them)

orderDir: "desc",
});
setSearchResult(result);
Expand Down Expand Up @@ -103,6 +109,11 @@ export default function UserSearch() {
<UserEntry user={u} />
))}
</div>
<Pagination
currentPage={currentPage}
setPage={setCurrentPage}
totalNumberOfPages={Math.ceil(searchResult.total / pageLength)}
/>
</PageWithAdminSubMenu>
);
}
Expand Down
23 changes: 13 additions & 10 deletions components/dashboard/src/admin/WorkspacesSearch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import moment from "moment";
import { useEffect, useState } from "react";
import { useLocation } from "react-router";
import { Link } from "react-router-dom";
import Pagination from "../Pagination/Pagination";
import { getGitpodService } from "../service/service";
import { getProject, WorkspaceStatusIndicator } from "../workspaces/WorkspaceEntry";
import WorkspaceDetail from "./WorkspaceDetail";
Expand All @@ -43,6 +44,11 @@ export function WorkspaceSearch(props: Props) {
const [queryTerm, setQueryTerm] = useState("");
const [searching, setSearching] = useState(false);
const [currentWorkspace, setCurrentWorkspaceState] = useState<WorkspaceAndInstance | undefined>(undefined);
const pageLength = 50;
const [currentPage, setCurrentPage] = useState(1);
useEffect(() => {
search();
}, [currentPage]);

useEffect(() => {
const workspaceId = location.pathname.split("/")[3];
Expand Down Expand Up @@ -72,11 +78,6 @@ export function WorkspaceSearch(props: Props) {
}

const search = async () => {
// Disables empty search on the workspace search page
if (!props.user && queryTerm.length === 0) {
return;
}

setSearching(true);
try {
const query: AdminGetWorkspacesQuery = {
Expand All @@ -87,14 +88,11 @@ export function WorkspaceSearch(props: Props) {
} else if (matchesNewWorkspaceIdExactly(queryTerm)) {
query.workspaceId = queryTerm;
}
if (!query.ownerId && !query.instanceIdOrWorkspaceId && !query.workspaceId) {
return;
}

const result = await getGitpodService().server.adminGetWorkspaces({
limit: 100,
limit: pageLength,
orderBy: "instanceCreationTime",
offset: 0,
offset: (currentPage - 1) * pageLength,
orderDir: "desc",
...query,
});
Expand Down Expand Up @@ -152,6 +150,11 @@ export function WorkspaceSearch(props: Props) {
<WorkspaceEntry ws={ws} />
))}
</div>
<Pagination
currentPage={currentPage}
setPage={setCurrentPage}
totalNumberOfPages={Math.ceil(searchResult.total / pageLength)}
/>
</>
);
}
Expand Down