Skip to content

Commit

Permalink
fixed count for pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
edewit committed Oct 17, 2023
1 parent e803cd8 commit 895f9c8
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 27 deletions.
27 changes: 10 additions & 17 deletions js/apps/admin-ui/src/components/group/GroupPickerDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
ModalVariant,
} from "@patternfly/react-core";
import { AngleRightIcon } from "@patternfly/react-icons";
import { useState } from "react";
import { Fragment, useState } from "react";
import { useTranslation } from "react-i18next";
import { adminClient } from "../../admin-client";
import { fetchAdminUI } from "../../context/auth/admin-ui-endpoint";
Expand All @@ -24,6 +24,7 @@ import { PaginatingTableToolbar } from "../table-toolbar/PaginatingTableToolbar"
import { GroupPath } from "./GroupPath";

import "./group-picker-dialog.css";
import { countGroups } from "../../groups/components/GroupTree";

export type GroupPickerDialogProps = {
id?: string;
Expand Down Expand Up @@ -72,16 +73,16 @@ export const GroupPickerDialog = ({
let group;
let groups;
let existingUserGroups;
let count = 0;
if (!groupId) {
groups = await fetchAdminUI<GroupRepresentation[]>(
"ui-ext/groups",
Object.assign(
{
first: `${first}`,
max: `${max + 1}`,
global: "false",
},
isSearching ? { search: filter } : null,
isSearching ? { search: filter, global: "true" } : null,
),
);
} else if (!navigation.map(({ id }) => id).includes(groupId)) {
Expand All @@ -92,20 +93,15 @@ export const GroupPickerDialog = ({
groups = group.subGroups!;
}

if (isSearching) {
count = (await adminClient.groups.count({ search: filter, top: true }))
.count;
}

if (id) {
existingUserGroups = await adminClient.users.listGroups({
id,
});
}

return { group, groups, existingUserGroups, count };
return { group, groups, existingUserGroups };
},
async ({ group: selectedGroup, groups, existingUserGroups, count }) => {
async ({ group: selectedGroup, groups, existingUserGroups }) => {
setJoinedGroups(existingUserGroups || []);
if (selectedGroup) {
setNavigation([...navigation, selectedGroup]);
Expand All @@ -117,7 +113,7 @@ export const GroupPickerDialog = ({
});
setGroups(groups);
}
setCount(count);
setCount(isSearching ? countGroups(groups || []) : groups?.length || 0);
},
[groupId, filter, first, max],
);
Expand Down Expand Up @@ -160,10 +156,7 @@ export const GroupPickerDialog = ({
]}
>
<PaginatingTableToolbar
count={
(isSearching ? count : groups.length) -
(groupId || isSearching ? first : 0)
}
count={count - (groupId || isSearching ? first : 0)}
first={first}
max={max}
onNextClick={setFirst}
Expand Down Expand Up @@ -222,7 +215,7 @@ export const GroupPickerDialog = ({
{groups
.slice(groupId ? first : 0, max + (groupId ? first : 0))
.map((group: SelectableGroup) => (
<>
<Fragment key={group.id}>
<GroupRow
key={group.id}
group={group}
Expand Down Expand Up @@ -251,7 +244,7 @@ export const GroupPickerDialog = ({
canBrowse={canBrowse}
/>
))}
</>
</Fragment>
))}
</DataList>
{groups.length === 0 && !isSearching && (
Expand Down
26 changes: 16 additions & 10 deletions js/apps/admin-ui/src/groups/components/GroupTree.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,11 @@ import {
TreeView,
TreeViewDataItem,
} from "@patternfly/react-core";
import { AngleRightIcon } from "@patternfly/react-icons";
import { unionBy } from "lodash-es";
import { useRef, useState } from "react";
import { useTranslation } from "react-i18next";
import { useNavigate } from "react-router-dom";

import { AngleRightIcon } from "@patternfly/react-icons";
import { unionBy } from "lodash-es";
import { adminClient } from "../../admin-client";
import { useAlerts } from "../../components/alert/Alerts";
import { KeycloakSpinner } from "../../components/keycloak-spinner/KeycloakSpinner";
import { PaginatingTableToolbar } from "../../components/table-toolbar/PaginatingTableToolbar";
Expand All @@ -41,6 +39,16 @@ type GroupTreeContextMenuProps = {
refresh: () => void;
};

export function countGroups(groups: GroupRepresentation[]) {
let count = groups.length;
for (const group of groups) {
if (group.subGroups) {
count += countGroups(group.subGroups);
}
}
return count;
}

const GroupTreeContextMenu = ({
group,
refresh,
Expand Down Expand Up @@ -180,8 +188,6 @@ export const GroupTree = ({
search === "" ? null : { search },
),
);
const count = (await adminClient.groups.count({ search, top: true }))
.count;
let subGroups: GroupRepresentation[] = [];
if (activeItem) {
subGroups = await fetchAdminUI<GroupRepresentation[]>(
Expand All @@ -193,9 +199,9 @@ export const GroupTree = ({
},
);
}
return { groups, count, subGroups };
return { groups, subGroups };
},
({ groups, count, subGroups }) => {
({ groups, subGroups }) => {
const found: TreeViewDataItem[] = [];
if (activeItem) findGroup(data || [], activeItem.id!, [], found);

Expand Down Expand Up @@ -233,7 +239,7 @@ export const GroupTree = ({
),
);
}
setCount(count);
setCount(countGroups(groups));
prefFirst.current = first;
prefMax.current = max;
},
Expand Down Expand Up @@ -265,7 +271,7 @@ export const GroupTree = ({

return data ? (
<PaginatingTableToolbar
count={count - first}
count={count}
first={first}
max={max}
onNextClick={setFirst}
Expand Down

0 comments on commit 895f9c8

Please sign in to comment.