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.

Enable editing for additional partner links

Kiran K430f14b773ea
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/groups/[groupSlug]/links/add-edit-additional-partner-link-modal.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/groups/[groupSlug]/links/add-edit-additional-partner-link-modal.tsx
index 6589b860eb..561cfee202 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/groups/[groupSlug]/links/add-edit-additional-partner-link-modal.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/groups/[groupSlug]/links/add-edit-additional-partner-link-modal.tsx
@@ -1,6 +1,10 @@
"use client";
+import { mutatePrefix } from "@/lib/swr/mutate";
+import { useApiMutation } from "@/lib/swr/use-api-mutation";
+import useGroup from "@/lib/swr/use-group";
import { AdditionalPartnerLink } from "@/lib/types";
+import { MAX_ADDITIONAL_PARTNER_LINKS } from "@/lib/zod/schemas/groups";
import { Button, Input, Modal } from "@dub/ui";
import { CircleCheckFill } from "@dub/ui/icons";
import { cn } from "@dub/utils";
@@ -23,38 +27,95 @@ const URL_VALIDATION_MODES = [
interface AddDestinationUrlModalProps {
setIsOpen: Dispatch<SetStateAction<boolean>>;
+ linkIndex?: number;
}
function AddDestinationUrlModalContent({
setIsOpen,
+ linkIndex,
}: AddDestinationUrlModalProps) {
- const {
- register,
- handleSubmit,
- watch,
- setValue,
- formState: { isSubmitting },
- } = useForm<AdditionalPartnerLink>({
- defaultValues: {
- url: "",
- urlValidationMode: "domain",
- },
- });
-
- const urlValidationMode = watch("urlValidationMode");
+ const { group } = useGroup();
+ const { makeRequest: updateGroup, isSubmitting } = useApiMutation();
+
+ const link =
+ linkIndex !== undefined && group?.additionalLinks
+ ? group.additionalLinks[linkIndex]
+ : undefined;
+
+ const { register, handleSubmit, watch, setValue } =
+ useForm<AdditionalPartnerLink>({
+ defaultValues: {
+ url: link?.url || "",
+ urlValidationMode: link?.urlValidationMode || "domain",
+ },
+ });
+
+ const [url, urlValidationMode] = watch(["url", "urlValidationMode"]);
const onSubmit = async (data: AdditionalPartnerLink) => {
- // TODO: Add backend integration here
- console.log("Form data:", data);
- toast.success("Destination URL added successfully");
- setIsOpen(false);
+ if (!group) return;
+
+ const currentAdditionalLinks = group.additionalLinks || [];
+
+ // Check for duplicate URLs (excluding the current link being edited)
+ const existingUrls = currentAdditionalLinks
+ .map((link, index) => ({ url: link.url, index }))
+ .filter(({ index }) => linkIndex === undefined || index !== linkIndex)
+ .map(({ url }) => url.toLowerCase());
+
+ if (existingUrls.includes(data.url.toLowerCase())) {
+ toast.error("This destination URL already exists.");
+ return;
+ }
+
+ let updatedAdditionalLinks: AdditionalPartnerLink[];
+
+ if (linkIndex !== undefined) {
+ // Editing existing link
+ updatedAdditionalLinks = [...currentAdditionalLinks];
+ updatedAdditionalLinks[linkIndex] = data;
+ } else {
+ // Check if we're at the maximum number of additional links
+ if (currentAdditionalLinks.length >= MAX_ADDITIONAL_PARTNER_LINKS) {
+ toast.error(
+ `You can only create up to ${MAX_ADDITIONAL_PARTNER_LINKS} additional destination URLs.`,
+ );
+ return;
+ }
+
+ // Creating new link
+ updatedAdditionalLinks = [...currentAdditionalLinks, data];
+ }
+
+ await updateGroup(`/api/groups/${group.id}`, {
+ method: "PATCH",
+ body: {
+ additionalLinks: updatedAdditionalLinks,
+ },
+ onSuccess: async () => {
+ await mutatePrefix("/api/groups");
+ setIsOpen(false);
+ toast.success(
+ linkIndex !== undefined
+ ? "Destination URL updated successfully!"
+ : "Destination URL added successfully!",
+ );
+ },
+ onError: () => {
+ toast.error("Failed to save destination URL. Please try again.");
+ },
+ });
};
+ const isEditing = linkIndex !== undefined && link;
+
return (
<form onSubmit={handleSubmit(onSubmit)}>
<div className="sticky top-0 z-10 border-b border-neutral-200 bg-white">
<div className="flex h-16 items-center justify-between px-6 py-4">
- <h2 className="text-lg font-semibold">Add destination URL</h2>
+ <h2 className="text-lg font-semibold">
+ {isEditing ? "Edit destination URL" : "Add destination URL"}
+ </h2>
</div>
</div>
@@ -118,27 +179,29 @@ function AddDestinationUrlModalContent({
</div>
</div>
- <div className="flex items-start gap-2.5">
- <input
- type="checkbox"
- className="mt-1 h-4 w-4 rounded border-neutral-300 text-neutral-900 focus:ring-neutral-500"
- required
- />
- <label
- htmlFor="conversionTracking"
- className="text-sm text-neutral-600"
- >
- I confirm that conversion tracking has been set up on this URL.{" "}
- <a
- href="https://dub.co/docs/partners/quickstart"
- target="_blank"
- rel="noreferrer noopener"
- className="text-neutral-900 underline hover:text-neutral-700"
+ {linkIndex === undefined && (
+ <div className="flex items-start gap-2.5">
+ <input
+ type="checkbox"
+ className="mt-1 h-4 w-4 rounded border-neutral-300 text-neutral-900 focus:ring-neutral-500"
+ required
+ />
+ <label
+ htmlFor="conversionTracking"
+ className="text-sm text-neutral-600"
>
- Learn more
- </a>
- </label>
- </div>
+ I confirm that conversion tracking has been set up on this URL.{" "}
+ <a
+ href="https://dub.co/docs/partners/quickstart"
+ target="_blank"
+ rel="noreferrer noopener"
+ className="text-neutral-900 underline hover:text-neutral-700"
+ >
+ Learn more
+ </a>
+ </label>
+ </div>
+ )}
</div>
</div>
@@ -156,8 +219,9 @@ function AddDestinationUrlModalContent({
<Button
type="submit"
variant="primary"
- text="Add destination URL"
+ text={isEditing ? "Update destination URL" : "Add destination URL"}
className="w-fit"
+ disabled={!url || !urlValidationMode}
loading={isSubmitting}
/>
</div>
@@ -169,12 +233,16 @@ function AddDestinationUrlModalContent({
export function AddDestinationUrlModal({
isOpen,
setIsOpen,
+ linkIndex,
}: AddDestinationUrlModalProps & {
isOpen: boolean;
}) {
return (
<Modal showModal={isOpen} setShowModal={setIsOpen}>
- <AddDestinationUrlModalContent setIsOpen={setIsOpen} />
+ <AddDestinationUrlModalContent
+ setIsOpen={setIsOpen}
+ linkIndex={linkIndex}
+ />
</Modal>
);
}
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/groups/[groupSlug]/links/add-edit-default-partner-link-sheet.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/groups/[groupSlug]/links/add-edit-default-partner-link-sheet.tsx
index 248c3f2e0c..d4b353c702 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/groups/[groupSlug]/links/add-edit-default-partner-link-sheet.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/groups/[groupSlug]/links/add-edit-default-partner-link-sheet.tsx
@@ -71,17 +71,6 @@ function DefaultPartnerLinkSheetContent({
return;
}
- // Check if we're at the maximum number of default links
- const hasReachedMaxLinks =
- (group?.defaultLinks?.length || 0) >= MAX_DEFAULT_PARTNER_LINKS;
-
- if (linkIndex === undefined && hasReachedMaxLinks) {
- toast.error(
- `You can only create up to ${MAX_DEFAULT_PARTNER_LINKS} default links.`,
- );
- return;
- }
-
let updatedDefaultLinks: DefaultPartnerLink[];
const currentDefaultLinks = group.defaultLinks || [];
@@ -90,6 +79,14 @@ function DefaultPartnerLinkSheetContent({
updatedDefaultLinks = [...currentDefaultLinks];
updatedDefaultLinks[linkIndex] = data;
} else {
+ // Check if we're at the maximum number of default links
+ if (currentDefaultLinks.length >= MAX_DEFAULT_PARTNER_LINKS) {
+ toast.error(
+ `You can only create up to ${MAX_DEFAULT_PARTNER_LINKS} default links.`,
+ );
+ return;
+ }
+
// Creating new link
updatedDefaultLinks = [...currentDefaultLinks, data];
}
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/groups/[groupSlug]/links/group-additional-links.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/groups/[groupSlug]/links/group-additional-links.tsx
index d744f58685..e66dfd2d70 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/groups/[groupSlug]/links/group-additional-links.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/groups/[groupSlug]/links/group-additional-links.tsx
@@ -3,7 +3,10 @@
import { mutatePrefix } from "@/lib/swr/mutate";
import { useApiMutation } from "@/lib/swr/use-api-mutation";
import useGroup from "@/lib/swr/use-group";
-import { updateGroupSchema } from "@/lib/zod/schemas/groups";
+import {
+ MAX_ADDITIONAL_PARTNER_LINKS,
+ updateGroupSchema,
+} from "@/lib/zod/schemas/groups";
import { Button, LinkLogo, NumberStepper } from "@dub/ui";
import { getApexDomain, getPrettyUrl } from "@dub/utils";
import { PropsWithChildren } from "react";
@@ -85,7 +88,7 @@ export function GroupAdditionalLinks() {
<div className="flex flex-col gap-2">
{additionalLinks.length > 0 &&
additionalLinks.map((link, index) => (
- <DestinationUrl key={index} url={link.url} />
+ <DestinationUrl key={index} link={link} linkIndex={index} />
))}
</div>
@@ -94,6 +97,12 @@ export function GroupAdditionalLinks() {
variant="primary"
className="mt-4 h-8 w-fit rounded-lg px-3"
onClick={() => setIsOpen(true)}
+ disabled={additionalLinks.length >= MAX_ADDITIONAL_PARTNER_LINKS}
+ disabledTooltip={
+ additionalLinks.length >= MAX_ADDITIONAL_PARTNER_LINKS
+ ? `You can only create up to ${MAX_ADDITIONAL_PARTNER_LINKS} additional destination URLs.`
+ : undefined
+ }
/>
</div>
</SettingsRow>
@@ -136,31 +145,47 @@ function SettingsRow({
);
}
-function DestinationUrl({ url }: { url: string }) {
+function DestinationUrl({
+ link,
+ linkIndex,
+}: {
+ link: { url: string; urlValidationMode: string };
+ linkIndex: number;
+}) {
+ const { addDestinationUrlModal, setIsOpen } = useAddDestinationUrlModal({
+ linkIndex,
+ });
+
return (
- <div className="border-border-subtle group relative flex h-16 items-center gap-3 rounded-xl border bg-white p-4 transition-all hover:border-neutral-300 hover:shadow-sm">
- <div className="relative flex shrink-0 items-center">
- <div className="absolute inset-0 h-8 w-8 rounded-full border border-neutral-200 sm:h-10 sm:w-10">
- <div className="h-full w-full rounded-full border border-white bg-gradient-to-t from-neutral-100" />
- </div>
- <div className="relative z-10 p-2">
- {url ? (
- <LinkLogo
- apexDomain={getApexDomain(url)}
- className="size-4 sm:size-6"
- imageProps={{
- loading: "lazy",
- }}
- />
- ) : (
- <div className="size-4 rounded-full bg-neutral-200 sm:size-6" />
- )}
+ <>
+ <div
+ className="border-border-subtle group relative flex h-16 cursor-pointer items-center gap-3 rounded-xl border bg-white p-4 transition-all hover:border-neutral-300 hover:shadow-sm"
+ onClick={() => setIsOpen(true)}
+ >
+ <div className="relative flex shrink-0 items-center">
+ <div className="absolute inset-0 h-8 w-8 rounded-full border border-neutral-200 sm:h-10 sm:w-10">
+ <div className="h-full w-full rounded-full border border-white bg-gradient-to-t from-neutral-100" />
+ </div>
+ <div className="relative z-10 p-2">
+ {link.url ? (
+ <LinkLogo
+ apexDomain={getApexDomain(link.url)}
+ className="size-4 sm:size-6"
+ imageProps={{
+ loading: "lazy",
+ }}
+ />
+ ) : (
+ <div className="size-4 rounded-full bg-neutral-200 sm:size-6" />
+ )}
+ </div>
</div>
+ <span className="text-content-default truncate text-sm font-semibold">
+ {getPrettyUrl(link.url)}
+ </span>
</div>
- <span className="text-content-default truncate text-sm font-semibold">
- {getPrettyUrl(url)}
- </span>
- </div>
+ {addDestinationUrlModal}
+ </>
);
}
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/groups/[groupSlug]/links/page.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/groups/[groupSlug]/links/page.tsx
index 0af461037b..3717b7c6cc 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/groups/[groupSlug]/links/page.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/groups/[groupSlug]/links/page.tsx
@@ -5,8 +5,8 @@ import { GroupUTM } from "./group-utm";
export default function GroupDefaultLinksPage() {
return (
<div className="flex flex-col gap-6">
- <GroupAdditionalLinks />
<GroupDefaultLinks />
+ <GroupAdditionalLinks />
<GroupUTM />
</div>
);
diff --git a/apps/web/lib/zod/schemas/groups.ts b/apps/web/lib/zod/schemas/groups.ts
index c20c47f91e..5a06d81ad9 100644
--- a/apps/web/lib/zod/schemas/groups.ts
+++ b/apps/web/lib/zod/schemas/groups.ts
@@ -18,6 +18,7 @@ export const DEFAULT_PARTNER_GROUP = {
} as const;
export const MAX_DEFAULT_PARTNER_LINKS = 3;
+
export const MAX_ADDITIONAL_PARTNER_LINKS = 10;
export const defaultPartnerLinkSchema = z.object({