Skip to content

Commit 5391c15

Browse files
committed
use the reduced groups for user endpoint on the AccountPage
1 parent 2409072 commit 5391c15

File tree

5 files changed

+45
-15
lines changed

5 files changed

+45
-15
lines changed

site/src/api/api.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1619,6 +1619,21 @@ class ApiMethods {
16191619
return response.data;
16201620
};
16211621

1622+
/**
1623+
* @param organization Can be the organization's ID or name
1624+
* @param user Can be the user's ID, username, or "me"
1625+
*/
1626+
getReducedGroupsForUser = async (
1627+
organization: string,
1628+
user: string,
1629+
): Promise<TypesGen.ReducedGroup[]> => {
1630+
const response = await this.axios.get(
1631+
`/api/v2/organizations/${organization}/users/${user}/reduced-groups`,
1632+
);
1633+
1634+
return response.data;
1635+
};
1636+
16221637
addMember = async (groupId: string, userId: string) => {
16231638
return this.patchGroup(groupId, {
16241639
name: "",

site/src/api/queries/groups.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type {
44
CreateGroupRequest,
55
Group,
66
PatchGroupRequest,
7+
ReducedGroup,
78
} from "api/typesGenerated";
89

910
type GroupSortOrder = "asc" | "desc";
@@ -125,6 +126,13 @@ export const deleteGroup = (queryClient: QueryClient) => {
125126
};
126127
};
127128

129+
export function reducedGroupsForUser(organization: string, userId: string) {
130+
return {
131+
queryKey: ["organization", organization, "user", userId, "reduced-groups"],
132+
queryFn: () => API.getReducedGroupsForUser(organization, userId),
133+
} as const satisfies UseQueryOptions<ReducedGroup[], unknown, readonly ReducedGroup[]>;
134+
}
135+
128136
export const addMember = (queryClient: QueryClient) => {
129137
return {
130138
mutationFn: ({ groupId, userId }: { groupId: string; userId: string }) =>

site/src/pages/UserSettingsPage/AccountPage/AccountPage.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { FC } from "react";
22
import { useQuery } from "react-query";
3-
import { groupsForUser } from "api/queries/groups";
3+
import { reducedGroupsForUser } from "api/queries/groups";
44
import { Stack } from "components/Stack/Stack";
55
import { useAuthContext } from "contexts/auth/AuthProvider";
66
import { useAuthenticated } from "contexts/auth/RequireAuth";
@@ -18,7 +18,7 @@ export const AccountPage: FC = () => {
1818
const hasGroupsFeature = entitlements.features.user_role_management.enabled;
1919
const groupsQuery = useQuery({
2020
// TODO: This should probably list all groups, not just default org groups
21-
...groupsForUser("default", me.id),
21+
...reducedGroupsForUser("default", me.id),
2222
enabled: hasGroupsFeature,
2323
});
2424

site/src/pages/UserSettingsPage/AccountPage/AccountUserGroups.stories.tsx

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,24 @@
11
import type { Meta, StoryObj } from "@storybook/react";
2-
import type { Group } from "api/typesGenerated";
2+
import type { ReducedGroup } from "api/typesGenerated";
33
import {
44
MockGroup as MockGroup1,
5-
MockUser,
65
mockApiError,
76
} from "testHelpers/entities";
87
import { AccountUserGroups } from "./AccountUserGroups";
98

10-
const MockGroup2: Group = {
11-
...MockGroup1,
12-
avatar_url: "",
9+
const MockReducedGroup1: ReducedGroup = {
10+
id: MockGroup1.id,
11+
name: MockGroup1.name,
12+
display_name: MockGroup1.display_name,
13+
organization_id: MockGroup1.organization_id,
14+
avatar_url: MockGroup1.avatar_url,
15+
total_member_count: 10,
16+
};
17+
18+
const MockReducedGroup2: ReducedGroup = {
19+
...MockReducedGroup1,
1320
display_name: "Goofy Goobers",
14-
members: [MockUser],
21+
total_member_count: 5,
1522
};
1623

1724
const mockError = mockApiError({
@@ -22,7 +29,7 @@ const meta: Meta<typeof AccountUserGroups> = {
2229
title: "pages/UserSettingsPage/AccountUserGroups",
2330
component: AccountUserGroups,
2431
args: {
25-
groups: [MockGroup1, MockGroup2],
32+
groups: [MockReducedGroup1, MockReducedGroup2],
2633
loading: false,
2734
},
2835
};
@@ -40,7 +47,7 @@ export const NoGroups: Story = {
4047

4148
export const OneGroup: Story = {
4249
args: {
43-
groups: [MockGroup1],
50+
groups: [MockReducedGroup1],
4451
},
4552
};
4653

@@ -61,7 +68,7 @@ export const Error: Story = {
6168

6269
export const ErrorWithPreviousData: Story = {
6370
args: {
64-
groups: [MockGroup1, MockGroup2],
71+
groups: [MockReducedGroup1, MockReducedGroup2],
6572
error: mockError,
6673
loading: false,
6774
},

site/src/pages/UserSettingsPage/AccountPage/AccountUserGroups.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ import { useTheme } from "@emotion/react";
22
import Grid from "@mui/material/Grid";
33
import type { FC } from "react";
44
import { isApiError } from "api/errors";
5-
import type { Group } from "api/typesGenerated";
5+
import type { ReducedGroup } from "api/typesGenerated";
66
import { ErrorAlert } from "components/Alert/ErrorAlert";
77
import { AvatarCard } from "components/AvatarCard/AvatarCard";
88
import { Loader } from "components/Loader/Loader";
99
import { Section } from "../Section";
1010

1111
type AccountGroupsProps = {
12-
groups: readonly Group[] | undefined;
12+
groups: readonly ReducedGroup[] | undefined;
1313
error: unknown;
1414
loading: boolean;
1515
};
@@ -57,8 +57,8 @@ export const AccountUserGroups: FC<AccountGroupsProps> = ({
5757
header={group.display_name || group.name}
5858
subtitle={
5959
<>
60-
{group.members.length} member
61-
{group.members.length !== 1 && "s"}
60+
{group.total_member_count} member
61+
{group.total_member_count !== 1 && "s"}
6262
</>
6363
}
6464
/>

0 commit comments

Comments
 (0)