Skip to content

Commit

Permalink
List users with a given a badge
Browse files Browse the repository at this point in the history
  • Loading branch information
aapeliv committed Dec 16, 2024
1 parent f4b883e commit f8cdbbc
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 13 deletions.
32 changes: 30 additions & 2 deletions app/web/features/badges/BadgesPage.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import CenteredSpinner from "components/CenteredSpinner/CenteredSpinner";
import {
Divider,
DividerProps,
Expand All @@ -7,10 +6,13 @@ import {
styled,
Typography,
} from "@mui/material";
import Button from "components/Button";
import CenteredSpinner from "components/CenteredSpinner/CenteredSpinner";
import HtmlMeta from "components/HtmlMeta";
import PageTitle from "components/PageTitle";
import UsersList from "components/UsersList";
import Badge from "features/badges/Badge";
import { useBadges } from "features/badges/hooks";
import { useBadges, useBadgeUsers } from "features/badges/hooks";
import { useTranslation } from "i18n";
import { GLOBAL, PROFILE } from "i18n/namespaces";

Expand All @@ -36,6 +38,31 @@ const CenteredDiv = styled(ContentDiv)(({ theme }) => ({
justifyContent: "center",
}));

export interface BadgeUserListProps {
badgeId: string;
}

function BadgeUserList({ badgeId }: BadgeUserListProps) {
const { badgeUserIds, hasNextPage, isFetchingNextPage, fetchNextPage } =
useBadgeUsers(badgeId);

return (
<UsersList
userIds={badgeUserIds}
endChildren={
hasNextPage && (
<Button loading={isFetchingNextPage} onClick={() => fetchNextPage()}>
Load more
</Button>
)
}
emptyListChildren={
<Typography variant="body1">No people with this badge!</Typography>
}
/>
);
}

export interface BadgesPageProps {
badgeId?: string;
}
Expand Down Expand Up @@ -75,6 +102,7 @@ export default function BadgesPage({ badgeId = undefined }: BadgesPageProps) {
</Typography>
</FlexDiv>
<StyledDivider />
<BadgeUserList badgeId={badgeId} />
</>
) : (
<>Badge not found</>
Expand Down
19 changes: 10 additions & 9 deletions app/web/features/badges/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { ListBadgeUsersRes } from "proto/api_pb";
import { Badge } from "proto/resources_pb";
import { useInfiniteQuery, useQuery } from "react-query";
import { service } from "service";
import type { ListBadgeUsersInput } from "service/api";

export const useBadges = () => {
const { data, ...rest } = useQuery(badgesKey, () =>
Expand All @@ -27,14 +26,16 @@ export const useBadges = () => {
};
};

export function useBadgeUsers({
badgeId,
pageSize,
}: Omit<ListBadgeUsersInput, "pageToken">) {
return useInfiniteQuery<ListBadgeUsersRes.AsObject, RpcError>({
queryKey: badgeUsersKey,
export function useBadgeUsers(badgeId: string) {
const query = useInfiniteQuery<ListBadgeUsersRes.AsObject, RpcError>({
queryKey: badgeUsersKey({ badgeId }),
queryFn: ({ pageParam }) =>
service.api.listBadgeUsers({ badgeId, pageSize, pageToken: pageParam }),
getNextPageParam: (lastPage) => lastPage.nextPageToken ?? undefined,
service.api.listBadgeUsers({ badgeId, pageToken: pageParam }),
getNextPageParam: (lastPage) => lastPage.nextPageToken || undefined,
});
const badgeUserIds = query.data?.pages.flatMap((res) => res.userIdsList);
return {
...query,
badgeUserIds,
};
}
2 changes: 1 addition & 1 deletion app/web/features/communities/events/EventUsers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export default function EventUsers({
)
}
emptyListChildren={
!error && <Typography variant="body1">{emptyState}</Typography>
<Typography variant="body1">{emptyState}</Typography>
}
/>
</Card>
Expand Down
8 changes: 7 additions & 1 deletion app/web/features/queryKeys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,4 +156,10 @@ export const myEventsKey = (type: EventsType) => ["myEvents", { type }];
export const activeLoginsKey = "activeLogins";

// Badges
export const badgeUsersKey = "badgeUsers";
export interface BadgeUsersInput {
badgeId: string;
}
export const badgeUsersKey = ({ badgeId }: BadgeUsersInput) => [
"badgeUsers",
badgeId,
];

0 comments on commit f8cdbbc

Please sign in to comment.