🐛 Fix possible race condition when adding new participant (#1617)

This commit is contained in:
Luke Vella 2025-03-06 16:35:09 +00:00 committed by GitHub
parent 3d8604a379
commit bb8a7f4102
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 7 additions and 9 deletions

View file

@ -7,6 +7,7 @@ import { FormMessage } from "@rallly/ui/form";
import { Input } from "@rallly/ui/input";
import * as Sentry from "@sentry/nextjs";
import { TRPCClientError } from "@trpc/client";
import { signIn, useSession } from "next-auth/react";
import { useForm } from "react-hook-form";
import z from "zod";
@ -91,6 +92,7 @@ export const NewParticipantForm = (props: NewParticipantModalProps) => {
const isEmailRequired = poll.requireParticipantEmail;
const { user } = useUser();
const session = useSession();
const isLoggedIn = !user.isGuest;
const { register, setError, formState, handleSubmit } =
useForm<NewParticipantFormData>({
@ -111,6 +113,11 @@ export const NewParticipantForm = (props: NewParticipantModalProps) => {
<form
onSubmit={handleSubmit(async (data) => {
try {
if (session.status !== "authenticated") {
await signIn("guest", {
redirect: false,
});
}
const newParticipant = await addParticipant.mutateAsync({
name: data.name,
votes: props.votes,

View file

@ -1,6 +1,5 @@
import { usePostHog } from "@rallly/posthog/client";
import { useToast } from "@rallly/ui/hooks/use-toast";
import { signIn, useSession } from "next-auth/react";
import { usePoll } from "@/components/poll-context";
import { trpc } from "@/trpc/client";
@ -20,15 +19,7 @@ export const normalizeVotes = (
export const useAddParticipantMutation = () => {
const posthog = usePostHog();
const queryClient = trpc.useUtils();
const session = useSession();
return trpc.polls.participants.add.useMutation({
onMutate: async () => {
if (session.status !== "authenticated") {
await signIn("guest", {
redirect: false,
});
}
},
onSuccess: async (newParticipant, input) => {
const { pollId, name, email } = newParticipant;
queryClient.polls.participants.list.setData(