mirror of
https://github.com/lukevella/rallly.git
synced 2025-04-29 10:16:32 +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 { Input } from "@rallly/ui/input";
|
||||||
import * as Sentry from "@sentry/nextjs";
|
import * as Sentry from "@sentry/nextjs";
|
||||||
import { TRPCClientError } from "@trpc/client";
|
import { TRPCClientError } from "@trpc/client";
|
||||||
|
import { signIn, useSession } from "next-auth/react";
|
||||||
import { useForm } from "react-hook-form";
|
import { useForm } from "react-hook-form";
|
||||||
import z from "zod";
|
import z from "zod";
|
||||||
|
|
||||||
|
@ -91,6 +92,7 @@ export const NewParticipantForm = (props: NewParticipantModalProps) => {
|
||||||
const isEmailRequired = poll.requireParticipantEmail;
|
const isEmailRequired = poll.requireParticipantEmail;
|
||||||
|
|
||||||
const { user } = useUser();
|
const { user } = useUser();
|
||||||
|
const session = useSession();
|
||||||
const isLoggedIn = !user.isGuest;
|
const isLoggedIn = !user.isGuest;
|
||||||
const { register, setError, formState, handleSubmit } =
|
const { register, setError, formState, handleSubmit } =
|
||||||
useForm<NewParticipantFormData>({
|
useForm<NewParticipantFormData>({
|
||||||
|
@ -111,6 +113,11 @@ export const NewParticipantForm = (props: NewParticipantModalProps) => {
|
||||||
<form
|
<form
|
||||||
onSubmit={handleSubmit(async (data) => {
|
onSubmit={handleSubmit(async (data) => {
|
||||||
try {
|
try {
|
||||||
|
if (session.status !== "authenticated") {
|
||||||
|
await signIn("guest", {
|
||||||
|
redirect: false,
|
||||||
|
});
|
||||||
|
}
|
||||||
const newParticipant = await addParticipant.mutateAsync({
|
const newParticipant = await addParticipant.mutateAsync({
|
||||||
name: data.name,
|
name: data.name,
|
||||||
votes: props.votes,
|
votes: props.votes,
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
import { usePostHog } from "@rallly/posthog/client";
|
import { usePostHog } from "@rallly/posthog/client";
|
||||||
import { useToast } from "@rallly/ui/hooks/use-toast";
|
import { useToast } from "@rallly/ui/hooks/use-toast";
|
||||||
import { signIn, useSession } from "next-auth/react";
|
|
||||||
|
|
||||||
import { usePoll } from "@/components/poll-context";
|
import { usePoll } from "@/components/poll-context";
|
||||||
import { trpc } from "@/trpc/client";
|
import { trpc } from "@/trpc/client";
|
||||||
|
@ -20,15 +19,7 @@ export const normalizeVotes = (
|
||||||
export const useAddParticipantMutation = () => {
|
export const useAddParticipantMutation = () => {
|
||||||
const posthog = usePostHog();
|
const posthog = usePostHog();
|
||||||
const queryClient = trpc.useUtils();
|
const queryClient = trpc.useUtils();
|
||||||
const session = useSession();
|
|
||||||
return trpc.polls.participants.add.useMutation({
|
return trpc.polls.participants.add.useMutation({
|
||||||
onMutate: async () => {
|
|
||||||
if (session.status !== "authenticated") {
|
|
||||||
await signIn("guest", {
|
|
||||||
redirect: false,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
},
|
|
||||||
onSuccess: async (newParticipant, input) => {
|
onSuccess: async (newParticipant, input) => {
|
||||||
const { pollId, name, email } = newParticipant;
|
const { pollId, name, email } = newParticipant;
|
||||||
queryClient.polls.participants.list.setData(
|
queryClient.polls.participants.list.setData(
|
||||||
|
|
Loading…
Add table
Reference in a new issue