Skip to content

Commit

Permalink
✨ Set default values in new participant form when user is logged in (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
lukevella authored Jan 17, 2025
1 parent aad31ff commit bbf9bae
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions apps/web/src/components/new-participant-modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ import { TRPCClientError } from "@trpc/client";
import clsx from "clsx";
import { useTranslation } from "next-i18next";
import { useForm } from "react-hook-form";
import { useMount } from "react-use";
import z from "zod";

import { usePoll } from "@/contexts/poll";

import { useAddParticipantMutation } from "./poll/mutations";
import VoteIcon from "./poll/vote-icon";
import { useUser } from "./user-provider";

const requiredEmailSchema = z.object({
requireEmail: z.literal(true),
Expand Down Expand Up @@ -90,17 +90,23 @@ export const NewParticipantForm = (props: NewParticipantModalProps) => {

const isEmailRequired = poll.requireParticipantEmail;

const { register, setError, formState, setFocus, handleSubmit } =
const { user } = useUser();
const isLoggedIn = !user.isGuest;

const { register, setError, formState, handleSubmit } =
useForm<NewParticipantFormData>({
resolver: zodResolver(schema),
defaultValues: {
requireEmail: isEmailRequired,
...(isLoggedIn
? { name: user.name, email: user.email ?? "" }
: {
name: "",
email: "",
}),
},
});
const addParticipant = useAddParticipantMutation();
useMount(() => {
setFocus("name");
});

return (
<form
Expand Down Expand Up @@ -131,6 +137,7 @@ export const NewParticipantForm = (props: NewParticipantModalProps) => {
<Input
className="w-full"
data-1p-ignore="true"
autoFocus={true}
error={!!formState.errors.name}
disabled={formState.isSubmitting}
placeholder={t("namePlaceholder")}
Expand Down

0 comments on commit bbf9bae

Please sign in to comment.