Skip to content

Commit ad1bb46

Browse files
authored
fix: update WebsiteDialog form state handling. (#251)
* fix: update WebsiteDialog form state handling. * Add validation messages to website dialog.
1 parent 58d31b6 commit ad1bb46

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

apps/dashboard/components/website-dialog.tsx

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
"use client";
22

3+
import { WEBSITE_NAME_REGEX } from "@databuddy/validation";
34
import { zodResolver } from "@hookform/resolvers/zod";
45
import { useEffect } from "react";
5-
import { useForm } from "react-hook-form";
6+
import { type SubmitHandler, useForm } from "react-hook-form";
67
import { toast } from "sonner";
78
import { z } from "zod";
89
import { useOrganizationsContext } from "@/components/providers/organizations-provider";
@@ -41,7 +42,11 @@ const domainRegex =
4142
const wwwRegex = /^www\./;
4243

4344
const formSchema = z.object({
44-
name: z.string().min(1, "Name is required"),
45+
name: z
46+
.string()
47+
.trim()
48+
.min(1, "Name is required")
49+
.regex(WEBSITE_NAME_REGEX, "Use alphanumeric, spaces, -, _, or ."),
4550
domain: z
4651
.string()
4752
.min(1, "Domain is required")
@@ -72,6 +77,7 @@ export function WebsiteDialog({
7277

7378
const form = useForm<FormData>({
7479
resolver: zodResolver(formSchema),
80+
mode: "onBlur",
7581
defaultValues: {
7682
name: "",
7783
domain: "",
@@ -117,8 +123,7 @@ export function WebsiteDialog({
117123
return rpcError?.message || defaultMessage;
118124
};
119125

120-
const handleSubmit = async () => {
121-
const formData = form.getValues();
126+
const handleSubmit: SubmitHandler<FormData> = async (formData) => {
122127
const submissionData: CreateWebsiteData = {
123128
name: formData.name,
124129
domain: formData.domain,
@@ -154,7 +159,10 @@ export function WebsiteDialog({
154159
const isPending =
155160
createWebsiteMutation.isPending || updateWebsiteMutation.isPending;
156161

157-
const isSubmitDisabled = !(form.formState.isValid && form.formState.isDirty);
162+
// Should not access directly (form.formState.isValid); that doesn't trigger re-render
163+
// https://react-hook-form.com/docs/useform/formstate
164+
const { isValid, isDirty } = form.formState;
165+
const isSubmitDisabled = !(isValid && isDirty);
158166

159167
return (
160168
<FormDialog

0 commit comments

Comments
 (0)