Prefill notification email in Add Company wizard with the account's own email
Was always blank by default. Now prefilled from useCurrentUser() once it loads (defaultValues can't do this since the query resolves after mount), gated on the field not being dirty yet so it never clobbers something the user already typed - still fully editable either way. Co-Authored-By: Claude Sonnet 5 <[email protected]>
This commit is contained in:
@@ -3,7 +3,7 @@
|
||||
import { zodResolver } from "@hookform/resolvers/zod";
|
||||
import Link from "next/link";
|
||||
import { useRouter, useSearchParams } from "next/navigation";
|
||||
import { Suspense, useState } from "react";
|
||||
import { Suspense, useEffect, useState } from "react";
|
||||
import { Controller, useForm } from "react-hook-form";
|
||||
import { Loader2 } from "lucide-react";
|
||||
import { z } from "zod";
|
||||
@@ -13,7 +13,7 @@ import { api } from "@/lib/api-client";
|
||||
import { useCompanies, useCreateCompany } from "@/hooks/use-companies";
|
||||
import { useCreateNotificationDestination } from "@/hooks/use-notification-destinations";
|
||||
import { useDiscoverCompany } from "@/hooks/use-discovery";
|
||||
import { authErrorMessage, useSystemStatus } from "@/hooks/use-auth";
|
||||
import { authErrorMessage, useCurrentUser, useSystemStatus } from "@/hooks/use-auth";
|
||||
import {
|
||||
FREQUENCY_LABELS,
|
||||
MONITORING_FREQUENCIES,
|
||||
@@ -126,6 +126,7 @@ function AddCompanyWizard() {
|
||||
const createDestination = useCreateNotificationDestination();
|
||||
const discoverCompany = useDiscoverCompany();
|
||||
const { data: systemStatus } = useSystemStatus();
|
||||
const { data: currentUser } = useCurrentUser();
|
||||
|
||||
const {
|
||||
register,
|
||||
@@ -135,7 +136,7 @@ function AddCompanyWizard() {
|
||||
setValue,
|
||||
getValues,
|
||||
trigger,
|
||||
formState: { errors },
|
||||
formState: { errors, dirtyFields },
|
||||
} = useForm<CompanyForm>({
|
||||
resolver: zodResolver(companySchema),
|
||||
defaultValues: {
|
||||
@@ -146,6 +147,16 @@ function AddCompanyWizard() {
|
||||
},
|
||||
});
|
||||
|
||||
// Prefill with the account's own email once it loads - `defaultValues`
|
||||
// can't do this since useCurrentUser resolves after the form has already
|
||||
// mounted. Only sets it while the field is still untouched, so it never
|
||||
// clobbers something the user already typed.
|
||||
useEffect(() => {
|
||||
if (currentUser?.email && !dirtyFields.notificationEmail) {
|
||||
setValue("notificationEmail", currentUser.email);
|
||||
}
|
||||
}, [currentUser?.email, dirtyFields.notificationEmail, setValue]);
|
||||
|
||||
const frequencyType = watch("frequencyType");
|
||||
|
||||
const goNext = async () => {
|
||||
|
||||
Reference in New Issue
Block a user