diff --git a/app/plus/api.ts b/app/plus/api.ts index 06708b8e3..713f42588 100644 --- a/app/plus/api.ts +++ b/app/plus/api.ts @@ -15,6 +15,12 @@ const plusApi = createRouter() return service.getPlusStatuses(); }, }) + .query("ballots", { + resolve({ ctx }) { + const user = throwIfNotLoggedIn(ctx.user); + return service.getBallots(user.id); + }, + }) .mutation("suggestion", { input: suggestionFullSchema, resolve({ input, ctx }) { diff --git a/app/plus/components/PlusVotingPage.tsx b/app/plus/components/PlusVotingPage.tsx new file mode 100644 index 000000000..425c90a78 --- /dev/null +++ b/app/plus/components/PlusVotingPage.tsx @@ -0,0 +1,12 @@ +import usePlusVoting from "../hooks/usePlusVoting"; + +export default function PlusVotingPage() { + const { isLoading, usersToVoteOn } = usePlusVoting(); + + if (isLoading) return null; + return ( +

+
{JSON.stringify(usersToVoteOn, null, 2)}
+

+ ); +} diff --git a/app/plus/hooks/usePlusVoting.ts b/app/plus/hooks/usePlusVoting.ts new file mode 100644 index 000000000..f84d17d07 --- /dev/null +++ b/app/plus/hooks/usePlusVoting.ts @@ -0,0 +1,39 @@ +import { useUser } from "hooks/common"; +import { getVotingRange } from "utils/plus"; +import { trpc } from "utils/trpc"; + +export default function usePlusVoting() { + const [user] = useUser(); + const { data: ballotsData, isLoading: isLoadingBallots } = trpc.useQuery([ + "plus.ballots", + ]); + const { data: statusesData, isLoading: isLoadingStatuses } = trpc.useQuery([ + "plus.statuses", + ]); + const { + data: suggestionsData, + isLoading: isLoadingSuggestions, + } = trpc.useQuery(["plus.suggestions"]); + + const ownPlusStatus = statusesData?.find( + (status) => status.user.id === user?.id + ); + + const votingTier = ownPlusStatus?.membershipTier; + + return { + ballotsData: ballotsData?.filter((ballot) => !ballot.isStale), + staleBallots: ballotsData?.filter((ballot) => ballot.isStale), + shouldRedirect: + (statusesData && !votingTier) || !getVotingRange().isHappening, + usersToVoteOn: statusesData?.filter( + (user) => + (user.membershipTier && user.membershipTier === votingTier) || + (user.vouchTier && user.vouchTier === votingTier) + ), + suggestedUsersToVoteOn: suggestionsData?.filter( + (suggestion) => suggestion.tier === votingTier + ), + isLoading: isLoadingBallots || isLoadingStatuses || isLoadingSuggestions, + }; +} diff --git a/app/plus/service.ts b/app/plus/service.ts index e140db60b..a36768376 100644 --- a/app/plus/service.ts +++ b/app/plus/service.ts @@ -163,6 +163,11 @@ const getDistinctSummaryMonths = () => { }); }; +const getBallots = (userId: number) => { + if (!getVotingRange().isHappening) return null; + return prisma.plusBallot.findMany({ where: { voterUser: { id: userId } } }); +}; + const addSuggestion = async ({ input, userId, @@ -296,6 +301,7 @@ export default { getVotingSummariesByMonthAndTier, getMostRecentVotingWithResultsMonth, getDistinctSummaryMonths, + getBallots, addSuggestion, addVouch, }; diff --git a/pages/plus/voting.tsx b/pages/plus/voting.tsx new file mode 100644 index 000000000..1c8f7bcc3 --- /dev/null +++ b/pages/plus/voting.tsx @@ -0,0 +1,3 @@ +import PlusVotingPage from "app/plus/components/PlusVotingPage"; + +export default PlusVotingPage;