mirror of
https://github.com/lukevella/rallly.git
synced 2025-04-28 17:56:37 +02:00
🐛 Fix possible race condition when adding new participant (#1617)
This commit is contained in:
parent
3d8604a379
commit
bb8a7f4102
2 changed files with 7 additions and 9 deletions
|
@ -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,
|
||||
|
|
|
@ -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(
|
||||
|
|
Loading…
Add table
Reference in a new issue