initial useplusvoting

This commit is contained in:
Kalle (Sendou) 2021-03-17 13:36:56 +02:00
parent 0524fe4aca
commit 8ddc3ac353
5 changed files with 66 additions and 0 deletions

View File

@ -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 }) {

View File

@ -0,0 +1,12 @@
import usePlusVoting from "../hooks/usePlusVoting";
export default function PlusVotingPage() {
const { isLoading, usersToVoteOn } = usePlusVoting();
if (isLoading) return null;
return (
<h1>
<pre>{JSON.stringify(usersToVoteOn, null, 2)}</pre>
</h1>
);
}

View File

@ -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,
};
}

View File

@ -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,
};

3
pages/plus/voting.tsx Normal file
View File

@ -0,0 +1,3 @@
import PlusVotingPage from "app/plus/components/PlusVotingPage";
export default PlusVotingPage;