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.

Auto generate link when inviting a partner

Kiran K32be7c2ef8ae
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/programs/[programId]/partners/invite-partner-sheet.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/programs/[programId]/partners/invite-partner-sheet.tsx
index 0e66ad6704..b4214adeb9 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/programs/[programId]/partners/invite-partner-sheet.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/programs/[programId]/partners/invite-partner-sheet.tsx
@@ -15,6 +15,7 @@ import {
Button,
Eye,
EyeSlash,
+ InfoTooltip,
Sheet,
useLocalStorage,
useMediaQuery,
@@ -50,7 +51,7 @@ function InvitePartnerSheetContent({ setIsOpen }: InvitePartnerSheetProps) {
formState: { errors },
} = useForm<InvitePartnerFormData>();
- const selectedLinkId = watch("linkId");
+ const [name, email, linkId] = watch(["name", "email", "linkId"]);
const { executeAsync, isPending } = useAction(invitePartnerAction, {
onSuccess: async () => {
@@ -116,13 +117,6 @@ function InvitePartnerSheetContent({ setIsOpen }: InvitePartnerSheetProps) {
});
};
- const buttonDisabled =
- isPending ||
- !!errors.name ||
- !!errors.email ||
- !!errors.linkId ||
- !selectedLinkId;
-
return (
<form onSubmit={handleSubmit(onSubmit)} className="flex h-full flex-col">
<div className="sticky top-0 z-10 border-b border-neutral-200 bg-white">
@@ -144,9 +138,13 @@ function InvitePartnerSheetContent({ setIsOpen }: InvitePartnerSheetProps) {
<div className="p-6">
<div className="grid grid-cols-1 gap-6">
<div>
- <label htmlFor="name" className="flex items-center space-x-2">
- <h2 className="text-sm font-medium text-neutral-900">Name</h2>
+ <label
+ htmlFor="name"
+ className="block text-sm font-medium text-neutral-900"
+ >
+ Name
</label>
+
<div className="relative mt-2 rounded-md shadow-sm">
<input
{...register("name", { required: true })}
@@ -160,9 +158,13 @@ function InvitePartnerSheetContent({ setIsOpen }: InvitePartnerSheetProps) {
</div>
<div>
- <label htmlFor="email" className="flex items-center space-x-2">
- <h2 className="text-sm font-medium text-neutral-900">Email</h2>
+ <label
+ htmlFor="email"
+ className="block text-sm font-medium text-neutral-900"
+ >
+ Email
</label>
+
<div className="relative mt-2 rounded-md shadow-sm">
<input
{...register("email", { required: true })}
@@ -176,9 +178,18 @@ function InvitePartnerSheetContent({ setIsOpen }: InvitePartnerSheetProps) {
<div>
<div className="flex items-center justify-between">
- <h2 className="text-sm font-medium text-neutral-900">
- Referral link
- </h2>
+ <div className="flex items-center gap-2">
+ <label
+ htmlFor="linkId"
+ className="block text-sm font-medium text-neutral-900"
+ >
+ Referral link{" "}
+ <span className="text-neutral-500">(optional)</span>
+ </label>
+
+ <InfoTooltip content="Choose a referral link for this partner. If left empty, a unique referral link will be created for them automatically." />
+ </div>
+
<a
href={`/${slug}/programs/${program?.id}/settings/links`}
target="_blank"
@@ -195,7 +206,7 @@ function InvitePartnerSheetContent({ setIsOpen }: InvitePartnerSheetProps) {
>
<div className="p-1">
<PartnerLinkSelector
- selectedLinkId={selectedLinkId}
+ selectedLinkId={linkId || null}
setSelectedLinkId={(id) => {
clearErrors("linkId");
setValue("linkId", id, { shouldDirty: true });
@@ -211,6 +222,7 @@ function InvitePartnerSheetContent({ setIsOpen }: InvitePartnerSheetProps) {
}}
error={!!errors.linkId}
/>
+
{errors.linkId && (
<p className="mt-2 text-xs text-red-600">
{errors.linkId.message}
@@ -243,13 +255,12 @@ function InvitePartnerSheetContent({ setIsOpen }: InvitePartnerSheetProps) {
<div>
<label
htmlFor="rewardId"
- className="flex items-center space-x-2"
+ className="block text-sm font-medium text-neutral-900"
>
- <h2 className="text-sm font-medium text-neutral-900">
- Reward{" "}
- <span className="text-neutral-500">(optional)</span>
- </h2>
+ Reward{" "}
+ <span className="text-neutral-500">(optional)</span>
</label>
+
<div className="relative mt-2 rounded-md shadow-sm">
<select
className={cn(
@@ -274,13 +285,12 @@ function InvitePartnerSheetContent({ setIsOpen }: InvitePartnerSheetProps) {
<div>
<label
htmlFor="discountId"
- className="flex items-center space-x-2"
+ className="block text-sm font-medium text-neutral-900"
>
- <h2 className="text-sm font-medium text-neutral-900">
- Discount{" "}
- <span className="text-neutral-500">(optional)</span>
- </h2>
+ Discount{" "}
+ <span className="text-neutral-500">(optional)</span>
</label>
+
<div className="relative mt-2 rounded-md shadow-sm">
<select
className={cn(
@@ -326,7 +336,7 @@ function InvitePartnerSheetContent({ setIsOpen }: InvitePartnerSheetProps) {
text="Send invite"
className="w-fit"
loading={isPending}
- disabled={buttonDisabled}
+ disabled={isPending || !name || !email}
/>
</div>
</div>
diff --git a/apps/web/lib/actions/partners/invite-partner.ts b/apps/web/lib/actions/partners/invite-partner.ts
index 03eb27bb19..e745aceb0d 100644
--- a/apps/web/lib/actions/partners/invite-partner.ts
+++ b/apps/web/lib/actions/partners/invite-partner.ts
@@ -1,6 +1,7 @@
"use server";
import { createAndEnrollPartner } from "@/lib/api/partners/create-and-enroll-partner";
+import { createPartnerLink } from "@/lib/api/partners/create-partner-link";
import { getDiscountOrThrow } from "@/lib/api/partners/get-discount-or-throw";
import { getRewardOrThrow } from "@/lib/api/partners/get-reward-or-throw";
import { invitePartnerSchema } from "@/lib/zod/schemas/partners";
@@ -15,20 +16,22 @@ import { authActionClient } from "../safe-action";
export const invitePartnerAction = authActionClient
.schema(invitePartnerSchema)
.action(async ({ parsedInput, ctx }) => {
- const { workspace } = ctx;
+ const { workspace, user } = ctx;
const { programId, name, email, linkId, rewardId, discountId } =
parsedInput;
- const [program, link, , ,] = await Promise.all([
+ let [program, link, , ,] = await Promise.all([
getProgramOrThrow({
workspaceId: workspace.id,
programId,
}),
- getLinkOrThrow({
- workspaceId: workspace.id,
- linkId,
- }),
+ linkId
+ ? getLinkOrThrow({
+ workspaceId: workspace.id,
+ linkId,
+ })
+ : null,
rewardId
? getRewardOrThrow({
@@ -45,7 +48,7 @@ export const invitePartnerAction = authActionClient
: null,
]);
- if (link.partnerId) {
+ if (link?.partnerId) {
throw new Error("Link is already associated with another partner.");
}
@@ -74,6 +77,19 @@ export const invitePartnerAction = authActionClient
}
}
+ // If the link is not provided, create a new one
+ if (!link) {
+ link = await createPartnerLink({
+ workspace,
+ program,
+ partner: {
+ name,
+ email,
+ },
+ userId: user.id,
+ });
+ }
+
await createAndEnrollPartner({
program,
link,
diff --git a/apps/web/lib/actions/safe-action.ts b/apps/web/lib/actions/safe-action.ts
index a97366f9c0..08cafc1230 100644
--- a/apps/web/lib/actions/safe-action.ts
+++ b/apps/web/lib/actions/safe-action.ts
@@ -2,6 +2,7 @@ import { prisma } from "@dub/prisma";
import { createSafeActionClient } from "next-safe-action";
import { normalizeWorkspaceId } from "../api/workspace-id";
import { getSession } from "../auth";
+import { PlanProps } from "../types";
export const actionClient = createSafeActionClient({
handleServerError: (e) => {
@@ -70,7 +71,10 @@ export const authActionClient = actionClient.use(
return next({
ctx: {
user: session.user,
- workspace,
+ workspace: {
+ ...workspace,
+ plan: workspace.plan as PlanProps,
+ },
},
});
},
diff --git a/apps/web/lib/api/links/get-link-or-throw.ts b/apps/web/lib/api/links/get-link-or-throw.ts
index 8865a1100f..5068a77966 100644
--- a/apps/web/lib/api/links/get-link-or-throw.ts
+++ b/apps/web/lib/api/links/get-link-or-throw.ts
@@ -80,5 +80,5 @@ export const getLinkOrThrow = async (params: GetLinkParams) => {
});
}
- return decodeLinkIfCaseSensitive(link);
+ return decodeLinkIfCaseSensitive(link) as Link;
};
diff --git a/apps/web/lib/zod/schemas/partners.ts b/apps/web/lib/zod/schemas/partners.ts
index 922287b23c..751b365007 100644
--- a/apps/web/lib/zod/schemas/partners.ts
+++ b/apps/web/lib/zod/schemas/partners.ts
@@ -446,7 +446,7 @@ export const invitePartnerSchema = z.object({
programId: z.string(),
name: z.string().trim().min(1).max(100),
email: z.string().trim().email().min(1).max(100),
- linkId: z.string(),
+ linkId: z.string().optional(),
rewardId: z.string().optional(),
discountId: z.string().optional(),
});