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.

Simplify invite partners step in onboarding flow

Steven Teya7cd6fc16aad
diff --git a/apps/web/app/(ee)/app.dub.co/(new-program)/[slug]/program/new/partners/form.tsx b/apps/web/app/(ee)/app.dub.co/(new-program)/[slug]/program/new/partners/form.tsx
index af50662824..ebbdb03f63 100644
--- a/apps/web/app/(ee)/app.dub.co/(new-program)/[slug]/program/new/partners/form.tsx
+++ b/apps/web/app/(ee)/app.dub.co/(new-program)/[slug]/program/new/partners/form.tsx
@@ -3,16 +3,14 @@
import { onboardProgramAction } from "@/lib/actions/partners/onboard-program";
import useWorkspace from "@/lib/swr/use-workspace";
import { ProgramData } from "@/lib/types";
-import { AlertCircleFill } from "@/ui/shared/icons";
import { Button, Input } from "@dub/ui";
import { cn } from "@dub/utils";
-import { Plus, Trash2 } from "lucide-react";
+import { Plus, Trash } from "lucide-react";
import { useAction } from "next-safe-action/hooks";
import { useRouter } from "next/navigation";
-import { useEffect, useState } from "react";
+import { useState } from "react";
import { useFieldArray, useFormContext } from "react-hook-form";
import { toast } from "sonner";
-import { useDebounce } from "use-debounce";
const MAX_PARTNERS = 5;
@@ -56,62 +54,6 @@ export function Form() {
"partners",
]);
- const [debouncedPartners] = useDebounce(partners, 500);
-
- const handleKeyFocus = (index: number) => {
- const email = watch(`partners.${index}.email`);
- const currentKey = watch(`partners.${index}.key`);
-
- if (email && !currentKey) {
- setValue(`partners.${index}.key`, generateKeyFromEmail(email));
- }
- };
-
- const runKeyChecks = async (index: number, value: string) => {
- if (!value || !domain || !workspaceId) return;
-
- try {
- const response = await fetch(
- `/api/links/exists?domain=${domain}&key=${value}&workspaceId=${workspaceId}`,
- );
-
- const { error } = await response.json();
-
- if (error) {
- setKeyErrors((prev) => ({
- ...prev,
- [index]: error.message.replace("Duplicate key: ", ""),
- }));
- } else {
- setKeyErrors((prev) => {
- const newErrors = { ...prev };
- delete newErrors[index];
- return newErrors;
- });
- }
- } catch (e) {
- console.error(e);
- }
- };
-
- const handleKeyChange = (index: number, key: string) => {
- setKeyErrors((prev) => {
- const newErrors = { ...prev };
- delete newErrors[index];
- return newErrors;
- });
- };
-
- useEffect(() => {
- if (!debouncedPartners) return;
-
- debouncedPartners.forEach((partner, index) => {
- if (partner.key) {
- runKeyChecks(index, partner.key);
- }
- });
- }, [debouncedPartners, domain, workspaceId]);
-
const { executeAsync, isPending } = useAction(onboardProgramAction, {
onSuccess: () => {
router.push(`/${workspaceSlug}/program/new/support`);
@@ -124,23 +66,6 @@ export function Form() {
const onSubmit = async (data: ProgramData) => {
if (!workspaceId) return;
-
- data.partners =
- data?.partners?.filter(
- (partner) => partner.email !== "" && partner.key !== "",
- ) ?? null;
-
- if (data.partners && data.partners.length > 0) {
- await Promise.all(
- data.partners.map((partner, index) => runKeyChecks(index, partner.key)),
- );
-
- if (Object.keys(keyErrors).length > 0) {
- toast.error("Please check the referral link for each partner.");
- return;
- }
- }
-
setHasSubmitted(true);
await executeAsync({
...data,
@@ -182,82 +107,28 @@ export function Form() {
<form onSubmit={handleSubmit(onSubmit)} className="grid gap-2">
<div className="flex flex-col gap-4">
{fields.map((field, index) => (
- <div
- key={field.id}
- className="grid grid-cols-1 gap-3 rounded-lg border border-neutral-200 p-3 sm:grid-cols-2 sm:border-0 sm:p-0"
- >
- <label>
- <span className="mb-1.5 block text-sm font-medium text-neutral-800">
- Email
- </span>
+ <div key={field.id} className="flex flex-col gap-1">
+ <span className="mb-1.5 block text-sm font-medium text-neutral-800">
+ Email
+ </span>
+ <div className="relative w-full">
<Input
{...register(`partners.${index}.email`)}
type="email"
placeholder="[email protected]"
+ className={cn("max-w-none", fields.length > 1 && "mr-12")}
/>
- </label>
-
- <label>
- <span className="mb-1.5 block text-sm font-medium text-neutral-800">
- Referral link
- </span>
- <div className="flex items-center gap-1">
- <div className="relative w-full">
- <div
- className={cn(
- "relative flex items-stretch overflow-hidden rounded-md border border-neutral-300 bg-white focus-within:border-neutral-500 focus-within:ring-1 focus-within:ring-neutral-500",
- keyErrors[index] &&
- "border-red-500 focus-within:border-red-500 focus-within:ring-red-500",
- )}
- >
- <div
- className={cn(
- "flex items-center border-r border-neutral-300 bg-neutral-100 px-3",
- keyErrors[index] && "border-red-500",
- )}
- >
- <span className="text-sm font-medium text-neutral-800">
- {domain}
- </span>
- </div>
- <input
- {...register(`partners.${index}.key`)}
- type="text"
- placeholder="panic"
- onFocus={() => handleKeyFocus(index)}
- onChange={(e) => handleKeyChange(index, e.target.value)}
- className={cn(
- "w-full border-0 bg-transparent px-3 py-2 text-sm text-neutral-900 placeholder:text-neutral-400 focus:outline-none focus:ring-0",
- keyErrors[index] &&
- "pr-10 text-red-700 placeholder-red-300",
- )}
- />
-
- {keyErrors[index] && (
- <div className="absolute inset-y-0 right-0 flex items-center pr-3">
- <AlertCircleFill className="h-5 w-5 text-red-700" />
- </div>
- )}
- </div>
- </div>
-
- {index > 0 && (
- <div className="flex w-8">
- <Button
- variant="outline"
- icon={<Trash2 className="size-4" />}
- className="size-[38px] w-full p-0"
- onClick={() => remove(index)}
- />
- </div>
- )}
- </div>
- {keyErrors[index] && (
- <p className="py-1 text-xs text-red-700">
- {keyErrors[index]}
- </p>
+ {fields.length > 1 && (
+ <Button
+ type="button"
+ variant="secondary"
+ icon={<Trash className="size-3.5 text-neutral-600" />}
+ onClick={() => remove(index)}
+ aria-label="Remove partner"
+ className="absolute right-2 top-1/2 h-auto w-auto -translate-y-1/2 p-2.5"
+ />
)}
- </label>
+ </div>
</div>
))}
@@ -269,7 +140,7 @@ export function Form() {
className="w-fit"
onClick={() => {
if (fields.length < MAX_PARTNERS) {
- append({ email: "", key: "" });
+ append({ email: "" });
}
}}
disabled={fields.length >= MAX_PARTNERS}
diff --git a/apps/web/lib/actions/partners/create-program.ts b/apps/web/lib/actions/partners/create-program.ts
index c50b36ab9f..c7afc6f5a9 100644
--- a/apps/web/lib/actions/partners/create-program.ts
+++ b/apps/web/lib/actions/partners/create-program.ts
@@ -1,10 +1,10 @@
import { createId } from "@/lib/api/create-id";
import { getDomainOrThrow } from "@/lib/api/domains/get-domain-or-throw";
-import { createLink, processLink } from "@/lib/api/links";
import { createAndEnrollPartner } from "@/lib/api/partners/create-and-enroll-partner";
+import { createPartnerLink } from "@/lib/api/partners/create-partner-link";
import { rewardfulImporter } from "@/lib/rewardful/importer";
import { isStored, storage } from "@/lib/storage";
-import { PlanProps } from "@/lib/types";
+import { WorkspaceProps } from "@/lib/types";
import { programDataSchema } from "@/lib/zod/schemas/program-onboarding";
import { sendEmail } from "@dub/email";
import { PartnerInvite } from "@dub/email/templates/partner-invite";
@@ -214,35 +214,22 @@ async function invitePartner({
workspace: Pick<Project, "id" | "plan" | "webhookEnabled">;
partner: {
email: string;
- key: string;
};
userId: string;
}) {
- const { link: partnerLink, error } = await processLink({
- payload: {
- url: program.url!,
- domain: program.domain!,
- key: partner.key,
- programId: program.id,
- trackConversion: true,
- },
- workspace: {
- id: workspace.id,
- plan: workspace.plan as PlanProps,
+ const partnerLink = await createPartnerLink({
+ workspace: workspace as WorkspaceProps,
+ program,
+ partner: {
+ name: partner.email.split("@")[0],
+ email: partner.email,
},
userId,
});
- if (error != null) {
- console.log("Error creating partner link", error);
- return;
- }
-
- const link = await createLink(partnerLink);
-
await createAndEnrollPartner({
program,
- link,
+ link: partnerLink,
workspace,
partner: {
name: partner.email.split("@")[0],
diff --git a/apps/web/lib/zod/schemas/program-onboarding.ts b/apps/web/lib/zod/schemas/program-onboarding.ts
index ae7ebfe746..ccd8770968 100644
--- a/apps/web/lib/zod/schemas/program-onboarding.ts
+++ b/apps/web/lib/zod/schemas/program-onboarding.ts
@@ -45,16 +45,12 @@ export const programInvitePartnersSchema = z.object({
.array(
z.object({
email: z.string().email("Please enter a valid email"),
- key: z.string().min(1, "Please enter a referral key"),
}),
)
.max(10, "You can only invite up to 10 partners.")
.nullable()
.transform(
- (partners) =>
- partners?.filter(
- (partner) => partner.email.trim() && partner.key.trim(),
- ) || null,
+ (partners) => partners?.filter((partner) => partner.email.trim()) || null,
),
});