Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

Disable submit buttons when form states are clean

Tim Wilson4c476836cb74
diff --git a/apps/web/ui/modals/link-builder/expiration-modal.tsx b/apps/web/ui/modals/link-builder/expiration-modal.tsx
index 4690b55d9d..10494b11d0 100644
--- a/apps/web/ui/modals/link-builder/expiration-modal.tsx
+++ b/apps/web/ui/modals/link-builder/expiration-modal.tsx
@@ -49,7 +49,7 @@ function ExpirationModal({
register,
setValue,
reset,
- formState: { errors },
+ formState: { isDirty, errors },
handleSubmit,
} = useForm<Pick<LinkFormData, "expiresAt" | "expiredUrl">>({
values: {
@@ -75,8 +75,12 @@ function ExpirationModal({
onSubmit={(e) => {
e.stopPropagation();
handleSubmit((data) => {
- setValueParent("expiresAt", data.expiresAt);
- setValueParent("expiredUrl", data.expiredUrl);
+ setValueParent("expiresAt", data.expiresAt, {
+ shouldDirty: true,
+ });
+ setValueParent("expiredUrl", data.expiredUrl, {
+ shouldDirty: true,
+ });
setShowExpirationModal(false);
})(e);
}}
@@ -133,7 +137,9 @@ function ExpirationModal({
if (e.target.value.length > 0) {
const parsedDateTime = parseDateTime(e.target.value);
if (parsedDateTime) {
- setValue("expiresAt", parsedDateTime);
+ setValue("expiresAt", parsedDateTime, {
+ shouldDirty: true,
+ });
e.target.value = formatDateTime(parsedDateTime);
}
}
@@ -145,7 +151,9 @@ function ExpirationModal({
inputRef.current.value,
);
if (parsedDateTime) {
- setValue("expiresAt", parsedDateTime);
+ setValue("expiresAt", parsedDateTime, {
+ shouldDirty: true,
+ });
inputRef.current.value = formatDateTime(parsedDateTime);
}
}
@@ -159,7 +167,7 @@ function ExpirationModal({
value={expiresAt ? getDateTimeLocal(expiresAt) : ""}
onChange={(e) => {
const expiryDate = new Date(e.target.value);
- setValue("expiresAt", expiryDate);
+ setValue("expiresAt", expiryDate, { shouldDirty: true });
if (inputRef.current) {
inputRef.current.value = formatDateTime(expiryDate);
}
@@ -219,8 +227,8 @@ function ExpirationModal({
type="button"
className="text-xs font-medium text-gray-700 transition-colors hover:text-gray-950"
onClick={() => {
- setValueParent("expiresAt", null);
- setValueParent("expiredUrl", null);
+ setValueParent("expiresAt", null, { shouldDirty: true });
+ setValueParent("expiredUrl", null, { shouldDirty: true });
setShowExpirationModal(false);
}}
>
@@ -244,6 +252,7 @@ function ExpirationModal({
variant="primary"
text={expiresAtParent ? "Save" : "Add expiration"}
className="h-9 w-fit"
+ disabled={!isDirty}
/>
</div>
</div>
diff --git a/apps/web/ui/modals/link-builder/index.tsx b/apps/web/ui/modals/link-builder/index.tsx
index fb9c823f25..8e68f2f92e 100644
--- a/apps/web/ui/modals/link-builder/index.tsx
+++ b/apps/web/ui/modals/link-builder/index.tsx
@@ -20,7 +20,6 @@ import {
import { ArrowTurnLeft } from "@dub/ui/src/icons";
import {
cn,
- deepEqual,
DEFAULT_LINK_PROPS,
getApexDomain,
getUrlWithoutUTMParams,
@@ -118,7 +117,7 @@ function LinkBuilderInner({
setValue,
setError,
clearErrors,
- formState: { isSubmitting, isSubmitSuccessful, errors },
+ formState: { isDirty, isSubmitting, isSubmitSuccessful, errors },
} = useFormContext<LinkFormData>();
const data = watch();
@@ -159,20 +158,7 @@ function LinkBuilderInner({
isSubmitSuccessful ||
errors.key ||
errors.url ||
- (props &&
- Object.entries(props).every(([key, value]) => {
- // If the key is "title" or "description" and proxy is not enabled, return true (skip the check)
- if (
- (key === "title" || key === "description" || key === "image") &&
- !proxy
- )
- return true;
- else if (key === "geo")
- return deepEqual(props.geo as object, data.geo as object);
-
- // Otherwise, check for discrepancy in the current key-value pair
- return data[key] === value;
- })),
+ (props && !isDirty),
);
}, [showLinkBuilder, isSubmitting, isSubmitSuccessful, errors, props, data]);
diff --git a/apps/web/ui/modals/link-builder/more-dropdown.tsx b/apps/web/ui/modals/link-builder/more-dropdown.tsx
index aa327ca82f..8253683cbf 100644
--- a/apps/web/ui/modals/link-builder/more-dropdown.tsx
+++ b/apps/web/ui/modals/link-builder/more-dropdown.tsx
@@ -40,7 +40,7 @@ export function MoreDropdown() {
if (!option) return;
setOpenPopover(false);
- setValue(option.key as any, !data[option.key]);
+ setValue(option.key as any, !data[option.key], { shouldDirty: true });
},
{ modal: true },
);
@@ -76,7 +76,10 @@ export function MoreDropdown() {
targeting: setShowTargetingModal,
expiresAt: setShowExpirationModal,
})[option.key]?.(true);
- } else setValue(option.key as any, !enabled);
+ } else
+ setValue(option.key as any, !enabled, {
+ shouldDirty: true,
+ });
}}
className="h-9 w-full justify-start px-2 text-sm text-gray-700"
textWrapperClassName="grow"
diff --git a/apps/web/ui/modals/link-builder/og-modal.tsx b/apps/web/ui/modals/link-builder/og-modal.tsx
index beefe72155..d7ce00a98c 100644
--- a/apps/web/ui/modals/link-builder/og-modal.tsx
+++ b/apps/web/ui/modals/link-builder/og-modal.tsx
@@ -60,9 +60,13 @@ function OGModalInner({
setValue: setValueParent,
} = useFormContext<LinkFormData>();
- const { watch, setValue, getValues, reset, handleSubmit } = useForm<
- Pick<LinkFormData, "image" | "title" | "description" | "proxy">
- >({
+ const {
+ watch,
+ setValue,
+ reset,
+ handleSubmit,
+ formState: { isDirty },
+ } = useForm<Pick<LinkFormData, "image" | "title" | "description" | "proxy">>({
defaultValues: {
image: getValuesParent("image"),
title: getValuesParent("title"),
@@ -86,8 +90,8 @@ function OGModalInner({
onSubmit: (image) => {
if (!image) return;
- setValue("image", image);
- setValue("proxy", true);
+ setValue("image", image, { shouldDirty: true });
+ setValue("proxy", true, { shouldDirty: true });
},
});
@@ -100,7 +104,6 @@ function OGModalInner({
complete: completeTitle,
} = useCompletion({
api: `/api/ai/completion?workspaceId=${workspaceId}`,
- id: "metatags-title-ai",
onError: (error) => {
if (error.message.includes("Upgrade to Pro")) {
toast.custom(() => (
@@ -136,7 +139,10 @@ function OGModalInner({
};
useEffect(() => {
- if (completionTitle) setValue("title", completionTitle);
+ if (completionTitle) {
+ setValue("title", completionTitle, { shouldDirty: true });
+ setValue("proxy", true, { shouldDirty: true });
+ }
}, [completionTitle]);
const {
@@ -145,7 +151,6 @@ function OGModalInner({
complete: completeDescription,
} = useCompletion({
api: `/api/ai/completion?workspaceId=${workspaceId}`,
- id: "metatags-description-ai",
onError: (error) => {
if (error.message.includes("Upgrade to Pro")) {
toast.custom(() => (
@@ -180,7 +185,10 @@ function OGModalInner({
};
useEffect(() => {
- if (completionDescription) setValue("description", completionDescription);
+ if (completionDescription) {
+ setValue("description", completionDescription, { shouldDirty: true });
+ setValue("proxy", true, { shouldDirty: true });
+ }
}, [completionDescription]);
return (
@@ -198,7 +206,7 @@ function OGModalInner({
handleSubmit(async (data) => {
setShowOGModal(false);
(["image", "title", "description", "proxy"] as const).forEach(
- (key) => setValueParent(key, data[key]),
+ (key) => setValueParent(key, data[key], { shouldDirty: true }),
);
})(e);
}}
@@ -246,8 +254,8 @@ function OGModalInner({
type="button"
className="text-xs font-medium text-gray-700 transition-colors hover:text-gray-950"
onClick={() => {
- setValue("image", null);
- setValue("proxy", false);
+ setValue("image", null, { shouldDirty: true });
+ setValue("proxy", false, { shouldDirty: true });
}}
>
Remove
@@ -265,8 +273,8 @@ function OGModalInner({
content={
<UnsplashSearch
onImageSelected={(image) => {
- setValue("image", image);
- setValue("proxy", true);
+ setValue("image", image, { shouldDirty: true });
+ setValue("proxy", true, { shouldDirty: true });
}}
setOpenPopover={setOpenUnsplashPopover}
/>
@@ -294,8 +302,8 @@ function OGModalInner({
setResizing(true);
const image = await resizeImage(file);
- setValue("image", image);
- setValue("proxy", true);
+ setValue("image", image, { shouldDirty: true });
+ setValue("proxy", true, { shouldDirty: true });
// Delay to prevent flickering
setTimeout(() => setResizing(false), 500);
@@ -351,7 +359,8 @@ function OGModalInner({
placeholder="Add a title..."
value={title || ""}
onChange={(e) => {
- setValue("title", e.target.value);
+ setValue("title", e.target.value, { shouldDirty: true });
+ setValue("proxy", true, { shouldDirty: true });
}}
aria-invalid="true"
/>
@@ -396,7 +405,12 @@ function OGModalInner({
className="block w-full rounded-md border-gray-300 text-gray-900 placeholder-gray-400 focus:border-gray-500 focus:outline-none focus:ring-gray-500 sm:text-sm"
placeholder="Add a description..."
value={description || ""}
- onChange={(e) => setValue("description", e.target.value)}
+ onChange={(e) => {
+ setValue("description", e.target.value, {
+ shouldDirty: true,
+ });
+ setValue("proxy", true, { shouldDirty: true });
+ }}
aria-invalid="true"
/>
</div>
@@ -420,6 +434,7 @@ function OGModalInner({
variant="primary"
text="Save changes"
className="h-9 w-fit"
+ disabled={!isDirty}
/>
</div>
</div>
diff --git a/apps/web/ui/modals/link-builder/password-modal.tsx b/apps/web/ui/modals/link-builder/password-modal.tsx
index db078b6dae..c01486ed03 100644
--- a/apps/web/ui/modals/link-builder/password-modal.tsx
+++ b/apps/web/ui/modals/link-builder/password-modal.tsx
@@ -39,7 +39,7 @@ function PasswordModal({
register,
setValue,
reset,
- formState: { errors },
+ formState: { isDirty, errors },
handleSubmit,
} = useForm<Pick<LinkFormData, "password">>({
values: {
@@ -63,7 +63,7 @@ function PasswordModal({
onSubmit={(e) => {
e.stopPropagation();
handleSubmit((data) => {
- setValueParent("password", data.password);
+ setValueParent("password", data.password, { shouldDirty: true });
setShowPasswordModal(false);
})(e);
}}
@@ -122,7 +122,7 @@ function PasswordModal({
className="text-gray-500 transition-colors hover:text-gray-800"
tooltipContent="Generate a random password"
onClick={() => {
- setValue("password", nanoid(24));
+ setValue("password", nanoid(24), { shouldDirty: true });
}}
>
<Shuffle className="size-4" />
@@ -153,7 +153,7 @@ function PasswordModal({
type="button"
className="text-xs font-medium text-gray-700 transition-colors hover:text-gray-950"
onClick={() => {
- setValueParent("password", null);
+ setValueParent("password", null, { shouldDirty: true });
setShowPasswordModal(false);
}}
>
@@ -177,6 +177,7 @@ function PasswordModal({
variant="primary"
text={passwordParent ? "Save" : "Add password"}
className="h-9 w-fit"
+ disabled={!isDirty}
/>
</div>
</div>
diff --git a/apps/web/ui/modals/link-builder/tag-select.tsx b/apps/web/ui/modals/link-builder/tag-select.tsx
index 327eccce9a..e50b445dc7 100644
--- a/apps/web/ui/modals/link-builder/tag-select.tsx
+++ b/apps/web/ui/modals/link-builder/tag-select.tsx
@@ -74,6 +74,7 @@ export function TagSelect() {
setValue(
"tags",
selectedIds.map((id) => availableTags?.find((t) => t.id === id)),
+ { shouldDirty: true },
);
}}
options={options}
diff --git a/apps/web/ui/modals/link-builder/targeting-modal.tsx b/apps/web/ui/modals/link-builder/targeting-modal.tsx
index 0de57784d8..fd7091e2cd 100644
--- a/apps/web/ui/modals/link-builder/targeting-modal.tsx
+++ b/apps/web/ui/modals/link-builder/targeting-modal.tsx
@@ -35,9 +35,14 @@ function TargetingModal({
setValue: setValueParent,
} = useFormContext<LinkFormData>();
- const { watch, register, setValue, reset, handleSubmit } = useForm<
- Pick<LinkFormData, "ios" | "android" | "geo">
- >({
+ const {
+ watch,
+ register,
+ setValue,
+ reset,
+ handleSubmit,
+ formState: { isDirty },
+ } = useForm<Pick<LinkFormData, "ios" | "android" | "geo">>({
values: {
ios: getValuesParent("ios"),
android: getValuesParent("android"),
@@ -69,8 +74,8 @@ function TargetingModal({
onSubmit={(e) => {
e.stopPropagation();
handleSubmit((data) => {
- setValueParent("ios", data.ios);
- setValueParent("android", data.android);
+ setValueParent("ios", data.ios, { shouldDirty: true });
+ setValueParent("android", data.android, { shouldDirty: true });
// Filter out empty geo values
const geo = Object.fromEntries(
@@ -78,7 +83,9 @@ function TargetingModal({
([key, value]) => key?.trim() && value?.trim(),
),
);
- setValueParent("geo", Object.keys(geo).length > 0 ? geo : null);
+ setValueParent("geo", Object.keys(geo).length > 0 ? geo : null, {
+ shouldDirty: true,
+ });
setShowTargetingModal(false);
})(e);
@@ -134,7 +141,7 @@ function TargetingModal({
delete Object.assign(newGeo, geo, {
[e.target.value]: value,
})[key];
- setValue("geo", newGeo);
+ setValue("geo", newGeo, { shouldDirty: true });
}}
className="flex w-32 items-center justify-center rounded-l-md border border-r-0 border-gray-300 bg-white pl-3 pr-7 text-left text-sm text-gray-500 focus:border-gray-300 focus:outline-none focus:ring-0"
>
@@ -154,10 +161,14 @@ function TargetingModal({
className="h-full grow rounded-r-md border border-gray-300 text-sm placeholder-gray-400 focus:border-gray-500 focus:ring-gray-500"
value={value}
onChange={(e) => {
- setValue(`geo`, {
- ...((geo as object) || {}),
- [key]: e.target.value,
- });
+ setValue(
+ `geo`,
+ {
+ ...((geo as object) || {}),
+ [key]: e.target.value,
+ },
+ { shouldDirty: true },
+ );
}}
/>
</Fragment>
@@ -170,7 +181,11 @@ function TargetingModal({
text="Add location"
className="h-9"
onClick={() => {
- setValue("geo", { ...((geo as object) || {}), "": "" });
+ setValue(
+ "geo",
+ { ...((geo as object) || {}), "": "" },
+ { shouldDirty: true },
+ );
}}
disabled={Object.keys(geo || {}).includes("")}
/>
@@ -243,9 +258,9 @@ function TargetingModal({
type="button"
className="text-xs font-medium text-gray-700 transition-colors hover:text-gray-950"
onClick={() => {
- setValueParent("ios", null);
- setValueParent("android", null);
- setValueParent("geo", null);
+ setValueParent("ios", null, { shouldDirty: true });
+ setValueParent("android", null, { shouldDirty: true });
+ setValueParent("geo", null, { shouldDirty: true });
setShowTargetingModal(false);
}}
>
@@ -269,6 +284,7 @@ function TargetingModal({
variant="primary"
text={parentEnabled ? "Save" : "Add targeting"}
className="h-9 w-fit"
+ disabled={!isDirty}
/>
</div>
</div>