Skip to content

Commit

Permalink
feat(gitlab): add support for paginated project list (reanahub#403)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: The response of `gitlab_projects` API endpoint has been
modified to also include the pagination details.

Closes reanahub/reana-server#518
  • Loading branch information
mdonadoni committed Mar 20, 2024
1 parent 9113bfc commit 06e15f8
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 8 deletions.
6 changes: 4 additions & 2 deletions reana-ui/src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,10 @@ class Client {
});
}

getGitlabProjects({ search } = {}) {
return this._request(GITLAB_PROJECTS_URL({ search }));
getGitlabProjects({ search, pagination } = {}) {
return this._request(
GITLAB_PROJECTS_URL({ ...(pagination ?? {}), search }),
);
}

toggleGitlabProject(method, data) {
Expand Down
43 changes: 37 additions & 6 deletions reana-ui/src/pages/profile/components/GitLabProjects.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,18 @@ import { useEffect, useState, useRef } from "react";
import { Button, List, Loader, Radio, Message, Icon } from "semantic-ui-react";

import client, { GITLAB_AUTH_URL } from "~/client";
import { Search } from "~/components";
import { Search, Pagination } from "~/components";

import styles from "./GitLabProjects.module.scss";

export default function GitLabProjects() {
const DEFAULT_PAGINATION = { page: 1, size: 10 };

const [projects, setProjects] = useState(null);
const [fetchingProjects, setFetchingProjects] = useState(false);
const [searchFilter, setSearchFilter] = useState(null);
const [pagination, setPagination] = useState(DEFAULT_PAGINATION);
const [totalPages, setTotalPages] = useState(0);

// keep track of last fetch request in order to avoid
// updating the state with out-of-order responses
Expand All @@ -29,7 +33,10 @@ export default function GitLabProjects() {
useEffect(() => {
// Fetch project list
setFetchingProjects(true);
let request = client.getGitlabProjects({ search: searchFilter });
let request = client.getGitlabProjects({
pagination,
search: searchFilter,
});
lastFetchRequest.current = request;

request
Expand All @@ -39,10 +46,17 @@ export default function GitLabProjects() {
return;
}
let newProjects = {};
for (const [id, details] of Object.entries(res.data)) {
newProjects[id] = { ...details, toggling: false };
for (const project of res.data.items) {
newProjects[project.id] = { ...project, toggling: false };
}
setProjects(newProjects);
const newTotalPages =
res.data.total != null
? Math.ceil(res.data.total / pagination.size)
: res.data.has_next
? pagination.page + 1
: pagination.page;
setTotalPages(newTotalPages);
setFetchingProjects(false);
})
.catch((e) => {
Expand All @@ -51,9 +65,10 @@ export default function GitLabProjects() {
return;
}
setProjects(null);
setTotalPages(0);
setFetchingProjects(false);
});
}, [searchFilter]);
}, [searchFilter, pagination]);

const setToggling = (projectId, toggling) => {
setProjects((currentProjects) => ({
Expand Down Expand Up @@ -101,6 +116,12 @@ export default function GitLabProjects() {
});
};

const onSearchFilterChange = (value) => {
// reset pagination if search filter changes
setPagination(DEFAULT_PAGINATION);
setSearchFilter(value);
};

if (fetchingProjects && projects === null) {
// projects were never fetched before, show spinner
return (
Expand Down Expand Up @@ -136,7 +157,7 @@ export default function GitLabProjects() {
} else {
return (
<>
<Search search={setSearchFilter} loading={fetchingProjects} />
<Search search={onSearchFilterChange} loading={fetchingProjects} />
{!isEmpty(projects) ? (
<>
<List>
Expand Down Expand Up @@ -167,6 +188,16 @@ export default function GitLabProjects() {
},
)}
</List>
<div className={styles.pagination}>
<Pagination
activePage={pagination.page}
totalPages={totalPages}
onPageChange={(_, { activePage }) => {
setPagination({ ...pagination, page: activePage });
}}
disabled={fetchingProjects}
/>
</div>
</>
) : (
<Message info icon>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,8 @@
display: flex;
align-items: center;
}

.pagination {
width: fit-content;
margin: auto;
}

0 comments on commit 06e15f8

Please sign in to comment.